Safe Haskell | Safe-Inferred |
---|
System.Console.MultiArg.CommandLine
Contents
Description
Some pre-built command line parsers. One is a simple command line parser that can parse options that take an optional argument, one or two arguments, or a variable number of arguments. For sample code that uses this parser, see System.Console.MultiArg.SampleParser.
Another parser is provided for multi-mode programs that are similar
to git
or darcs
.
Previously there was a bug in System.Environment.getArgs that would not properly encode Unicode command line arguments. multiarg used to provide its own GetArgs module to deal with this. This bug was in base 4.3.1.0, which was bundled with ghc 7.0.4. This bug was fixed in base 4.4.0.0, which came with ghc 7.2. Since this bug has been fixed for awhile, multiarg no longer has its own GetArgs module.
- data Intersperse
- type ProgName = String
- data Opts s a = Opts {
- oOptions :: [OptSpec a]
- oShortcuts :: [OptSpec s]
- class MapShortcuts f where
- smap :: (a -> b) -> f a o -> f b o
- data OptsWithPosArgs s a = OptsWithPosArgs {
- opOpts :: Opts s a
- opIntersperse :: Intersperse
- opPosArg :: String -> Either InputError a
- data Mode s r = forall a . Mode {
- mModeName :: String
- mGetResult :: [a] -> r
- mOpts :: OptsWithPosArgs s a
- simplePure :: OptsWithPosArgs s a -> [String] -> Either Error (Either s [a])
- simpleIO :: [OptSpec a] -> Intersperse -> (String -> Either InputError a) -> IO [a]
- simpleHelp :: (ProgName -> String) -> [OptSpec a] -> Intersperse -> (String -> Either InputError a) -> IO [a]
- simpleHelpVersion :: (ProgName -> String) -> (ProgName -> String) -> [OptSpec a] -> Intersperse -> (String -> Either InputError a) -> IO [a]
- modesPure :: Opts s g -> ([g] -> Either String (Either r [Mode s r])) -> [String] -> Either Error (Either s r)
- modesIO :: Opts s g -> ([g] -> Either String (Either r [Mode s r])) -> IO (Either s r)
- optsHelp :: h -> [OptSpec a] -> Opts h a
- optsHelpVersion :: h -> h -> [OptSpec a] -> Opts h a
- modeHelp :: String -> h -> ([a] -> r) -> [OptSpec a] -> Intersperse -> (String -> Either InputError a) -> Mode h r
Interspersion control
data Intersperse Source
What to do after encountering the first non-option, non-option-argument word on the command line? In either case, no more options are parsed after a stopper.
Constructors
Intersperse | Additional options are allowed on the command line after
encountering the first positional argument. For example, if |
StopOptions | No additional options will be parsed after encountering the
first positional argument. For example, if |
Types
The name of the program that was entered on the command line, obtained from System.Environment.getProgName.
Specifies a set of options.
Constructors
Opts | |
Fields
|
Instances
MapShortcuts Opts | |
Functor (Opts s) |
class MapShortcuts f whereSource
Things that contain shortcut options that can be changed.
data OptsWithPosArgs s a Source
Specification for both options and positional arguments.
Constructors
OptsWithPosArgs | |
Fields
|
Instances
Specifies a mode.
Constructors
forall a . Mode | |
Fields
|
Instances
MapShortcuts Mode | |
Functor (Mode s) |
Simple parsers
Arguments
:: OptsWithPosArgs s a | Specifies allowed regular options, allowed shortcut options, and how to parse positional arguments. Also specifies whether the user may intersperse options with positional arguments. |
-> [String] | The command line arguments to parse |
-> Either Error (Either s [a]) | Returns an error if the command line arguments could not be parsed. If the parse was successful, returns an Either. A Left indicates that the user selected a shortcut option. A Right indicates that the user did not specify a shortcut option, and will contain a list of the options and positional arguments. |
A pure (non-IO) parser for simple command lines--that is, command lines that do not have modes.
Arguments
:: [OptSpec a] | Options to parse |
-> Intersperse | Allow interspersion of options and arguments? |
-> (String -> Either InputError a) | How to parse positional arguments |
-> IO [a] | If there is an error parsing the command line, the program will exit with an error message. If successful the results are returned here. |
A parser for simple command lines that do not contain modes. Runs in the IO monad.
Arguments
:: (ProgName -> String) | Indicate the help here. This function, when applied to the name
of the program, returns help. simpleHelp automatically adds
options for |
-> [OptSpec a] | Options to parse |
-> Intersperse | Allow interspersion of options and positional arguments? |
-> (String -> Either InputError a) | How to parse positional arguments |
-> IO [a] | If the parser fails, the program will exit with an error. If the user requested help, it will be displayed and the program exits successfully. Otherwise, the options and positional arguments are returned here. |
A parser for simple command lines. Adds a --help
option for
you.
Arguments
:: (ProgName -> String) | Indicate the help here. This function, when applied to the name
of the program, returns help. simpleHelpVersion automatically adds
options for |
-> (ProgName -> String) | Indicate the version here. This function, when applied to the
name of the program, returns a version string. simpleHelpVersion
automatically adds an option for |
-> [OptSpec a] | Options to parse |
-> Intersperse | Allow interspersion of options and positional arguments? |
-> (String -> Either InputError a) | How to parse positional arguments |
-> IO [a] | If the parser fails, the program will exit with an error. If the user requested help or version information, it will be displayed and the program exits successfully. Otherwise, the options and positional arguments are returned here. |
A parser for simple command lines without modes. Adds options
for --help
and --version
for you.
Mode parsers
Arguments
:: Opts s g | Global options. These are specified before any mode. For
instance, in the command |
-> ([g] -> Either String (Either r [Mode s r])) | This function processes the global options. If there are no
shortcut options specified in the global options, it is applied
to the result of processing the global options. This function
may return a Left if there is something wrong with the
global options (a nonsensical combination, perhaps.) Otherwise,
it returns a |
-> [String] | Command line arguments to parse |
-> Either Error (Either s r) | If the command line arguments fail to parse, this will be a
Left with the error. If the parser is successful, this
returns a |
A pure (non-IO) parser for command lines that contain modes.
Arguments
:: Opts s g | Specifies global options and global shortcut options |
-> ([g] -> Either String (Either r [Mode s r])) | This function processes the global options. If there are no
shortcut options specified in the global options, it is applied
to the result of processing the global options. This function
may return a Left if there is something wrong with the
global options (a nonsensical combination, perhaps.) Otherwise,
it returns a |
-> IO (Either s r) | If parsing fails, the program will exit with a failure. If successful, the result is returned here. A Left indicates a shortcut option, either from the global options or from the mode-specific options; a Right indicates the mode a user selected. |
A command line parser for multi-mode command lines. Runs in the IO monad.
Helpers to create various options and modes
Creates an Opts with a help shortcut option.
Arguments
:: h | What you wish to use for help |
-> h | What you wish to use for version |
-> [OptSpec a] | |
-> Opts h a |
Creates an Opts with help and version shortcut options.
Arguments
:: String | Mode name |
-> h | Whatever you want to use for the help (perhaps a string, or a function, or an IO action). Its type will have to match up with the type of the global shortcut options and with the shortcut type of the other modes. |
-> ([a] -> r) | When applied to the the mode options, returns the result. |
-> [OptSpec a] | Options for this mode |
-> Intersperse | Allow interspersion of mode options and positional arguments? |
-> (String -> Either InputError a) | Parses positional arguments |
-> Mode h r |
Creates a Mode with a help option (help specific to the mode.)