Module

Options.Applicative

Re-exports from Options.Applicative.Builder

#Mod

data Mod f a

An option modifier.

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

The type parameter @a@ is the pure 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').

Contraints are often used to ensure that the modifiers can be sensibly applied. For example, positional arguments can't be specified by long or short names, so the 'HasName' constraint is used to ensure we have a flag or option.

Instances

#InfoMod

newtype InfoMod a

Modifier for 'ParserInfo'.

Instances

#FlagFields

#CommandFields

#value

value :: forall f 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.

#switch

switch :: Mod FlagFields Boolean -> Parser Boolean

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

#subparserInline

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.

#subparser

subparser :: forall a. Mod CommandFields a -> Parser a

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

#style

style :: forall f 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 bold)

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

#strOption

strOption :: Mod OptionFields String -> Parser String

Builder for an option taking a 'String' argument.

#strArgument

strArgument :: Mod ArgumentFields String -> Parser String

Builder for a 'String' argument.

#str

str :: ReadM String

String 'Option' reader.

#showHelpOnError

showHelpOnError :: PrefsMod

Show full help text on any error.

#showHelpOnEmpty

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.

#showDefaultWith

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

Show the default value for this option using a function.

#showDefault

showDefault :: forall f a. Show a => Mod f a

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

#short

short :: forall f a. HasName f => Char -> Mod f a

Specify a short name for an option.

#progDescDoc

progDescDoc :: forall a. Maybe Doc -> InfoMod a

Specify a short program description as a 'Text.PrettyPrint.ANSI.Leijen.Doc' value.

#progDesc

progDesc :: forall a. String -> InfoMod a

Specify a short program description.

#prefs

prefs :: PrefsMod -> ParserPrefs

Create a ParserPrefs given a modifier

#option

option :: forall a. 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' )

#number

number :: ReadM Number

Number 'Option' reader.

#noIntersperse

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

#noBacktrack

noBacktrack :: PrefsMod

Turn off backtracking after subcommand is parsed.

#noArgError

noArgError :: forall a. ParseError -> Mod OptionFields a

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

#multiSuffix

multiSuffix :: String -> PrefsMod

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

#metavar

metavar :: forall f 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.

#maybeReader

maybeReader :: forall a. (String -> Maybe a) -> ReadM a

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

#long

long :: forall f a. HasName f => String -> Mod f a

Specify a long name for an option.

#internal

internal :: forall f a. Mod f a

Hide this option from the help text

#int

int :: ReadM Int

Int 'Option' reader.

#infoOption

infoOption :: forall a. String -> Mod OptionFields (a -> a) -> Parser (a -> a)

An option that always fails and displays a message.

#info

info :: forall a. Parser a -> InfoMod a -> ParserInfo a

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

#idm

idm :: forall m. Monoid m => m

Trivial option modifier.

#hidden

hidden :: forall f a. Mod f a

Hide this option from the brief description.

#helpDoc

helpDoc :: forall f a. Maybe Doc -> Mod f a

Specify the help text for an option as a 'Text.PrettyPrint.ANSI.Leijen.Doc' value.

#help

help :: forall a f. String -> Mod f a

Specify the help text for an option.

#headerDoc

headerDoc :: forall a. Maybe Doc -> InfoMod a

Specify a header for this parser as a 'Text.PrettyPrint.ANSI.Leijen.Doc' value.

#header

header :: forall a. String -> InfoMod a

Specify a header for this parser.

#fullDesc

fullDesc :: forall a. InfoMod a

Show a full description in the help text of this parser (i.e. options with the hidden modifier will still be displayed, unlike briefDesc).

#forwardOptions

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

#footerDoc

footerDoc :: forall a. Maybe Doc -> InfoMod a

Specify a footer for this parser as a 'Text.PrettyPrint.ANSI.Leijen.Doc' value.

#footer

footer :: forall a. String -> InfoMod a

Specify a footer for this parser.

#flag'

flag' :: forall a. a -> Mod FlagFields a -> 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.

#flag

flag :: forall a. a -> a -> Mod FlagFields a -> 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'@.

#failureCode

failureCode :: forall a. ExitCode -> InfoMod a

Specify an exit code if a parse error occurs.

#eitherReader

eitherReader :: forall a. (String -> Either String a) -> ReadM a

Convert a function producing an 'Either' into a reader.

#disabled

disabled :: forall a. ReadM a

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

#defaultPrefs

defaultPrefs :: ParserPrefs

Default preferences.

#completer

completer :: forall f a. HasCompleter f => Completer -> Mod f a

Add a completer to an argument.

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

#completeWith

completeWith :: forall f a. HasCompleter f => Array String -> Mod f a

Add a list of possible completion values.

#commandGroup

commandGroup :: forall a. 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.

#command

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

#columns

columns :: Int -> PrefsMod

Set the maximum width of the generated help text.

#briefDesc

briefDesc :: forall a. InfoMod a

Only show a brief description in the help text of this parser (i.e. options with the hidden modifier will NOT be displayed, unlike fullDesc).

#boolean

boolean :: ReadM Boolean

Boolean 'Option' reader.

#argument

argument :: forall a. ReadM a -> Mod ArgumentFields a -> Parser a

Builder for an argument parser.

#action

action :: forall f 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.

#abortOption

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

Re-exports from Options.Applicative.Builder.Completer

#listIOCompleter

listIOCompleter :: Effect (Array String) -> Completer

Create a 'Completer' from an IO action

#listCompleter

listCompleter :: (Array String) -> Completer

Create a 'Completer' from a constant

#bashCompleter

bashCompleter :: String -> Completer

Run a compgen completion action.

Re-exports from Options.Applicative.Extra

#parserFailure

parserFailure :: forall a. ParserPrefs -> ParserInfo a -> ParseError -> Array Context -> ParserFailure ParserHelp

Generate a ParserFailure from a ParseError in a given Context.

This function can be used, for example, to show the help text for a parser:

handleParseResult <<< Failure $ parserFailure pprefs pinfo ShowHelpText mempty

#hsubparser

hsubparser :: forall a. Mod CommandFields a -> Parser a

Builder for a command parser with a "helper" option attached. Used in the same way as subparser, but includes a --help|-h inside the subcommand.

#helper

helper :: forall a. Parser (a -> a)

A hidden "helper" option which always fails. Use this to add the --help flag to your CLI parser

A common usage pattern is to apply this applicatively when creating a 'ParserInfo'

opts :: ParserInfo Sample opts = info (sample <**> helper) mempty

#handleParseResult

handleParseResult :: forall a. ParserResult a -> Effect a

Handle ParserResult.

#getParseResult

getParseResult :: forall a. ParserResult a -> Maybe a

Extract the actual result from a ParserResult value.

This function returns 'Nothing' in case of errors. Possible error messages or completion actions are simply discarded.

If you want to display error messages and invoke completion actions appropriately, use 'handleParseResult' instead.

#execParserPure

execParserPure :: forall a. ParserPrefs -> ParserInfo a -> Array String -> ParserResult a

The most general way to run a program description in pure code.

#execParser

execParser :: forall a. ParserInfo a -> Effect a

Run a program description.

Parse command line arguments. Display help text and exit if any parse error occurs.

#customExecParser

customExecParser :: forall a. ParserPrefs -> ParserInfo a -> Effect a

Run a program description with custom preferences.

Re-exports from Options.Applicative.Internal.Utils

#(<**>)

Operator alias for Options.Applicative.Internal.Utils.apApplyFlipped (left-associative / precedence 4)

Re-exports from Options.Applicative.Types

#ReadM

newtype ReadM a

A reader is used by the 'option' and 'argument' builders to parse the data passed by the user on the command line into a data type.

The most common are 'str' which is used for 'String', there are readers for Int, Number, Boolean.

More complex types can use the 'eitherReader' or 'maybeReader' functions to pattern match or use a more expressive parser like a member of the 'Parsec' family. A newtype over 'ReaderT String Except', used by option readers.

Instances

#ParserPrefs

newtype ParserPrefs

Global preferences for a top-level 'Parser'. A 'ParserPrefs' contains general preferences for all command-line options, and can be built with the 'prefs' function.

Constructors

Instances

#ParserInfo

newtype ParserInfo a

A 'ParserInfo' describes a command line program, used to generate a help screen. Two help modes are supported: brief and full. In brief mode, only an option and argument summary is displayed, while in full mode each available option and command, including hidden ones, is described.

A basic 'ParserInfo' with default values for fields can be created using the 'info' function.

Constructors

Instances

#ParserHelp

newtype ParserHelp

Constructors

Instances

#Parser

data Parser a

A 'Parser' is the core type in optparse-applicative. A value of type Parser a@ represents a specification for a set of options, which will yield a value of type a when the command line arguments are successfully parsed.

There are several types of primitive 'Parser'.

  • Flags: simple no-argument options. When a flag is encountered on the command line, its value is returned.

  • Options: options with an argument. An option can define a /reader/, which converts its argument from String to the desired value, or throws a parse error if the argument does not validate correctly.

  • Arguments: positional arguments, validated in the same way as option arguments.

  • Commands. A command defines a completely independent sub-parser. When a command is encountered, the whole command line is passed to the corresponding parser.

** Parser builders

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

For example:

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

creates a parser for an option called "output".

Instances

#CompletionResult

#Completer

newtype Completer

optparse-applicative supplies a rich completion system for bash, zsh, and fish shells.

'Completer' functions are used for option and argument to complete their values.

Use the 'completer' builder to use these. The 'action' and 'completeWith' builders are also provided for convenience, to use 'bashCompleter' and 'listCompleter' as a 'Mod'.

Instances

#some

some :: forall a. Parser a -> Parser (NonEmptyList a)

Parses 1 or more values using the given parser. Note: this should never be used with the value modifier.

For example, by using this option some (strOption (long "arg-name"))

one could write

command
# produces failure message

command --arg-name first
# produces (NonEmptyList "first" Nil)

command --arg-name first --arg-name second
# produces (NonEmptyList "first" ("second" : Nil))

To parse 0 or more values, see many instead.

#readerError

readerError :: forall a. String -> ReadM a

Abort option reader by exiting with an error message.

#readerAbort

readerAbort :: forall a. ParseError -> ReadM a

Abort option reader by exiting with a 'ParseError'.

#overFailure

#mkCompleter

mkCompleter :: (String -> Effect (Array String)) -> Completer

Smart constructor for a 'Completer'

#many

many :: forall a. Parser a -> Parser (List a)

Parses 0 or more values using the given parser. Note: this should never be used with the value modifier.

For example, by using this option many (strOption (long "arg-name"))

one could write

command
# produces Nil

command --arg-name first
# produces ("first" : Nil)

command --arg-name first --arg-name second
# produces ("first" : "second" : Nil)

To parse 1 or more values, see some instead.

Modules