ContentsIndex
GetOptions
Contents
Functions
Option Parsing
Option Augmentation
Types and Classes
Description
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.
Synopsis
getOptions :: Option a b => b -> IO ([String], a)
parseOptions :: (Option a b, Monad m) => b -> [String] -> (String, m ([String], a))
(==>) :: a -> b -> HasDefault a b
(??) :: a -> String -> HasHelp a
noHelp :: a -> NoHelp a
class Option a b
class (Show a, Read a) => ArgOption a
data HasDefault a b
data HasHelp a
data NoHelp a
Functions
Option Parsing
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.

for instance

 (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.

For example

 (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.

parseOptions
:: (Option a b, Monad m)
=> bThe option parameters
-> [String]The raw options from the command line
-> (String, m ([String], a))(help text,(non-option args, results) or fail if error found)
Underlying option parser
Option Augmentation
(==>)
:: aargument
-> bdefault value
-> HasDefault a b
Specify a default value if an argument is not found.
(??)
:: aargument
-> Stringhelp message
-> HasHelp a
Specify a help message.
noHelp :: a -> NoHelp a
Cause an option to not occur in the help message.
Types and Classes
class Option a b
show/hide Instances
Option Bool String
(Option a c, Option b d) => Option (a, b) (c, d)
(Option a1 b1, Option a2 b2, Option a3 b3) => Option (a1, a2, a3) (b1, b2, b3)
(Option a1 b1, Option a2 b2, Option a3 b3, Option a4 b4) => Option (a1, a2, a3, a4) (b1, b2, b3, b4)
(Option a1 b1, Option a2 b2, Option a3 b3, Option a4 b4, Option a5 b5) => Option (a1, a2, a3, a4, a5) (b1, b2, b3, b4, b5)
(Option a1 b1, Option a2 b2, Option a3 b3, Option a4 b4, Option a5 b5, Option a6 b6) => Option (a1, a2, a3, a4, a5, a6) (b1, b2, b3, b4, b5, b6)
(Option a1 b1, Option a2 b2, Option a3 b3, Option a4 b4, Option a5 b5, Option a6 b6, Option a7 b7) => Option (a1, a2, a3, a4, a5, a6, a7) (b1, b2, b3, b4, b5, b6, b7)
(Option a1 b1, Option a2 b2, Option a3 b3, Option a4 b4, Option a5 b5, Option a6 b6, Option a7 b7, Option a8 b8) => Option (a1, a2, a3, a4, a5, a6, a7, a8) (b1, b2, b3, b4, b5, b6, b7, b8)
(Option a1 b1, Option a2 b2, Option a3 b3, Option a4 b4, Option a5 b5, Option a6 b6, Option a7 b7, Option a8 b8, Option a9 b9) => Option (a1, a2, a3, a4, a5, a6, a7, a8, a9) (b1, b2, b3, b4, b5, b6, b7, b8, b9)
(Option a1 b1, Option a2 b2, Option a3 b3, Option a4 b4, Option a5 b5, Option a6 b6, Option a7 b7, Option a8 b8, Option a9 b9, Option a10 b10) => Option (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10)
(Option a1 b1, Option a2 b2, Option a3 b3, Option a4 b4, Option a5 b5, Option a6 b6, Option a7 b7, Option a8 b8, Option a9 b9, Option a10 b10, Option a11 b11) => Option (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11)
(Option a1 b1, Option a2 b2, Option a3 b3, Option a4 b4, Option a5 b5, Option a6 b6, Option a7 b7, Option a8 b8, Option a9 b9, Option a10 b10, Option a11 b11, Option a12 b12) => Option (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12)
(Option a1 b1, Option a2 b2, Option a3 b3, Option a4 b4, Option a5 b5, Option a6 b6, Option a7 b7, Option a8 b8, Option a9 b9, Option a10 b10, Option a11 b11, Option a12 b12, Option a13 b13) => Option (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13)
(Option a1 b1, Option a2 b2, Option a3 b3, Option a4 b4, Option a5 b5, Option a6 b6, Option a7 b7, Option a8 b8, Option a9 b9, Option a10 b10, Option a11 b11, Option a12 b12, Option a13 b13, Option a14 b14) => Option (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14)
(Option a1 b1, Option a2 b2, Option a3 b3, Option a4 b4, Option a5 b5, Option a6 b6, Option a7 b7, Option a8 b8, Option a9 b9, Option a10 b10, Option a11 b11, Option a12 b12, Option a13 b13, Option a14 b14, Option a15 b15) => Option (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15) (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15)
Option a b => Option a (HasHelp b)
Option a b => Option a (NoHelp b)
ArgOption a => Option a (HasDefault String a)
ArgOption a => Option (Maybe a) String
ArgOption a => Option [a] String
class (Show a, Read a) => ArgOption a
show/hide Instances
ArgOption Double
ArgOption Float
ArgOption Int
ArgOption Integer
ArgOption String
(ArgOption a, ArgOption b) => ArgOption (Either a b)
data HasDefault a b
Type of arguments with a default value.
show/hide Instances
ArgOption a => Option a (HasDefault String a)
(Show a, Show b) => Show (HasDefault a b)
data HasHelp a
Type of arguments with a user supplied help message.
show/hide Instances
Option a b => Option a (HasHelp b)
Show a => Show (HasHelp a)
data NoHelp a
show/hide Instances
Option a b => Option a (NoHelp b)
Produced by Haddock version 0.8