| Safe Haskell | None |
|---|
Development.Shake.Command
Description
This module provides functions for calling command line programs, primarily
command and cmd . As a simple example:
command [] "gcc" ["-c",myfile]
The functions from this module are now available directly from Development.Shake.
You should only need to import this module if you are using the cmd function in the IO monad.
Synopsis
- command :: CmdResult r => [CmdOption] -> String -> [String] -> Action r
- command_ :: [CmdOption] -> String -> [String] -> Action ()
- cmd :: CmdArguments args => args :-> Action r
- class CmdArguments t
- newtype Stdout = Stdout {
- fromStdout :: String
- newtype Stderr = Stderr {
- fromStderr :: String
- newtype Exit = Exit {}
- class CmdResult a
- data CmdOption
- = Cwd FilePath
- | Env [(String, String)]
- | Stdin String
- | Shell
- | BinaryPipes
- | Traced String
- | WithStderr Bool
- | EchoStdout Bool
- | EchoStderr Bool
- addPath :: MonadIO m => [String] -> [String] -> m CmdOption
- addEnv :: MonadIO m => [(String, String)] -> m CmdOption
Documentation
command :: CmdResult r => [CmdOption] -> String -> [String] -> Action rSource
Execute a system command. Before running command make sure you need any files
that are used by the command.
This function takes a list of options (often just [], see CmdOption for the available
options), the name of the executable (either a full name, or a program on the $PATH) and
a list of arguments. The result is often (), but can be a tuple containg any of Stdout ,
Stderr and Exit . Some examples:
command_[] "gcc" ["-c","myfile.c"] -- compile a file, throwing an exception on failureExitc <-command[] "gcc" ["-c",myfile] -- run a command, recording the exit code (Exitc,Stderrerr) <-command[] "gcc" ["-c","myfile.c"] -- run a command, recording the exit code and error outputStdoutout <-command[] "gcc" ["-MM","myfile.c"] -- run a command, recording the outputcommand_[Cwd"generated"] "gcc" ["-c",myfile] -- run a command in a directory
Unless you retrieve the ExitCode using Exit , any ExitFailure will throw an error, including
the Stderr in the exception message. If you capture the Stdout or Stderr , that stream will not be echoed to the console,
unless you use the option EchoStdout or EchoStderr .
If you use command inside a do block and do not use the result, you may get a compile-time error about being
unable to deduce CmdResult . To avoid this error, use command_ .
cmd :: CmdArguments args => args :-> Action rSource
Execute a system command. Before running cmd make sure you need any files
that are used by the command.
-
Stringarguments are treated as whitespace separated arguments. -
[String]arguments are treated as literal arguments. -
CmdOptionarguments are used as options.
To take the examples from command :
() <-cmd"gcc -c myfile.c" -- compile a file, throwing an exception on failureExitc <-cmd"gcc -c" [myfile] -- run a command, recording the exit code (Exitc,Stderrerr) <-cmd"gcc -c myfile.c" -- run a command, recording the exit code and error outputStdoutout <-cmd"gcc -MM myfile.c" -- run a command, recording the outputcmd(Cwd"generated") "gcc -c" [myfile] ::Action() -- run a command in a directory
When passing file arguments we use [myfile] so that if the myfile variable contains spaces they are properly escaped.
If you use cmd inside a do block and do not use the result, you may get a compile-time error about being
unable to deduce CmdResult . To avoid this error, bind the result to (), or include a type signature.
The cmd command can also be run in the IO monad, but then Traced is ignored and command lines are not echoed.
class CmdArguments t Source
Instances
Collect the stdout of the process.
If you are collecting the stdout, it will not be echoed to the terminal, unless you include EchoStdout .
Collect the stderr of the process.
If you are collecting the stderr, it will not be echoed to the terminal, unless you include EchoStderr .
Collect the ExitCode of the process.
If you do not collect the exit code, any ExitFailure will cause an exception.
Constructors
Change the current directory in the spawned process. By default uses this processes current directory.
Change the environment variables in the spawned process. By default uses this processes environment.
Use addPath to modify the $PATH variable, or addEnv to modify other variables.
Pass the command to the shell without escaping - any arguments will be joined with spaces. By default arguments are escaped properly.
Treat the stdin/stdout/stderr messages as binary. By default streams use text encoding.
Name to use with traced , or "" for no tracing. By default traces using the name of the executable.
Should I include the stderr in the exception if the command fails? Defaults to True .
addPath :: MonadIO m => [String] -> [String] -> m CmdOption Source
Produce a CmdOption of value Env that is the current environment, plus a
prefix and suffix to the $PATH environment variable. For example:
opt <-addPath["/usr/special"] []cmdopt "userbinary --version"
Would prepend /usr/special to the current $PATH, and the command would pick
/usr/special/userbinary, if it exists. To add other variables see addEnv .
addEnv :: MonadIO m => [(String, String)] -> m CmdOption Source
Produce a CmdOption of value Env that is the current environment, plus the argument
environment variables. For example:
opt <-addEnv[("CFLAGS","-O2")]cmdopt "gcc -c main.c"
Would add the environment variable $CFLAGS with value -O2. If the variable $CFLAGS
was already defined it would be overwritten. If you wish to modify $PATH see addPath .