Command -package:hedgehog package:core-program
Description of the command-line structure of a program which has
"commands" (sometimes referred to as "subcommands") representing
different modes of operation. This is familiar from tools like
git and docker.
Different ways parsing a simple or complex command-line can fail.
In a complex configuration, user didn't specify a command.
In a complex configuration, user specified a command that doesn't
match any in the configuration.
Given a program configuration schema and the command-line arguments,
process them into key/value pairs in a Parameters object.
This results in
InvalidCommandLine on the left side if one of
the passed in options is unrecognized or if there is some other
problem handling options or arguments (because at that point, we want
to rabbit right back to the top and bail out; there's no recovering).
This isn't something you'll ever need to call directly; it's exposed
for testing convenience. This function is invoked when you call
configure or
execute (which calls
configure with
a default
Config when initializing).
Retrieve the values of parameters parsed from options and arguments
supplied by the user on the command-line.
The command-line parameters are returned in a
Map, mapping from
from the option or argument name to the supplied value. You can query
this map directly:
program = do
params <- getCommandLine
let result = lookupKeyValue "silence" (paramterValuesFrom params)
case result of
Nothing -> return ()
Just quiet = case quiet of
Value _ -> throw NotQuiteRight -- complain that flag doesn't take value
Empty -> write "You should be quiet now" -- much better
...
which is pattern matching to answer "was this option specified by the
user?" or "what was the value of this [mandatory] argument?", and then
"if so, did the parameter have a value?"
This is available should you need to differentiate between a
Value and an
Empty ParameterValue, but for
many cases as a convenience you can use the
queryOptionFlag,
queryOptionValue, and
queryArgument functions below.
Retreive the sub-command mode selected by the user. This assumes your
program was set up to take sub-commands via
complexConfig.
mode <- queryCommandName