Portability | unknown |
---|---|
Stability | experimental |
Maintainer | aenor.realm@gmail.com |
Safe Haskell | Safe-Inferred |
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.
- type Options = Map String String
- type Flag = (String, String)
- type FlagMaker = String -> ArgDescr Flag
- arg :: FlagMaker
- noArg :: FlagMaker
- showUsage :: [String] -> [String] -> IO ()
- type FlagDescr = OptDescr Flag
- processOpts :: [FlagDescr] -> [String] -> [String] -> [String] -> IO (Options, [String])
- getOptsArgs :: [FlagDescr] -> [String] -> [String] -> IO (Options, [String])
- data Mode
- type Conf = [(FlagMaker, String, Mode, String)]
- makeOptions :: Conf -> [FlagDescr]
- option :: FlagMaker -> String -> String -> FlagDescr
- getUsingConf :: Conf -> [String] -> IO (Options, [String])
- fromConf :: Conf -> ([FlagDescr], [String], [(String, String)])
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.
Make an option with a value. Option argument value will be captured and “tagged” with an option name.
arg "user"
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.
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.
A validation flag. Ignored when building a option list but stored for future reference.
type Conf = [(FlagMaker, String, Mode, String)]Source
Conf
iguration type used to construct Option list and, later, check for mandatory options and arguments.
makeOptions :: Conf -> [FlagDescr]Source
Process a Conf
iguration 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.