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

Safe Haskell | Safe-Inferred
Language | Haskell98

Options.Applicative.Extra

Contents

- Extra parser utilities

Synopsis

- helper :: Parser (a -> a)
- helperWith :: Mod OptionFields (a -> a) -> Parser (a -> a)
- hsubparser :: Mod CommandFields a -> Parser a
- simpleVersioner :: String -> Parser (a -> a)
- execParser :: ParserInfo a -> IO a
- customExecParser :: ParserPrefs -> ParserInfo a -> IO a
- execParserPure :: ParserPrefs -> ParserInfo a -> [String] -> ParserResult a
- getParseResult :: ParserResult a -> Maybe a
- handleParseResult :: ParserResult a -> IO a
- parserFailure :: ParserPrefs -> ParserInfo a -> ParseError -> [Context] -> ParserFailure ParserHelp
- renderFailure :: ParserFailure ParserHelp -> String -> (String, ExitCode)
- newtype ParserFailure h = ParserFailure {
  - execFailure :: String -> (h, ExitCode, Int)
  }
- overFailure :: (ParserHelp -> ParserHelp) -> ParserResult a -> ParserResult a
- data ParserResult a
  - = Success a
  - | Failure (ParserFailure ParserHelp)
  - | CompletionInvoked CompletionResult
- data ParserPrefs = ParserPrefs {
  - prefMultiSuffix :: String
  - prefDisambiguate :: Bool
  - prefShowHelpOnError :: Bool
  - prefShowHelpOnEmpty :: Bool
  - prefBacktrack :: Backtracking
  - prefColumns :: Int
  - prefHelpLongEquals :: Bool
  - prefHelpShowGlobal :: Bool
  - prefTabulateFill :: Int
  }
- newtype CompletionResult = CompletionResult {
  - execCompletion :: String -> IO String
  }

# Extra parser utilities

This module contains high-level functions to run parsers.

helper :: Parser (a -> a) Source #

A hidden "helper" option which always fails.

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

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

helperWith :: Mod OptionFields (a -> a) -> Parser (a -> a) Source #

Like helper, but with a minimal set of modifiers that can be extended as desired.

    opts :: ParserInfo Sample
    opts = info (sample <**> helperWith (mconcat [
             long "help",
             short 'h',
             help "Show this help text",
             hidden
           ])) mempty

hsubparser :: Mod CommandFields a -> Parser a Source #

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.

simpleVersioner Source #

Arguments

:: String | Version string to be shown
-> Parser (a -> a) |

A hidden "--version" option that displays the version.

    opts :: ParserInfo Sample
    opts = info (sample <**> simpleVersioner "v1.2.3") mempty

execParser :: ParserInfo a -> IO a Source #

Run a program description.

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

customExecParser :: ParserPrefs -> ParserInfo a -> IO a Source #

Run a program description with custom preferences.

execParserPure Source #

Arguments

:: ParserPrefs | Global preferences for this parser
-> ParserInfo a | Description of the program to run
-> [String] | Program arguments
-> ParserResult a |

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

getParseResult :: ParserResult a -> Maybe a Source #

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.

handleParseResult :: ParserResult a -> IO a Source #

Handle `ParserResult`.

parserFailure :: ParserPrefs -> ParserInfo a -> ParseError -> [Context] -> ParserFailure ParserHelp Source #

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 Nothing) mempty

renderFailure :: ParserFailure ParserHelp -> String -> (String, ExitCode) Source #

newtype ParserFailure h Source #

Constructors

ParserFailure |

Fields

- execFailure :: String -> (h, ExitCode, Int)

#### Instances

Instances details

Functor ParserFailure Source # |
Instance details Defined in Options.Applicative.Types Methods fmap :: (a -> b) -> ParserFailure a -> ParserFailure b Source # (<$) :: a -> ParserFailure b -> ParserFailure a Source #
Show h => Show (ParserFailure h) Source # |
Instance details Defined in Options.Applicative.Types Methods showsPrec :: Int -> ParserFailure h -> ShowS Source # show :: ParserFailure h -> String Source # showList :: [ParserFailure h] -> ShowS Source #

overFailure :: (ParserHelp -> ParserHelp) -> ParserResult a -> ParserResult a Source #

data ParserResult a Source #

Result of `execParserPure`.

Constructors

Success a |
Failure (ParserFailure ParserHelp) |
CompletionInvoked CompletionResult |

#### Instances

Instances details

Applicative ParserResult Source # |
Instance details Defined in Options.Applicative.Types Methods pure :: a -> ParserResult a Source # (<*>) :: ParserResult (a -> b) -> ParserResult a -> ParserResult b Source # liftA2 :: (a -> b -> c) -> ParserResult a -> ParserResult b -> ParserResult c Source # (*>) :: ParserResult a -> ParserResult b -> ParserResult b Source # (<*) :: ParserResult a -> ParserResult b -> ParserResult a Source #
Functor ParserResult Source # |
Instance details Defined in Options.Applicative.Types Methods fmap :: (a -> b) -> ParserResult a -> ParserResult b Source # (<$) :: a -> ParserResult b -> ParserResult a Source #
Monad ParserResult Source # |
Instance details Defined in Options.Applicative.Types Methods (>>=) :: ParserResult a -> (a -> ParserResult b) -> ParserResult b Source # (>>) :: ParserResult a -> ParserResult b -> ParserResult b Source # return :: a -> ParserResult a Source #
Show a => Show (ParserResult a) Source # |
Instance details Defined in Options.Applicative.Types Methods showsPrec :: Int -> ParserResult a -> ShowS Source # show :: ParserResult a -> String Source # showList :: [ParserResult a] -> ShowS Source #

data ParserPrefs Source #

Global preferences for a top-level `Parser`.

Constructors

ParserPrefs |

Fields

- prefMultiSuffix :: String

  metavar suffix for multiple options

- prefDisambiguate :: Bool

  automatically disambiguate abbreviations (default: False)

- prefShowHelpOnError :: Bool

  always show help text on parse errors (default: False)

- prefShowHelpOnEmpty :: Bool

  show the help text for a command or subcommand if it fails with no input (default: False)

- prefBacktrack :: Backtracking

  backtrack to parent parser when a subcommand fails (default: Backtrack)

- prefColumns :: Int

  number of columns in the terminal, used to format the help page (default: 80)

- prefHelpLongEquals :: Bool

  when displaying long names in usage and help, use an '=' sign for long names, rather than a single space (default: False)

- prefHelpShowGlobal :: Bool

  when displaying subparsers' usage help, show parent options under a "global options" section (default: False)

- prefTabulateFill :: Int

  Indentation width for tables

#### Instances

Instances details

Show ParserPrefs Source # |
Instance details Defined in Options.Applicative.Types Methods showsPrec :: Int -> ParserPrefs -> ShowS Source # show :: ParserPrefs -> String Source # showList :: [ParserPrefs] -> ShowS Source #
Eq ParserPrefs Source # |
Instance details Defined in Options.Applicative.Types Methods (==) :: ParserPrefs -> ParserPrefs -> Bool Source # (/=) :: ParserPrefs -> ParserPrefs -> Bool Source #

newtype CompletionResult Source #

Constructors

CompletionResult |

Fields

- execCompletion :: String -> IO String

#### Instances

Instances details

Show CompletionResult Source # |
Instance details Defined in Options.Applicative.Types Methods showsPrec :: Int -> CompletionResult -> ShowS Source # show :: CompletionResult -> String Source # showList :: [CompletionResult] -> ShowS Source #