Safe Haskell | Safe-Inferred |
---|
Combinators that are useful for building command-line parsers. These build off the functions in System.Console.MultiArg.Prim. Unlike those functions, these functions have no access to the internals of the parser.
- notFollowedBy :: Parser a -> Parser ()
- data OptSpec a = OptSpec {}
- data OptArgError
- reader :: Read a => String -> Exceptional OptArgError a
- optReader :: Read a => Maybe String -> Exceptional OptArgError (Maybe a)
- data ArgSpec a
- = NoArg a
- | OptionalArg (Maybe String -> a)
- | OneArg (String -> a)
- | TwoArg (String -> String -> a)
- | ThreeArg (String -> String -> String -> a)
- | VariableArg ([String] -> a)
- | ChoiceArg [(String, a)]
- | OptionalArgE (Maybe String -> Exceptional OptArgError a)
- | OneArgE (String -> Exceptional OptArgError a)
- | TwoArgE (String -> String -> Exceptional OptArgError a)
- | ThreeArgE (String -> String -> String -> Exceptional OptArgError a)
- | VariableArgE ([String] -> Exceptional OptArgError a)
- parseOption :: [OptSpec a] -> Parser a
- formatError :: String -> Error -> String
Parser combinators
notFollowedBy :: Parser a -> Parser ()Source
notFollowedBy p
succeeds only if parser p fails. If p fails,
notFollowedBy succeeds without consuming any input. If p succeeds
and consumes input, notFollowedBy fails and consumes input. If p
succeeds and does not consume any input, notFollowedBy fails and
does not consume any input.
Combined long and short option parser
Specifies options for the parseOption
function. Each OptSpec
represents one command-line option.
OptSpec | |
|
data OptArgError Source
Indicates errors when parsing options to arguments.
NoMsg | No error message accompanies this failure. multiarg will create a generic error message for you. |
ErrorMsg String | Parsing the argument failed with this error message. An example
might be |
reader :: Read a => String -> Exceptional OptArgError aSource
Reads in values that are members of Read. Provides a generic error message if the read fails.
optReader :: Read a => Maybe String -> Exceptional OptArgError (Maybe a)Source
Reads in values that are members of Read, but the value does not have to appear on the command line. Provides a generic error message if the read fails. If the argument is Nothing, returns Nothing.
Specifies how many arguments each option takes. As with
ArgDescr
, there are (at least) two ways to
use this type. You can simply represent each possible option using
different data constructors in an algebraic data type. Or you can
have each ArgSpec yield a function that transforms a record. For an
example that uses an algebraic data type, see
System.Console.MultiArg.SampleParser.
Some of these value constructors have names ending in E
. Use
these constructors when you want to parse option arguments that may
fail to parse--for example, you want to parse an Int. The function
passed as an argument to the value constructor indicates failure by
returing an Exception
.
NoArg a | This option takes no arguments |
OptionalArg (Maybe String -> a) | This option takes an optional argument. As noted in "The Tao
of Option Parsing", optional arguments can result in some
ambiguity. (Read it here:
http://optik.sourceforge.net/doc/1.5/tao.html) If option |
OneArg (String -> a) | This option takes one argument. Here, if option |
TwoArg (String -> String -> a) | This option takes two arguments. Parsed similarly to
|
ThreeArg (String -> String -> String -> a) | This option takes three arguments. Parsed similarly to
|
VariableArg ([String] -> a) | This option takes a variable number of arguments--zero or
more. Option arguments continue until the command line contains
a word that begins with a hyphen. For example, if option |
ChoiceArg [(String, a)] | This option takes a single argument, which must match one of
the strings given in the list. The user may supply the shortest
unambiguous string. If the argument list to ChoiceArg has
duplicate strings, only the first string is used. For instance,
ChoiceArg could be useful if you were parsing the |
OptionalArgE (Maybe String -> Exceptional OptArgError a) | This option takes an optional argument, like
|
OneArgE (String -> Exceptional OptArgError a) | This option takes a single argument, like |
TwoArgE (String -> String -> Exceptional OptArgError a) | This option takes two arguments, like |
ThreeArgE (String -> String -> String -> Exceptional OptArgError a) | This option takes three arguments, like |
VariableArgE ([String] -> Exceptional OptArgError a) | This option takes a variable number of arguments, like
|
parseOption :: [OptSpec a] -> Parser aSource
Parses a single command line option. Examines all the options
specified using multiple OptSpec and parses one option on the
command line accordingly. Fails without consuming any input if the
next word on the command line is not a recognized option. Allows
the user to specify the shortest unambiguous match for long
options; for example, the user could type --verb
for --verbose
and --vers
for --version
.
This function is applied to a list of OptSpec, rather than to a
single OptSpec, because in order to correctly handle the parsing of
shortened long options (e.g. --verb
rather than --verbose
) it
is necessary for one function to have access to all of the
OptSpec. Applying this function multiple times to different lists
of OptSpec and then using the |
function to combine them will
break the proper parsing of shortened long options.
For an example that uses this function, see System.Console.MultiArg.SimpleParser.