cmdargs-0.10.14: Command line argument processing

Safe HaskellNone

System.Console.CmdArgs.Implicit

Contents

Description

This module provides simple command line argument processing. The main function of interest is cmdArgs. A simple example is:

data Sample = Sample {hello :: String} deriving (Show, Data, Typeable)
sample = Sample{hello = def &= help "World argument" &= opt "world"}
         &= summary "Sample v1"
main = print =<< cmdArgs sample

Attributes are used to control a number of behaviours:

Supported Types: Each field in the record must be one of the supported atomic types (String, Int, Integer, Float, Double, Bool, an enumeration, a tuple of atomic types) or a list ([]) or Maybe wrapping at atomic type.

Missing Fields: If a field is shared by multiple modes, it may be omitted in subsequent modes, and will default to the previous value.

Purity: Values created with annotations are not pure - the first time they are computed they will include the annotations, but subsequently they will not. If you wish to run the above example in a more robust way:

sample = cmdArgsMode $ Sample{hello = ... -- as before
main = print =<< cmdArgsRun sample

Even using this scheme, sometimes GHC's optimisations may share values who have the same annotation. To disable sharing you may need to specify {-# OPTIONS_GHC -fno-cse #-} in the module you define the flags.

Pure annotations: Alternatively, you may use pure annotations, which are referentially transparent, but less type safe and more verbose. The initial example may be written as:

sample = record Sample{} [hello := def += help "World argument" += opt "world"] += summary "Sample v1"

main = print =<< (cmdArgs_ sample :: IO Sample)

All the examples are written using impure annotations. To convert to pure annotations follow the rules:

 Ctor {field1 = value1 &= ann1, field2 = value2} &= ann2 ==> record Ctor{} [field1 := value1 += ann1, field2 := value2] += ann2
 Ctor (value1 &= ann1) value2 &= ann2 ==> record Ctor{} [atom value1 += ann1, atom value2] += ann2
 modes [Ctor1{...}, Ctor2{...}] ==> modes_ [record Ctor1{} [...], record Ctor2{} [...]]
 Ctor {field1 = enum [X &= ann, Y]} ==> record Ctor{} [enum_ field1 [atom X += ann, atom Y]]

If you are willing to use TemplateHaskell, you can write in the impure syntax, but have your code automatically translated to the pure style. For more details see System.Console.CmdArgs.Quote.

Synopsis

Running command lines

cmdArgs :: Data a => a -> IO a

Take impurely annotated records and run the corresponding command line. Shortcut for cmdArgsRun . cmdArgsMode.

To use cmdArgs with custom command line arguments see withArgs.

cmdArgsMode :: Data a => a -> Mode (CmdArgs a)

Take impurely annotated records and turn them in to a Mode value, that can make use of the System.Console.CmdArgs.Explicit functions (i.e. process).

Annotated records are impure, and will only contain annotations on their first use. The result of this function is pure, and can be reused.

cmdArgsRun :: Mode (CmdArgs a) -> IO a

Run a Mode structure. This function reads the command line arguments and then performs as follows:

  • If invalid arguments are given, it will display the error message and exit.
  • If --help is given, it will display the help message and exit.
  • If --version is given, it will display the version and exit.
  • In all other circumstances the program will return a value.
  • Additionally, if either --quiet or --verbose is given (see verbosity) it will set the verbosity (see setVerbosity).

cmdArgs_ :: Data a => Annotate Ann -> IO a

Take purely annotated records and run the corresponding command line. Shortcut for cmdArgsRun . cmdArgsMode_.

To use cmdArgs_ with custom command line arguments see withArgs.

cmdArgsMode_ :: Data a => Annotate Ann -> Mode (CmdArgs a)

Take purely annotated records and turn them in to a Mode value, that can make use of the System.Console.CmdArgs.Explicit functions (i.e. process).

cmdArgsApply :: CmdArgs a -> IO a

Perform the necessary actions dictated by a CmdArgs structure.

data CmdArgs a

A structure to store the additional data relating to --help, --version, --quiet and --verbose.

Constructors

CmdArgs 

Fields

cmdArgsValue :: a

The underlying value being wrapped.

cmdArgsHelp :: Maybe String

Just if --help is given, then gives the help message for display, including a trailing newline.

cmdArgsVersion :: Maybe String

Just if --version is given, then gives the version message for display, including a trailing newline.

cmdArgsVerbosity :: Maybe Verbosity

Just if --quiet or --verbose is given, then gives the verbosity to use.

cmdArgsPrivate :: CmdArgsPrivate

Private: Only exported due to Haddock limitations.

Instances

Functor CmdArgs 
Typeable1 CmdArgs 
Eq a => Eq (CmdArgs a) 
Data a => Data (CmdArgs a) 
Ord a => Ord (CmdArgs a) 
Show a => Show (CmdArgs a) 

Constructing command lines

Attributes can work on a flag (inside a field), on a mode (outside the record), or on all modes (outside the modes call).

opt :: (Show a, Typeable a) => a -> Ann

Flag: "I want users to be able to omit the value associated with this flag."

Make the value of a flag optional. If --flag is given, it will be treated as --flag=this_argument.

 {hello = def &= opt "foo"}
   -h --hello[=VALUE]    (default=foo)

Note that all flags in CmdArgs are optional, and if omitted will use their default value. Those annotated with opt also allow the flag to be present without an associated value. As an example:

 {hello = "DEFAULT" &= opt "OPTIONAL"}
 $ main
 {hello = "DEFAULT"}
 $ main --hello
 {hello = "OPTIONAL"}
 $ main --hello=VALUE
 {hello = "VALUE"}

typ :: String -> Ann

Flag: "For this flag, users need to give something of type ..."

The the type of a flag's value, usually upper case. Only used for the help message. Commonly the type will be FILE (typFile) or DIR (typDir).

 {hello = def &= typ "MESSAGE"}
   -h --hello=MESSAGE

typFile :: Ann

Flag: "Users must give a file for this flag's value."

Alias for typ FILE.

typDir :: Ann

Flag: "Users must give a directory for this flag's value."

Alias for typ DIR.

help :: String -> Ann

Flag/Mode: "The help message is ..."

Descriptive text used in the help output.

 {hello = def &= help "Help message"}
   -h --hello=VALUE      Help message

name :: String -> Ann

Flag: "Use this flag name for this field."

Add flags which trigger this option.

 {hello = def &= name "foo"}
   -h --hello --foo=VALUE

args :: Ann

Flag: "Put non-flag arguments here."

All argument flags not captured by argPos are returned by args.

 {hello = def &= args}

argPos :: Int -> Ann

Flag: "Put the nth non-flag argument here."

This field should be used to store a particular argument position (0-based).

 {hello = def &= argPos 0}

groupname :: String -> Ann

Flag/Mode: "Give these flags/modes a group name in the help output."

This mode will be used for all following modes/flags, until the next groupname.

 {hello = def &= groupname "Welcomes"}
 Welcomes
   -h --hello=VALUE

details :: [String] -> Ann

Mode: "A longer description of this mode is ..."

Suffix to be added to the help message.

 Sample{..} &= details ["More details on the website www.example.org"]

summary :: String -> Ann

Modes: "My program name/version/copyright is ..."

One line summary of the entire program, the first line of --help and the only line of --version. If the string contains a version number component will also provide --numeric-version.

 Sample{..} &= summary "CmdArgs v0.0, (C) Neil Mitchell 1981"

auto :: Ann

Mode: "If the user doesn't give a mode, use this one."

This mode is the default. If no mode is specified and a mode has this attribute then that mode is selected, otherwise an error is raised.

 modes [Mode1{..}, Mode2{..} &= auto, Mode3{..}]

program :: String -> Ann

Modes: "My program executable is named ..."

This is the name of the program executable. Only used in the help message. Defaults to the type of the mode.

 Sample{..} &= program "sample"

explicit :: Ann

Flag: "Don't guess any names for this field."

A field should not have any flag names guessed for it. All flag names must be specified by flag.

 {hello = def &= explicit &= name "foo"}
   --foo=VALUE

ignore :: Ann

Flag/Mode: "Ignore this field, don't let the user set it."

A mode or field is not dealt with by CmdArgs.

 {hello = def, extra = def &= ignore}
   --hello=VALUE

verbosity :: Ann

Modes: "My program needs verbosity flags."

Add --verbose and --quiet flags.

helpArg :: [Ann] -> Ann

Modes: "Customise the help argument."

Add extra options to a help argument, such as help, name, ignore or explicit.

 Sample{..} &= helpArg [explicit, name "h"]

versionArg :: [Ann] -> Ann

Modes: "Customise the version argument."

Add extra options to a version argument, such as help, name, ignore, summary or explicit.

 Sample{..} &= versionArg [ignore]

verbosityArgs :: [Ann] -> [Ann] -> Ann

Modes: "Customise the verbosity arguments."

Add extra options to a verbosity arguments (--verbose and --quiet), such as help, name, ignore or explicit. The verbose options come first, followed by the quiet options.

 Sample{..} &= verbosityArgs [ignore] [name "silent", explicit]

noAtExpand :: Ann

Program: "Turn off @ expansion."

Usually arguments starting with @ are treated as a file containing a set of arguments. This annotation turns off that behaviour.

 Sample{..} &= noAtExpand

Impure

(&=) :: Data val => val -> Ann -> val

Add an annotation to a value. Note that if the value is evaluated more than once the annotation will only be available the first time.

modes :: Data val => [val] -> val

Modes: "I want a program with multiple modes, like darcs or cabal."

Takes a list of modes, and creates a mode which includes them all. If you want one of the modes to be chosen by default, see auto.

 data Modes = Mode1 | Mode2 | Mode3 deriving Data
 cmdArgs $ modes [Mode1,Mode2,Mode3]

enum :: Data val => [val] -> val

Flag: "I want several different flags to set this one field to different values."

This annotation takes a type which is an enumeration, and provides multiple separate flags to set the field to each value. The first element in the list is used as the value of the field.

 data State = On | Off deriving Data
 data Mode = Mode {state :: State}
 cmdArgs $ Mode {state = enum [On &= help "Turn on",Off &= help "Turn off"]}
   --on   Turn on
   --off  Turn off

This annotation can be used to allow multiple flags within a field:

 data Mode = Mode {state :: [State]}
 cmdArgs $ Mode {state = enum [[] &= ignore, [On] &= help "Turn on", [Off] &= help "Turn off"]}

Now --on --off would produce Mode [On,Off].

Pure

(+=) :: Annotate ann -> ann -> Annotate ann

Add an annotation to a value.

record :: Data a => a -> [Annotate ann] -> Annotate ann

Create a constructor/record. The first argument should be the type of field, the second should be a list of fields constructed originally defined by := or :=+.

This operation is not type safe, and may raise an exception at runtime if any field has the wrong type or label.

atom :: Data val => val -> Annotate ann

Lift a pure value to an annotation.

data Annotate ann

This type represents an annotated value. The type of the underlying value is not specified.

Constructors

forall c f . (Data c, Data f) => (c -> f) := f

Construct a field, fieldname := value.

Instances

Typeable1 Annotate 

enum_ :: (Data c, Data f) => (c -> f) -> [Annotate Ann] -> Annotate Ann

Like enum, but using the pure annotations.

modes_ :: [Annotate Ann] -> Annotate Ann

Like modes, but using the pure annotations.

Re-exported for convenience

Provides a few opaque types (for writing type signatures), verbosity control, default values with def and the Data/Typeable type classes.

data Ann

The general type of annotations that can be associated with a value.

Instances

Eq Ann 
Data Ann 
Ord Ann 
Show Ann 
Typeable Ann 

data Mode a

A mode. Do not use the Mode constructor directly, instead use mode to construct the Mode and then record updates. Each mode has three main features:

To produce the help information for a mode, either use helpText or show.

Instances

Remap Mode 
Show (Mode a) 

class Typeable a => Data a

Instances

Data Bool 
Data Char 
Data Double 
Data Float 
Data Int 
Data Int8 
Data Int16 
Data Int32 
Data Int64 
Data Integer 
Data Ordering 
Data Word 
Data Word8 
Data Word16 
Data Word32 
Data Word64 
Data Exp 
Data Match 
Data Clause 
Data Pat 
Data Type 
Data Dec 
Data Name 
Data FunDep 
Data Pred 
Data TyVarBndr 
Data () 
Data Ann 
Data Verbosity 
Data TyLit 
Data Strict 
Data Stmt 
Data Safety 
Data RuleMatch 
Data RuleBndr 
Data Range 
Data Pragma 
Data Phases 
Data NameSpace 
Data Lit 
Data Inline 
Data Info 
Data Guard 
Data Foreign 
Data FixityDirection 
Data Fixity 
Data FamFlavour 
Data Con 
Data Callconv 
Data Body 
Data NameFlavour 
Data OccName 
Data PkgName 
Data ModName 
Data a => Data [a] 
(Data a, Integral a) => Data (Ratio a) 
Typeable a => Data (Ptr a) 
Data a => Data (Maybe a) 
Typeable a => Data (ForeignPtr a) 
Data a => Data (CmdArgs a) 
(Data a, Data b) => Data (Either a b) 
(Data a, Data b) => Data (a, b) 
(Typeable a, Data b, Ix a) => Data (Array a b) 
(Data a, Data b, Data c) => Data (a, b, c) 
(Data a, Data b, Data c, Data d) => Data (a, b, c, d) 
(Data a, Data b, Data c, Data d, Data e) => Data (a, b, c, d, e) 
(Data a, Data b, Data c, Data d, Data e, Data f) => Data (a, b, c, d, e, f) 
(Data a, Data b, Data c, Data d, Data e, Data f, Data g) => Data (a, b, c, d, e, f, g) 

class Typeable a

Instances

Typeable Bool 
Typeable Char 
Typeable Double 
Typeable Float 
Typeable Int 
Typeable Int8 
Typeable Int16 
Typeable Int32 
Typeable Int64 
Typeable Integer 
Typeable Ordering 
Typeable RealWorld 
Typeable Word 
Typeable Word8 
Typeable Word16 
Typeable Word32 
Typeable Word64 
Typeable Exp 
Typeable Match 
Typeable Clause 
Typeable Pat 
Typeable Type 
Typeable Dec 
Typeable Name 
Typeable FunDep 
Typeable Pred 
Typeable TyVarBndr 
Typeable () 
Typeable TypeRep 
Typeable TyCon 
Typeable Ann 
Typeable Verbosity 
Typeable SomeException 
Typeable CInt 
Typeable Handle__ 
Typeable Handle 
Typeable IOException 
Typeable CSize 
Typeable ErrorCall 
Typeable ArithException 
Typeable ExitCode 
Typeable Deadlock 
Typeable BlockedIndefinitelyOnSTM 
Typeable BlockedIndefinitelyOnMVar 
Typeable AsyncException 
Typeable AssertionFailed 
Typeable ArrayException 
Typeable CLong 
Typeable RecUpdError 
Typeable RecSelError 
Typeable RecConError 
Typeable PatternMatchFail 
Typeable NonTermination 
Typeable NoMethodError 
Typeable NestedAtomically 
Typeable CChar 
Typeable CWchar 
Typeable CUShort 
Typeable CUSeconds 
Typeable CULong 
Typeable CULLong 
Typeable CUIntPtr 
Typeable CUIntMax 
Typeable CUInt 
Typeable CUChar 
Typeable CTime 
Typeable CSigAtomic 
Typeable CShort 
Typeable CSUSeconds 
Typeable CSChar 
Typeable CPtrdiff 
Typeable CLLong 
Typeable CIntPtr 
Typeable CIntMax 
Typeable CFloat 
Typeable CDouble 
Typeable CClock 
Typeable TyLit 
Typeable Strict 
Typeable Stmt 
Typeable Safety 
Typeable RuleMatch 
Typeable RuleBndr 
Typeable Range 
Typeable Pragma 
Typeable Phases 
Typeable NameSpace 
Typeable Lit 
Typeable Inline 
Typeable Info 
Typeable Guard 
Typeable Foreign 
Typeable FixityDirection 
Typeable Fixity 
Typeable FamFlavour 
Typeable Con 
Typeable Callconv 
Typeable Body 
Typeable NameFlavour 
Typeable OccName 
Typeable PkgName 
Typeable ModName 
(Typeable1 s, Typeable a) => Typeable (s a)