optparse-applicative-0.18.1.0: Utilities and combinators for parsing command line options

Safe Haskell | Safe-Inferred
Language | Haskell98

Options.Applicative.Builder

Contents

- Parser builders
- Modifiers
- Readers
- Builder for `ParserInfo`
- Builder for `ParserPrefs`
- Types

Synopsis

- subparser :: Mod CommandFields a -> Parser a
- strArgument :: IsString s => Mod ArgumentFields s -> Parser s
- argument :: ReadM a -> Mod ArgumentFields a -> Parser a
- flag :: a -> a -> Mod FlagFields a -> Parser a
- flag' :: a -> Mod FlagFields a -> Parser a
- switch :: Mod FlagFields Bool -> Parser Bool
- abortOption :: ParseError -> Mod OptionFields (a -> a) -> Parser (a -> a)
- infoOption :: String -> Mod OptionFields (a -> a) -> Parser (a -> a)
- strOption :: IsString s => Mod OptionFields s -> Parser s
- option :: ReadM a -> Mod OptionFields a -> Parser a
- short :: forall (f :: Type -> Type) a. HasName f => Char -> Mod f a
- long :: forall (f :: Type -> Type) a. HasName f => String -> Mod f a
- help :: forall (f :: Type -> Type) a. String -> Mod f a
- helpDoc :: forall (f :: Type -> Type) a. Maybe Doc -> Mod f a
- value :: forall (f :: Type -> Type) a. HasValue f => a -> Mod f a
- showDefaultWith :: forall a (f :: Type -> Type). (a -> String) -> Mod f a
- showDefault :: forall a (f :: Type -> Type). Show a => Mod f a
- metavar :: forall (f :: Type -> Type) a. HasMetavar f => String -> Mod f a
- noArgError :: ParseError -> Mod OptionFields a
- data ParseError
  - = ErrorMsg String
  - | InfoMsg String
  - | ShowHelpText (Maybe String)
  - | UnknownError
  - | MissingError IsCmdStart SomeParser
  - | ExpectsArgError String
  - | UnexpectedError String SomeParser
- hidden :: forall (f :: Type -> Type) a. Mod f a
- internal :: forall (f :: Type -> Type) a. Mod f a
- style :: forall (f :: Type -> Type) a. (Doc -> Doc) -> Mod f a
- command :: String -> ParserInfo a -> Mod CommandFields a
- commandGroup :: String -> Mod CommandFields a
- completeWith :: forall (f :: Type -> Type) a. HasCompleter f => [String] -> Mod f a
- action :: forall (f :: Type -> Type) a. HasCompleter f => String -> Mod f a
- completer :: forall (f :: Type -> Type) a. HasCompleter f => Completer -> Mod f a
- idm :: Monoid m => m
- mappend :: Monoid a => a -> a -> a
- auto :: Read a => ReadM a
- str :: IsString s => ReadM s
- maybeReader :: (String -> Maybe a) -> ReadM a
- eitherReader :: (String -> Either String a) -> ReadM a
- disabled :: ReadM a
- readerAbort :: ParseError -> ReadM a
- readerError :: String -> ReadM a
- data InfoMod a
- fullDesc :: InfoMod a
- briefDesc :: InfoMod a
- header :: String -> InfoMod a
- headerDoc :: Maybe Doc -> InfoMod a
- footer :: String -> InfoMod a
- footerDoc :: Maybe Doc -> InfoMod a
- progDesc :: String -> InfoMod a
- progDescDoc :: Maybe Doc -> InfoMod a
- failureCode :: Int -> InfoMod a
- noIntersperse :: InfoMod a
- forwardOptions :: InfoMod a
- allPositional :: InfoMod a
- info :: Parser a -> InfoMod a -> ParserInfo a
- data PrefsMod
- multiSuffix :: String -> PrefsMod
- disambiguate :: PrefsMod
- showHelpOnError :: PrefsMod
- showHelpOnEmpty :: PrefsMod
- noBacktrack :: PrefsMod
- subparserInline :: PrefsMod
- columns :: Int -> PrefsMod
- helpLongEquals :: PrefsMod
- helpShowGlobals :: PrefsMod
- helpIndent :: Int -> PrefsMod
- prefs :: PrefsMod -> ParserPrefs
- defaultPrefs :: ParserPrefs
- data Mod (f :: Type -> Type) a
- data ReadM a
- data OptionFields a
- data FlagFields a
- data ArgumentFields a
- data CommandFields a
- class HasName (f :: Type -> Type)
- class HasCompleter (f :: Type -> Type)
- class HasValue (f :: Type -> Type)
- class HasMetavar (f :: Type -> Type)

# Parser builders

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".

subparser :: 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`.

strArgument :: IsString s => Mod ArgumentFields s -> Parser s #

Builder for a `String` argument.

argument :: ReadM a -> Mod ArgumentFields a -> Parser a #

Builder for an argument parser.

flag #

Arguments

:: a | default value
-> a | active value
-> Mod FlagFields a | option modifier
-> 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'`.

flag' #

Arguments

:: a | active value
-> Mod FlagFields a | option modifier
-> 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.

switch :: Mod FlagFields Bool -> Parser Bool #

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

abortOption :: 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.

infoOption :: String -> Mod OptionFields (a -> a) -> Parser (a -> a) #

An option that always fails and displays a message.

strOption :: IsString s => Mod OptionFields s -> Parser s #

Builder for an option taking a `String` argument.

option :: 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

short :: forall (f :: Type -> Type) a. HasName f => Char -> Mod f a #

Specify a short name for an option.

long :: forall (f :: Type -> Type) a. HasName f => String -> Mod f a #

Specify a long name for an option.

help :: forall (f :: Type -> Type) a. String -> Mod f a #

Specify the help text for an option.

helpDoc :: forall (f :: Type -> Type) a. Maybe Doc -> Mod f a #

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

value :: forall (f :: Type -> Type) a. 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.

showDefaultWith :: forall a (f :: Type -> Type). (a -> String) -> Mod f a #

Specify a function to show the default value for an option.

showDefault :: forall a (f :: Type -> Type). Show a => Mod f a #

Show the default value for this option using its `Show` instance.

metavar :: forall (f :: Type -> Type) a. 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.

noArgError :: ParseError -> Mod OptionFields a #

Specify the error to display when no argument is provided to this option.

data ParseError #

Constructors

ErrorMsg String |
InfoMsg String |
ShowHelpText (Maybe String) |
UnknownError |
MissingError IsCmdStart SomeParser |
ExpectsArgError String |
UnexpectedError String SomeParser |

#### Instances

Instances details

Monoid ParseError # |
Instance details Defined in Options.Applicative.Types Methods mempty :: ParseError # mappend :: ParseError -> ParseError -> ParseError # mconcat :: [ParseError] -> ParseError #
Semigroup ParseError # |
Instance details Defined in Options.Applicative.Types Methods (<>) :: ParseError -> ParseError -> ParseError # sconcat :: NonEmpty ParseError -> ParseError # stimes :: Integral b => b -> ParseError -> ParseError #

hidden :: forall (f :: Type -> Type) a. Mod f a #

Hide this option from the brief description.

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

internal :: forall (f :: Type -> Type) a. Mod f a #

Hide this option completely from the help text

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

style :: forall (f :: Type -> Type) a. (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.

command :: 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"))
           )

commandGroup :: String -> Mod CommandFields a #

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.

completeWith :: forall (f :: Type -> Type) a. HasCompleter f => [String] -> Mod f a #

Add a list of possible completion values.

action :: forall (f :: Type -> Type) a. HasCompleter f => String -> Mod f a #

Add a bash completion action. Common actions include `file` and `directory`. See http://www.gnu.org/software/bash/manual/html_node/Programmable-Completion-Builtins.html#Programmable-Completion-Builtins for a complete list.

completer :: forall (f :: Type -> Type) a. 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.

idm :: Monoid m => m #

Trivial option modifier.

mappend :: Monoid a => 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

A collection of basic `Option` readers.

auto :: Read a => ReadM a #

`Option` reader based on the `Read` type class.

str :: IsString s => ReadM s #

String `Option` reader.

Polymorphic over the `IsString` type class since 0.14.

maybeReader :: (String -> Maybe a) -> ReadM a #

Convert a function producing a `Maybe` into a reader.

eitherReader :: (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)

disabled :: ReadM a #

Null `Option` reader. All arguments will fail validation.

readerAbort :: ParseError -> ReadM a #

Abort option reader by exiting with a `ParseError`.

readerError :: String -> ReadM a #

Abort option reader by exiting with an error message.

# Builder for `ParserInfo`

data InfoMod a #

Modifier for `ParserInfo`.

#### Instances

Instances details

Monoid (InfoMod a) # |
Instance details Defined in Options.Applicative.Builder Methods mempty :: InfoMod a # mappend :: InfoMod a -> InfoMod a -> InfoMod a # mconcat :: [InfoMod a] -> InfoMod a #
Semigroup (InfoMod a) # |
Instance details Defined in Options.Applicative.Builder Methods (<>) :: InfoMod a -> InfoMod a -> InfoMod a # sconcat :: NonEmpty (InfoMod a) -> InfoMod a # stimes :: Integral b => b -> InfoMod a -> InfoMod a #

fullDesc :: InfoMod a #

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

briefDesc :: InfoMod a #

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

header :: String -> InfoMod a #

Specify a header for this parser.

headerDoc :: Maybe Doc -> InfoMod a #

Specify a header for this parser as a 'Prettyprinter.Doc AnsiStyle' value.

footer :: String -> InfoMod a #

Specify a footer for this parser.

footerDoc :: Maybe Doc -> InfoMod a #

Specify a footer for this parser as a 'Prettyprinter.Doc AnsiStyle' value.

progDesc :: String -> InfoMod a #

Specify a short program description.

progDescDoc :: Maybe Doc -> InfoMod a #

Specify a short program description as a 'Prettyprinter.Doc AnsiStyle' value.

failureCode :: Int -> InfoMod a #

Specify an exit code if a parse error occurs.

noIntersperse :: 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.

forwardOptions :: 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.

allPositional :: 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.

info :: Parser a -> InfoMod a -> ParserInfo a #

Create a `ParserInfo` given a `Parser` and a modifier.

# Builder for `ParserPrefs`

data PrefsMod #

#### Instances

Instances details

Monoid PrefsMod # |
Instance details Defined in Options.Applicative.Builder Methods mempty :: PrefsMod # mappend :: PrefsMod -> PrefsMod -> PrefsMod # mconcat :: [PrefsMod] -> PrefsMod #
Semigroup PrefsMod # |
Instance details Defined in Options.Applicative.Builder Methods (<>) :: PrefsMod -> PrefsMod -> PrefsMod # sconcat :: NonEmpty PrefsMod -> PrefsMod # stimes :: Integral b => b -> PrefsMod -> PrefsMod #

multiSuffix :: String -> PrefsMod #

Include a suffix to attach to the metavar when multiple values can be entered.

disambiguate :: PrefsMod #

Turn on disambiguation.

See https://github.com/pcapriotti/optparse-applicative#disambiguation

showHelpOnError :: PrefsMod #

Show full help text on any error.

showHelpOnEmpty :: PrefsMod #

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.

noBacktrack :: PrefsMod #

Turn off backtracking after subcommand is parsed.

subparserInline :: PrefsMod #

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.

columns :: Int -> PrefsMod #

Set the maximum width of the generated help text.

helpLongEquals :: PrefsMod #

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

helpShowGlobals :: PrefsMod #

Show global help information in subparser usage.

helpIndent :: Int -> PrefsMod #

Set fill width in help text presentation.

prefs :: PrefsMod -> ParserPrefs #

Create a `ParserPrefs` given a modifier

defaultPrefs :: ParserPrefs #

Default preferences.

# Types

data 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 `Builder`).

#### Instances

Instances details

Monoid (Mod f a) # |
Instance details Defined in Options.Applicative.Builder.Internal Methods mempty :: Mod f a # mappend :: Mod f a -> Mod f a -> Mod f a # mconcat :: [Mod f a] -> Mod f a #
Semigroup (Mod f a) # | Since: 0.13.0.0
Instance details Defined in Options.Applicative.Builder.Internal Methods (<>) :: Mod f a -> Mod f a -> Mod f a # sconcat :: NonEmpty (Mod f a) -> Mod f a # stimes :: Integral b => b -> Mod f a -> Mod f a #

data ReadM a #

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

#### Instances

Instances details

Alternative ReadM # |
Instance details Defined in Options.Applicative.Types Methods empty :: ReadM a # (<|>) :: ReadM a -> ReadM a -> ReadM a # some :: ReadM a -> ReadM [a] # many :: ReadM a -> ReadM [a] #
Applicative ReadM # |
Instance details Defined in Options.Applicative.Types Methods pure :: a -> ReadM a # (<*>) :: ReadM (a -> b) -> ReadM a -> ReadM b # liftA2 :: (a -> b -> c) -> ReadM a -> ReadM b -> ReadM c # (*>) :: ReadM a -> ReadM b -> ReadM b # (<*) :: ReadM a -> ReadM b -> ReadM a #
Functor ReadM # |
Instance details Defined in Options.Applicative.Types Methods fmap :: (a -> b) -> ReadM a -> ReadM b # (<$) :: a -> ReadM b -> ReadM a #
Monad ReadM # |
Instance details Defined in Options.Applicative.Types Methods (>>=) :: ReadM a -> (a -> ReadM b) -> ReadM b # (>>) :: ReadM a -> ReadM b -> ReadM b # return :: a -> ReadM a #
MonadPlus ReadM # |
Instance details Defined in Options.Applicative.Types Methods mzero :: ReadM a # mplus :: ReadM a -> ReadM a -> ReadM a #
MonadFail ReadM # |
Instance details Defined in Options.Applicative.Types Methods fail :: String -> ReadM a #

data OptionFields a #

#### Instances

Instances details

HasCompleter OptionFields # |
Instance details Defined in Options.Applicative.Builder.Internal Methods modCompleter :: (Completer -> Completer) -> OptionFields a -> OptionFields a #
HasMetavar OptionFields # |
Instance details Defined in Options.Applicative.Builder.Internal Methods hasMetavarDummy :: OptionFields a -> () #
HasName OptionFields # |
Instance details Defined in Options.Applicative.Builder.Internal Methods name :: OptName -> OptionFields a -> OptionFields a #
HasValue OptionFields # |
Instance details Defined in Options.Applicative.Builder.Internal Methods hasValueDummy :: OptionFields a -> () #

data FlagFields a #

#### Instances

Instances details

HasName FlagFields # |
Instance details Defined in Options.Applicative.Builder.Internal Methods name :: OptName -> FlagFields a -> FlagFields a #

data ArgumentFields a #

#### Instances

Instances details

HasCompleter ArgumentFields # |
Instance details Defined in Options.Applicative.Builder.Internal Methods modCompleter :: (Completer -> Completer) -> ArgumentFields a -> ArgumentFields a #
HasMetavar ArgumentFields # |
Instance details Defined in Options.Applicative.Builder.Internal Methods hasMetavarDummy :: ArgumentFields a -> () #
HasValue ArgumentFields # |
Instance details Defined in Options.Applicative.Builder.Internal Methods hasValueDummy :: ArgumentFields a -> () #

data CommandFields a #

#### Instances

Instances details

HasMetavar CommandFields # |
Instance details Defined in Options.Applicative.Builder.Internal Methods hasMetavarDummy :: CommandFields a -> () #

class HasName (f :: Type -> Type) #

Minimal complete definition

name

#### Instances

Instances details

HasName FlagFields # |
Instance details Defined in Options.Applicative.Builder.Internal Methods name :: OptName -> FlagFields a -> FlagFields a #
HasName OptionFields # |
Instance details Defined in Options.Applicative.Builder.Internal Methods name :: OptName -> OptionFields a -> OptionFields a #

class HasCompleter (f :: Type -> Type) #

Minimal complete definition

modCompleter

#### Instances

Instances details

HasCompleter ArgumentFields # |
Instance details Defined in Options.Applicative.Builder.Internal Methods modCompleter :: (Completer -> Completer) -> ArgumentFields a -> ArgumentFields a #
HasCompleter OptionFields # |
Instance details Defined in Options.Applicative.Builder.Internal Methods modCompleter :: (Completer -> Completer) -> OptionFields a -> OptionFields a #

class HasValue (f :: Type -> Type) #

Minimal complete definition

hasValueDummy

#### Instances

Instances details

HasValue ArgumentFields # |
Instance details Defined in Options.Applicative.Builder.Internal Methods hasValueDummy :: ArgumentFields a -> () #
HasValue OptionFields # |
Instance details Defined in Options.Applicative.Builder.Internal Methods hasValueDummy :: OptionFields a -> () #

class HasMetavar (f :: Type -> Type) #

Minimal complete definition

hasMetavarDummy

#### Instances

Instances details

HasMetavar ArgumentFields # |
Instance details Defined in Options.Applicative.Builder.Internal Methods hasMetavarDummy :: ArgumentFields a -> () #
HasMetavar CommandFields # |
Instance details Defined in Options.Applicative.Builder.Internal Methods hasMetavarDummy :: CommandFields a -> () #
HasMetavar OptionFields # |
Instance details Defined in Options.Applicative.Builder.Internal Methods hasMetavarDummy :: OptionFields a -> () #