command-qq-0.1.0.0: Quasiquoters for external commands

Safe HaskellNone

System.Command.QQ

Contents

Description

Quasiquoters for external commands

Synopsis

Quasiquoters

sh :: QuasiQuoterSource

Quasiquoter for the default shell

"default" here means it uses value of SHELL environment variable or /bin/sh if it is not set.

>>> [sh|echo "hi!"|] :: IO ExitCode
hi!
ExitSuccess
>>> [sh|echo "hi!"|] :: IO String
"hi!\n"

Haskell values can be embedded with Ruby-like syntax:

>>> let apples = 7
>>> [sh|echo "#{apples} apples!"|] :: IO String
"7 apples!\n"

Works only for expressions (obviously):

>>> return 3 :: IO [sh|blah|]

<interactive>:116:16:
    Exception when trying to run compile-time code:
      this quasiquoter does not support splicing types
      Code: quoteType sh "blah"

shell :: FilePath -> QuasiQuoterSource

Shell commands quasiquoter maker

"Shell" here means something that implements the following interface:

 <SHELL> -c <COMMAND>

e.g. sh, bash, zsh, ksh, tcsh, python, etc

Everything that applies to sh applies to shell

interpreter :: FilePath -> QuasiQuoterSource

Interpreter commands quasiquoter maker

"Interpreter" here means something that implements the following interface:

 <INTERPRETER> -e <COMMAND>

e.g. perl, ruby, ghc, etc

Everything that applies to sh applies to interpreter

Customizations

quoter :: (String -> Q Exp) -> QuasiQuoterSource

Construct quasiquoter from function taking the string and producing Haskell expression.

Other kinds of quasiquoters (patterns, types or declarations quasiquoters) will fail in compile time

callCommandSource

Arguments

:: FilePath

Command path

-> [String]

Arguments that go to command before quasiquoter contents

-> String

Quasiquoter contents

-> Q Exp 

Construct Haskell expression for external command call

class Eval r whereSource

Different interesting return types for quasiquoters

Instances here mostly resemble the types of things in System.Process

Methods

eval :: String -> [String] -> rSource

Instances

Eval (IO String)

Return only stdout of external process

Does not care if external process failed.

>>> [sh|echo hello world|] :: IO String
"hello world\n"
>>> [sh|echo hello world; return 1|] :: IO String
"hello world\n"
Eval (IO ())

Most basic instance: nothing is known about what happened in external command

>>> [sh|echo hello world|] :: IO ()
hello world
(~ * s ExitCode, ~ * o String, ~ * e String) => Eval (IO (s, o, e))

Return exit code, stdout, and stderr of external process

>>> [sh|echo hello world; echo bye world >&2; exit 1|] :: IO (ExitCode, String, String)
(ExitFailure 1,"hello world\n","bye world\n")
Eval (IO ExitCode)

Return only exit code of external process

>>> [sh|echo hello world|] :: IO ExitCode
hello world
ExitSuccess
>>> [sh|exit 1|] :: IO ExitCode
ExitFailure 1
(~ * i String, ~ * o (ExitCode, String, String)) => Eval (i -> IO o)

Return exit code, stdout, and stderr of external process and consume stdin from supplied String

>>> [sh|while read line; do echo ${#line}; done|] "hello\nworld!\n"
(ExitSuccess,"5\n6\n","")

class Embed a whereSource

Embed haskell values into external commands

I recommend using -XExtendedDefaultRules for modules where you want to embed values, it would save for annoying type annotations for numeric literals

 embed . embed = embed

Methods

embed :: a -> StringSource

Instances

Embed Char
>>> embed 'c'
"c"
Embed Double
>>> embed 4.0
"4.0"
>>> embed (7 :: Double)
"7.0"
Embed Float
>>> embed (7 :: Float)
"7.0"
Embed Int
>>> embed (7 :: Int)
"7"
Embed Int8 
Embed Int16 
Embed Int32 
Embed Int64 
Embed Integer
>>> embed 4
"4"
>>> embed (7 :: Integer)
"7"
Embed Word 
Embed Word8 
Embed Word16 
Embed Word32 
Embed Word64 
Embed String
>>> embed "hi"
"hi"