getopt-simple- A "System.Console.GetOpt" wrapper to make simple use case easy.

Safe HaskellSafe-Infered




There's no need to invoke full getOpt power in everyday use. So, here it is a most common use case implemented to be as painless as possible while retaining some functionality. It's divided into three layers, each built upon another. You can start at highest and peel of layers to gradually unlock more getOpt features.


Low level: using [OptDescr] with some helpers.

type Options = Map String StringSource

A result of all those musings is a plain string-to-string dictionary.

type Flag = (String, String)Source

An option is a string-tagged string value to be processed with Data.Map functions.

ArgDescr helpers

type FlagMaker = String -> ArgDescr FlagSource

We are using a no-processing, grab and run away ArgDesc. All options' arguments are required.

arg :: FlagMakerSource

Make an option with a value. Option argument value will be captured and “tagged” with an option name.

 arg "user"

noArg :: FlagMakerSource

Make a valueless option. The value captured will be an empty string.

 noArg "debug"

A generic «usage» message which prefixes getOpt's option dump.

showUsage :: [String] -> [String] -> IO ()Source

Generate and show usage example using getProgName and lists of required options and arguments.

Extract and check values.

type FlagDescr = OptDescr FlagSource

An option list which will result in a assoc list of options.

processOpts :: [FlagDescr] -> [String] -> [String] -> [String] -> IO (Options, [String])Source

Run getOpt against option list. Show errors and usage notice if something goes wrong.

getOptsArgs :: [FlagDescr] -> [String] -> [String] -> IO (Options, [String])Source

Extract values and validate against lists of mandatory arguments and options. This adds a default '-h/--help' option.

Low level uses getOpt's facilities to prepare option list and offers maximum flexibility:

 options = [ Option ['v'] ["verbose", "debug"] (noArg "debug")   "Dump all the stuff flying."
           , Option ['d'] ["date"]             (arg   "date")    "Report date."
           , Option ['c'] ["conf"]             (arg   "conf")    "Configuration file."
           , Option ['s'] ["section"]          (arg   "section") "Configuration section."

 (opts, args) <- getOptsArgs options ["conf", "section"] ["command"]

Intermediate: drop getOpts chains.

data Mode Source

A validation flag. Ignored when building a option list but stored for future reference.


type Conf = [(FlagMaker, String, Mode, String)]Source

Configuration type used to construct Option list and, later, check for mandatory options and arguments.

makeOptions :: Conf -> [FlagDescr]Source

Process a Configuration into a list of getOpt options. Required options and arguments are enforced, but defaults aren't being into a result.

 let options = makeOptions [ (noArg, "verbose", Optional,   "Dump all the stuff flying.")
                           , (arg,   "date",    Required,   "Report date.")]
                           , (arg,   "conf",    Required,   "Configuration file.")
                           , (arg,   "section", Default "", "Configuration section.")

 (opts, args) <- getOptsArgs (makeOptions options) ["conf", "section"] ["command"]

option :: FlagMaker -> String -> String -> FlagDescrSource

Construct an Option from a less verbose list of values.

Easy-mode using Configuration.

getUsingConf :: Conf -> [String] -> IO (Options, [String])Source


>>> (opts, args) <- getUsingConf options ["command"]
>>> print opts
fromList [("date", "2012-08-23"), ("conf", "/usr/local/etc/service.conf"), ("section", "")]

fromConf :: Conf -> ([FlagDescr], [String], [(String, String)])Source

Process configuration into function arguments.