HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulecmdargs-0.10.22Haskell2010

System.Console.CmdArgs.Explicit

This module constructs command lines. You may either use the helper functions (flagNone, flagOpt, mode etc.) or construct the type directly. These types are intended to give all the necessary power to the person constructing a command line parser.

For people constructing simpler command line parsers, the module System.Console.CmdArgs.Implicit may be more appropriate.

As an example of a parser:

   arguments :: Mode [(String,String)]
   arguments = mode "explicit" [] "Explicit sample program" (flagArg (upd "file") "FILE")
       [flagOpt "world" ["hello","h"] (upd "world") "WHO" "World argument"
       ,flagReq ["greeting","g"] (upd "greeting") "MSG" "Greeting to give"
       ,flagHelpSimple (("help",""):)]
       where upd msg x v = Right $ (msg,x):v
   

And this can be invoked by:

   main = do
       xs <- processArgs arguments
       if ("help","") `elem` xs then
           print $ helpText [] HelpFormatDefault arguments
        else
           print xs
   

Groups: The Group structure allows flags/modes to be grouped for the purpose of displaying help. When processing command lines, the group structure is ignored.

Modes: The Explicit module allows multiple mode programs by placing additional modes in modeGroupModes. Every mode is allowed sub-modes, and thus multiple levels of mode may be created. Given a mode x with sub-modes xs, if the first argument corresponds to the name of a sub-mode, then that sub-mode will be applied. If not, then the arguments will be processed by mode x. Consequently, if you wish to force the user to explicitly enter a mode, simply give sub-modes, and leave modeArgs as Nothing. Alternatively, if you want one sub-mode to be selected by default, place all it's flags both in the sub-mode and the outer mode.

Parsing rules: Command lines are parsed as per most GNU programs. Short arguments single letter flags start with -, longer flags start with --, and everything else is considered an argument. Anything after -- alone is considered to be an argument. For example:

-f --flag argument1 -- --argument2

This command line passes one single letter flag (f), one longer flag (flag) and two arguments (argument1 and --argument2).

  • 11 types
  • 1 class
  • 31 values
  • Packagecmdargs-0.10.22
  • Exports43
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceExplicit.hs

Running command lines

4 declarations
valueprocess :: Mode a -> [String] -> Either String a
#

Process a list of flags (usually obtained from getArgs/expandArgsAt) with a mode. Returns Left and an error message if the command line fails to parse, or Right and the associated value.

valueprocessArgs :: Mode a -> IO a
#

Process the flags obtained by getArgs and expandArgsAt with a mode. Displays an error and exits with failure if the command line fails to parse, or returns the associated value. Implemented in terms of process. This function makes use of the following environment variables:

  • $CMDARGS_COMPLETE - causes the program to produce completions using complete, then exit. Completions are based on the result of getArgs, the index of the current argument is taken from $CMDARGS_COMPLETE (set it to - to complete the last argument), and the index within that argument is taken from $CMDARGS_COMPLETE_POS (if set).

  • $CMDARGS_HELPER/$CMDARGS_HELPER_PROG - uses the helper mechanism for entering command line programs as described in System.Console.CmdArgs.Helper.

valueprocessValue :: Mode a -> [String] -> a
#

Process a list of flags (usually obtained from getArgs and expandArgsAt) with a mode. Throws an error if the command line fails to parse, or returns the associated value. Implemeneted in terms of process. This function does not take account of any environment variables that may be set (see processArgs).

If you are in IO you will probably get a better user experience by calling processValueIO.

Constructing command lines

32 declarations
typetype Help = String
#

A help message that goes with either a flag or a mode.

valuemodes :: String -> a -> Help -> [Mode a] -> Mode a
#

Create a list of modes, with a program name, an initial value, some help text and the child modes.

datadata Mode a
#

A mode. Do not use the Mode constructor directly, instead use mode to construct the Mode and then record updates. Each mode has three main features:

To produce the help information for a mode, either use helpText or show.

Constructors

Instances3Remap, Show, Packer
  • Remap ModeDefined in cmdargs-0.10.22 · System.Console.CmdArgs.Explicit.Type
  • Show (Mode a)Defined in cmdargs-0.10.22 · System.Console.CmdArgs.Explicit.Help · orphan
  • Packer a => Packer (Mode a)Defined in cmdargs-0.10.22 · System.Console.CmdArgs.Helper
classclass Remap (m :: Type -> Type) where
#

Like functor, but where the the argument isn't just covariant.

Methods

  • remap :: (a -> b) -> (b -> (a, a -> b)) -> m a -> m b

    Convert between two values.

Instances3Remap
  • Remap ArgDefined in cmdargs-0.10.22 · System.Console.CmdArgs.Explicit.Type
  • Remap FlagDefined in cmdargs-0.10.22 · System.Console.CmdArgs.Explicit.Type
  • Remap ModeDefined in cmdargs-0.10.22 · System.Console.CmdArgs.Explicit.Type
datadata Arg a
#

An unnamed argument. Anything not starting with - is considered an argument, apart from "-" which is considered to be the argument "-", and any arguments following "--". For example:

programname arg1 -j - --foo arg3 -- -arg4 --arg5=1 arg6

Would have the arguments:

["arg1","-","arg3","-arg4","--arg5=1","arg6"]

Constructors

  • Arg
    • argValue :: Update a

      A way of processing the argument.

    • argType :: FlagHelp

      The type of data for the argument, i.e. FILE/DIR/EXT

    • argRequire :: Bool

      Is at least one of these arguments required, the command line will fail if none are set

Instances3Remap, Show, Packer
  • Remap ArgDefined in cmdargs-0.10.22 · System.Console.CmdArgs.Explicit.Type
  • Show (Arg a)Defined in cmdargs-0.10.22 · System.Console.CmdArgs.Explicit.Help · orphan
  • Packer a => Packer (Arg a)Defined in cmdargs-0.10.22 · System.Console.CmdArgs.Helper
typetype Name = String
#

A name, either the name of a flag (--foo) or the name of a mode.

datadata Group a
#

A group of items (modes or flags). The items are treated as a list, but the group structure is used when displaying the help message.

Constructors

Instances5Functor, Show, Semigroup, Monoid, Packer
  • Functor GroupDefined in cmdargs-0.10.22 · System.Console.CmdArgs.Explicit.Type
  • Show a => Show (Group a)Defined in cmdargs-0.10.22 · System.Console.CmdArgs.Explicit.Type
  • Semigroup (Group a)Defined in cmdargs-0.10.22 · System.Console.CmdArgs.Explicit.Type
  • Monoid (Group a)Defined in cmdargs-0.10.22 · System.Console.CmdArgs.Explicit.Type
  • Packer a => Packer (Group a)Defined in cmdargs-0.10.22 · System.Console.CmdArgs.Helper
datadata Flag a
#

A flag, consisting of a list of flag names and other information.

Constructors

Instances3Remap, Show, Packer
  • Remap FlagDefined in cmdargs-0.10.22 · System.Console.CmdArgs.Explicit.Type
  • Show (Flag a)Defined in cmdargs-0.10.22 · System.Console.CmdArgs.Explicit.Help · orphan
  • Packer a => Packer (Flag a)Defined in cmdargs-0.10.22 · System.Console.CmdArgs.Helper
datadata FlagInfo
#

The FlagInfo type has the following meaning:

             FlagReq     FlagOpt      FlagOptRare/FlagNone
-xfoo        -x=foo      -x=foo       -x -foo
-x foo       -x=foo      -x foo       -x foo
-x=foo       -x=foo      -x=foo       -x=foo
--xx foo     --xx=foo    --xx foo     --xx foo
--xx=foo     --xx=foo    --xx=foo     --xx=foo

Constructors

Instances4Eq, Ord, Show, Packer
  • Eq FlagInfoDefined in cmdargs-0.10.22 · System.Console.CmdArgs.Explicit.Type
  • Ord FlagInfoDefined in cmdargs-0.10.22 · System.Console.CmdArgs.Explicit.Type
  • Show FlagInfoDefined in cmdargs-0.10.22 · System.Console.CmdArgs.Explicit.Type
  • Packer FlagInfoDefined in cmdargs-0.10.22 · System.Console.CmdArgs.Helper
typetype Update a = String -> a -> Either String a
#

A function to take a string, and a value, and either produce an error message (Left), or a modified value (Right).

valueremap2 :: Remap m => (a -> b) -> (b -> a) -> m a -> m b
#

Restricted version of remap where the values are isomorphic.

valuemodeEmpty :: a -> Mode a
#

Create an empty mode specifying only modeValue. All other fields will usually be populated using record updates.

valuemode :: Name -> a -> Help -> Arg a -> [Flag a] -> Mode a
#

Create a mode with a name, an initial value, some help text, a way of processing arguments and a list of flags.

valueflagNone :: [Name] -> (a -> a) -> Help -> Flag a
#

Create a flag taking no argument value, with a list of flag names, an update function and some help text.

valueflagOpt :: String -> [Name] -> Update a -> FlagHelp -> Help -> Flag a
#

Create a flag taking an optional argument value, with an optional value, a list of flag names, an update function, the type of the argument and some help text.

valueflagReq :: [Name] -> Update a -> FlagHelp -> Help -> Flag a
#

Create a flag taking a required argument value, with a list of flag names, an update function, the type of the argument and some help text.

valueflagArg :: Update a -> FlagHelp -> Arg a
#

Create an argument flag, with an update function and the type of the argument.

valueflagBool :: [Name] -> (Bool -> a -> a) -> Help -> Flag a
#

Create a boolean flag, with a list of flag names, an update function and some help text.

valueflagHelpFormat :: (HelpFormat -> TextFormat -> a -> a) -> Flag a
#

Create a help flag triggered by -?/--help. The user may optionally modify help by specifying the format, such as:

--help=all          - help for all modes
--help=html         - help in HTML format
--help=100          - wrap the text at 100 characters
--help=100,one      - full text wrapped at 100 characters
valueflagVersion :: (a -> a) -> Flag a
#

Create a version flag triggered by -V/--version.

Displaying help

2 declarations
datadata HelpFormat
#

Specify the format to output the help.

Constructors

Instances7Bounded, Enum, Eq, Ord, Read, Show, …
  • Bounded HelpFormatDefined in cmdargs-0.10.22 · System.Console.CmdArgs.Explicit.Help
  • Enum HelpFormatDefined in cmdargs-0.10.22 · System.Console.CmdArgs.Explicit.Help
  • Eq HelpFormatDefined in cmdargs-0.10.22 · System.Console.CmdArgs.Explicit.Help
  • Ord HelpFormatDefined in cmdargs-0.10.22 · System.Console.CmdArgs.Explicit.Help
  • Read HelpFormatDefined in cmdargs-0.10.22 · System.Console.CmdArgs.Explicit.Help
  • Show HelpFormatDefined in cmdargs-0.10.22 · System.Console.CmdArgs.Explicit.Help
  • Default HelpFormatDefined in cmdargs-0.10.22 · System.Console.CmdArgs.Explicit.Help

Utilities for working with command lines

5 declarations
valueexpandArgsAt :: [String] -> IO [String]
#

Expand @ directives in a list of arguments, usually obtained from getArgs. As an example, given the file test.txt with the lines hello and world:

expandArgsAt ["@test.txt","!"] == ["hello","world","!"]

Any @ directives in the files will be recursively expanded (raising an error if there is infinite recursion).

To supress @ expansion, pass any @ arguments after --.

valuejoinArgs :: [String] -> String
#

Given a sequence of arguments, join them together in a manner that could be used on the command line, giving preference to the Windows cmd shell quoting conventions.

For an alternative version, intended for actual running the result in a shell, see "System.Process.showCommandForUser"

datadata Complete
#

How to complete a command line option. The Show instance is suitable for parsing from shell scripts.

Constructors

Instances3Eq, Ord, Show
  • Eq CompleteDefined in cmdargs-0.10.22 · System.Console.CmdArgs.Explicit.Complete
  • Ord CompleteDefined in cmdargs-0.10.22 · System.Console.CmdArgs.Explicit.Complete
  • Show CompleteDefined in cmdargs-0.10.22 · System.Console.CmdArgs.Explicit.Complete
valuecomplete
  1. :: Mode a

    Mode specifying which arguments are allowed

  2. -> [String]

    Arguments the user has already typed

  3. -> (Int, Int)

    0-based index of the argument they are currently on, and the position in that argument

  4. -> [Complete]
#

Given a current state, return the set of commands you could type now, in preference order.