Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Parse command lines with options that might take multiple option arguments.
I built this library could not find anything that would readily
parse command lines where the options took more than one
option argument. For example, for the tail
command on GNU systems, the
--lines
option takes one option argument to specify how many
lines you want to see. Well, what if you want to build a program
with an option that takes two option arguments, like --foo bar
baz
? I found no such library so I built this one.
Please consult the Multiarg.Vocabulary module to learn common vocabulary used throughout Multiarg and its documentation. Words that appear in italics are defined in Multiarg.Vocabulary.
Use this module to build parsers for simple commands. The
parseCommandLine
function runs in the IO monad and will cause
your program to exit unsuccessfully if there are any errors in the
command line, printing an error message in the process. If you
want more control over error handling, use the Multiarg.Internal
module.
To write parsers for commands with multiple modes (for instance,
ghc-pkg
has multiple modes, such as ghc-pkg list
and ghc-pkg
check
) use the Multiarg.Mode module.
You will find examples in Multiarg.Examples.Telly for non-mode parsers, and in Multiarg.Examples.Grover for mode parsers.
Documentation
Specifies how many option arguments an option takes.
Specifies an option. Typically you will use optSpec
to
create an OptSpec
rather than using the constructor directly.
Each OptSpec
may contain mulitple short option names and
long option names; but each OptSpec
contains only one ArgSpec
.
Therefore, all short option names and long option names
specified in a single OptSpec
are synonymous.
:: [Char] | There is one character for each desired short option name.
Each of these characters may not be a hyphen; otherwise,
|
-> [String] | There is one string for each desired long option name. Each string:
|
-> ArgSpec a | How many option arguments this option takes. This also specifies what is returned when the option is parsed on the command line. |
-> OptSpec a |
Creates an OptSpec
.
:: (String -> String) | Returns help for your command. This function is applied to the
name of the program being run, which is obtained from
|
-> [OptSpec a] | All program options. An option for |
-> (String -> a) | Processes positional arguments. |
-> IO [a] | Fetches the words from the command line arguments using
|
Parses a command line. Runs in the IO monad so that it can do some tedious things for you:
- fetches the words on command line using
getArgs
and the name of the program withgetProgName
- prints help, if the user requested help, and exits successfully
- prints an error message and exits unsuccessfully, if the user entered a bad command line (such as an unknown option)
If you don't want this degree of automation or if you want a pure
function, see the parseCommandLinePure
function in the
Multiarg.Internal module.