|This module provides an advanced option parsing routine which can properly parse options depending on what types are infered for them as well as produce a pretty error message with usage info when an incorrect option is used.|
|getOptions :: Option a b => b -> IO ([String], a)|
Main Entry point. This retrieves the arguments passed to the program via getArgs and runs parseOptions on it. In addition, it handles errors by printing a help message and exiting as well as creating a --help option to force a printing of the help table.
The basic idea is that there is a matching between the data passed into getOptions and what is returned. In the examples below, I will include type information in comments for reference. This is not needed by the compiler however, as the proper types are infered.
(args,verb) <- getOptions "v|verbose" -- verb :: Bool
will cause verb == True if and only if --v or --verbose are on the command line.
It is also possible for options to take arguments:
(args,(verb,output_name)) <- getOptions ("v|verbose", "o") -- verb :: Bool, output_name :: Maybe String
will set output_name == Just "foo" iff -o foo is one of the arguments. (and set verb as above)
In addition to strings, arguments may be many other basic types, such as Ints, Doubles, or even Either types. If the argument passed on the command line does not match the type needed, an appropriate error message will be generated.
Note that multiple options are possible via tuples, which may also be nested. (useful for libraries)
(args,(verb,output_name)) <- getOptions ( "v|verbose" ,"o" ==> "out.txt" ) -- verb :: Bool, output_name :: String
Default options may also be specified with the ==> operator. Notice the type of output_name is now String rather than Maybe String.
Help messages may be specified with the ?? operator.
(as,(output_name,verb,count,xs)) <- getOptions ( "o" ==> "out.txt" ?? "Specify output file" ,"v|verbose" ?? "Enable verbose mode." ,"count" ==> (1::Int) ?? "How many iterations" ,"elem|e" ?? "A list of elements" )
produces the following help table
-o out.txt Specify output file -v, --verbose Enable verbose mode. --count 1 How many iterations --elem, -e ARG A list of elements --help Display usage information
If a list of items is returned, the option is allowed to occur multiple times and all its arguments are collected in the list.
|noHelp :: a -> NoHelp a|
|Cause an option to not occur in the help message.|
|Types and Classes|
|class Option a b|
|class (Show a, Read a) => ArgOption a|
|data HasDefault a b|
|data HasHelp a|
|data NoHelp a|
|Produced by Haddock version 0.8|