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

Moduleoptparse-applicative-0.18.1.0Haskell98

Options.Applicative.Builder

  • 9 types
  • 4 classes
  • 60 values

Parser builders

10 declarations

This module contains utility functions and combinators to create parsers for individual options.

Each parser builder takes an option modifier. A modifier can be created by composing the basic modifiers provided by this module using the Monoid operations mempty and mappend, or their aliases idm and <>.

For example:

out = strOption
    ( long "output"
   <> short 'o'
   <> metavar "FILENAME" )

creates a parser for an option called "output".

valuesubparser :: Mod CommandFields a -> Parser a
#

Builder for a command parser. The command modifier can be used to specify individual commands.

By default, sub-parsers allow backtracking to their parent's options when they are completed. To allow full mixing of parent and sub-parser options, turn on subparserInline; otherwise, to disable backtracking completely, use noBacktrack.

valueflag
  1. :: a

    default value

  2. -> a

    active value

  3. -> Mod FlagFields a

    option modifier

  4. -> Parser a
#

Builder for a flag parser.

A flag that switches from a "default value" to an "active value" when encountered. For a simple boolean value, use switch instead.

Note: Because this parser will never fail, it can not be used with combinators such as some or many, as these combinators continue until a failure occurs. See flag'.

valueflag'
  1. :: a

    active value

  2. -> Mod FlagFields a

    option modifier

  3. -> Parser a
#

Builder for a flag parser without a default value.

Same as flag, but with no default value. In particular, this flag will never parse successfully by itself.

It still makes sense to use it as part of a composite parser. For example

length <$> many (flag' () (short 't'))

is a parser that counts the number of "-t" arguments on the command line, alternatively

flag' True (long "on") <|> flag' False (long "off")

will require the user to enter '--on' or '--off' on the command line.

Builder for a boolean flag.

Note: Because this parser will never fail, it can not be used with combinators such as some or many, as these combinators continue until a failure occurs. See flag'.

switch = flag False True
valueabortOption :: ParseError -> Mod OptionFields (a -> a) -> Parser (a -> a)
#

An option that always fails.

When this option is encountered, the option parser immediately aborts with the given parse error. If you simply want to output a message, use infoOption instead.

valueoption :: ReadM a -> Mod OptionFields a -> Parser a
#

Builder for an option using the given reader.

This is a regular option, and should always have either a long or short name specified in the modifiers (or both).

nameParser = option str ( long "name" <> short 'n' )

Modifiers

20 declarations
valuehelp :: String -> Mod f a
#

Specify the help text for an option.

valuehelpDoc :: Maybe Doc -> Mod f a
#

Specify the help text for an option as a 'Prettyprinter.Doc AnsiStyle' value.

valuevalue :: HasValue f => a -> Mod f a
#

Specify a default value for an option.

Note: Because this modifier means the parser will never fail, do not use it with combinators such as some or many, as these combinators continue until a failure occurs. Careless use will thus result in a hang.

To display the default value, combine with showDefault or showDefaultWith.

valuemetavar :: HasMetavar f => String -> Mod f a
#

Specify a metavariable for the argument.

Metavariables have no effect on the actual parser, and only serve to specify the symbolic name for an argument to be displayed in the help text.

valuehidden :: Mod f a
#

Hide this option from the brief description.

Use internal to hide the option from the help text too.

valueinternal :: Mod f a
#

Hide this option completely from the help text

Use hidden if the option should remain visible in the full description.

valuestyle :: (Doc -> Doc) -> Mod f a
#

Apply a function to the option description in the usage text.

import Options.Applicative.Help
flag' () (short 't' <> style (annotate bold))

NOTE: This builder is more flexible than its name and example allude. One of the motivating examples for its addition was to use const to completely replace the usage text of an option.

valuecommand :: String -> ParserInfo a -> Mod CommandFields a
#

Add a command to a subparser option.

Suggested usage for multiple commands is to add them to a single subparser. e.g.

sample :: Parser Sample
sample = subparser
       ( command "hello"
         (info hello (progDesc "Print greeting"))
      <> command "goodbye"
         (info goodbye (progDesc "Say goodbye"))
       )

Add a description to a group of commands.

Advanced feature for separating logical groups of commands on the parse line.

If using the same metavar for each group of commands, it may yield a more attractive usage text combined with hidden for some groups.

valuecompleter :: HasCompleter f => Completer -> Mod f a
#

Add a completer to an argument.

A completer is a function String -> IO String which, given a partial argument, returns all possible completions for that argument.

valueidm :: Monoid m => m
#

Trivial option modifier.

methodmappend :: a -> a -> a
#

An associative operation

NOTE: This method is redundant and has the default implementation mappend = (<>) since base-4.11.0.0. Should it be implemented manually, since mappend is a synonym for (<>), it is expected that the two functions are defined the same way. In a future GHC release mappend will be removed from Monoid.

Readers

7 declarations

A collection of basic Option readers.

valueeitherReader :: (String -> Either String a) -> ReadM a
#

Convert a function producing an Either into a reader.

As an example, one can create a ReadM from an attoparsec Parser easily with

import qualified Data.Attoparsec.Text as A
import qualified Data.Text as T
attoparsecReader :: A.Parser a -> ReadM a
attoparsecReader p = eitherReader (A.parseOnly p . T.pack)

Builder for ParserInfo

14 declarations
newtypenewtype InfoMod a
#

Modifier for ParserInfo.

Instances2Semigroup, Monoid
  • Semigroup (InfoMod a)Defined in optparse-applicative-0.18.1.0 · Options.Applicative.Builder
  • Monoid (InfoMod a)Defined in optparse-applicative-0.18.1.0 · Options.Applicative.Builder
valuefullDesc :: InfoMod a
#

Show a full description in the help text of this parser (default).

valuebriefDesc :: InfoMod a
#

Only show a brief description in the help text of this parser.

valuenoIntersperse :: InfoMod a
#

Disable parsing of regular options after arguments. After a positional argument is parsed, all remaining options and arguments will be treated as a positional arguments. Not recommended in general as users often expect to be able to freely intersperse regular options and flags within command line options.

valueforwardOptions :: InfoMod a
#

Intersperse matched options and arguments normally, but allow unmatched options to be treated as positional arguments. This is sometimes useful if one is wrapping a third party cli tool and needs to pass options through, while also providing a handful of their own options. Not recommended in general as typos by the user may not yield a parse error and cause confusion.

valueallPositional :: InfoMod a
#

Disable parsing of regular options completely. All options and arguments will be treated as a positional arguments. Obviously not recommended in general as options will be unreachable. This is the same behaviour one sees after the "--" pseudo-argument.

Builder for ParserPrefs

13 declarations
newtypenewtype PrefsMod
#
Instances2Semigroup, Monoid
  • Semigroup PrefsModDefined in optparse-applicative-0.18.1.0 · Options.Applicative.Builder
  • Monoid PrefsModDefined in optparse-applicative-0.18.1.0 · Options.Applicative.Builder

Show the help text if the user enters only the program name or subcommand.

This will suppress a "Missing:" error and show the full usage instead if a user just types the name of the program.

Allow full mixing of subcommand and parent arguments by inlining selected subparsers into the parent parser.

NOTE: When this option is used, preferences for the subparser which effect the parser behaviour (such as noIntersperse) are ignored.

Show equals sign, rather than space, in usage and help text for options with long names.

Types

10 declarations
datadata Mod (f :: Type -> Type) a
#

An option modifier.

Option modifiers are values that represent a modification of the properties of an option.

The type parameter a is the return type of the option, while f is a record containing its properties (e.g. OptionFields for regular options, FlagFields for flags, etc...).

An option modifier consists of 3 elements:

  • A field modifier, of the form f a -> f a. These are essentially (compositions of) setters for some of the properties supported by f.

  • An optional default value and function to display it.

  • A property modifier, of the form OptProperties -> OptProperties. This is just like the field modifier, but for properties applicable to any option.

Modifiers are instances of Monoid, and can be composed as such.

One rarely needs to deal with modifiers directly, as most of the times it is sufficient to pass them to builders (such as strOption or flag) to create options (see Options.Applicative.Builder).

Instances2Semigroup, Monoid
  • Semigroup (Mod f a)Defined in optparse-applicative-0.18.1.0 · Options.Applicative.Builder.Internal
  • Monoid (Mod f a)Defined in optparse-applicative-0.18.1.0 · Options.Applicative.Builder.Internal
newtypenewtype ReadM a
#

A newtype over 'ReaderT String Except', used by option readers.

Instances6Monad, Functor, MonadFail, Applicative, Alternative, MonadPlus
  • Monad ReadMDefined in optparse-applicative-0.18.1.0 · Options.Applicative.Types
  • Functor ReadMDefined in optparse-applicative-0.18.1.0 · Options.Applicative.Types
  • MonadFail ReadMDefined in optparse-applicative-0.18.1.0 · Options.Applicative.Types
  • Applicative ReadMDefined in optparse-applicative-0.18.1.0 · Options.Applicative.Types
  • Alternative ReadMDefined in optparse-applicative-0.18.1.0 · Options.Applicative.Types
  • MonadPlus ReadMDefined in optparse-applicative-0.18.1.0 · Options.Applicative.Types
datadata OptionFields a
#
Instances4HasCompleter, HasMetavar, HasName, HasValue
datadata FlagFields a
#
Instances1HasName
  • HasName FlagFieldsDefined in optparse-applicative-0.18.1.0 · Options.Applicative.Builder.Internal
classclass HasName (f :: Type -> Type) where
#
Instances2HasName
  • HasName FlagFieldsDefined in optparse-applicative-0.18.1.0 · Options.Applicative.Builder.Internal
  • HasName OptionFieldsDefined in optparse-applicative-0.18.1.0 · Options.Applicative.Builder.Internal