Last active
March 25, 2016 11:06
-
-
Save ozgurakgun/1f1917dc781f8f230dcf to your computer and use it in GitHub Desktop.
print_stderr False, but stderr is still printed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
{-# OPTIONS_GHC -Wall #-} | |
module Main where | |
import System.IO ( Handle ) | |
import System.Environment ( getArgs ) | |
import GHC.IO.Handle ( hIsEOF, hClose, hGetLine ) | |
import Control.Monad.IO.Class ( liftIO ) | |
-- shelly | |
import Shelly ( runHandle, lastStderr, lastExitCode, errExit, Sh, shelly, print_stdout, print_stderr ) | |
-- text | |
import qualified Data.Text as T ( pack, unpack ) | |
main :: IO () | |
main = do | |
args <- getArgs | |
(stdout, stderr, exitCode) <- sh $ do | |
stdout <- runHandle "grep" (map T.pack args) (liftIO . stdoutHandler) | |
stderr <- lastStderr | |
exitCode <- lastExitCode | |
return (stdout, stderr, exitCode) | |
putStrLn $ unlines $ | |
[ "--------------------" | |
, unwords ["number of lines in stdout:", show stdout] | |
, unwords ["stderr:", T.unpack stderr] | |
, unwords ["exitCode:", show exitCode] | |
] | |
sh :: Sh a -> IO a | |
sh = shelly . print_stdout False . print_stderr False . errExit False | |
stdoutHandler :: Handle -> IO Int | |
stdoutHandler h = do | |
eof <- hIsEOF h | |
if eof | |
then do | |
hClose h | |
return 0 | |
else do | |
line <- hGetLine h | |
putStrLn $ unwords ["STDOUT:", line] | |
(1+) <$> stdoutHandler h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ echo foo > foo.txt | |
$ stack runhaskell shelly_test.hs --package shelly --package text -- foo foo.txt | |
Run from outside a project, using implicit global project config | |
Using resolver: lts-5.9 from implicit global project's config file: ... | |
STDOUT: foo | |
-------------------- | |
number of lines in stdout: 1 | |
stderr: | |
exitCode: 0 | |
$ stack runhaskell shelly_test.hs --package shelly --package text -- bar foo.txt | |
Run from outside a project, using implicit global project config | |
Using resolver: lts-5.9 from implicit global project's config file: ... | |
-------------------- | |
number of lines in stdout: 0 | |
stderr: | |
exitCode: 1 | |
$ stack runhaskell shelly_test.hs --package shelly --package text -- foo bar.txt | |
Run from outside a project, using implicit global project config | |
Using resolver: lts-5.9 from implicit global project's config file: ... | |
grep: bar.txt: No such file or directory | |
-------------------- | |
number of lines in stdout: 0 | |
stderr: grep: bar.txt: No such file or directory | |
exitCode: 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment