liveBook · Manning

ParserInfo

As explained in Learn Haskell by Example

ParserInfo is the optparse-applicative type that packages a parser together with metadata for executing it and generating help text.

ParserInfo packages a Parser with metadata used to execute command-line parsing and generate help text. Build one with info, combining the argument parser with helper and version parsers, then supply descriptions such as progDesc, header, and footer. section 15.3.1

Listing 15.14 An example ParserInfo
argumentParser :: ParserInfo Args
argumentParser =
  info                  #1
   ( argsParser
        <**> helper                   #2
        <**> simpleVersioner "v1.0"       #3
    )
    ( fullDesc                                        #4
        <> progDesc "This is an example"
        <> header "OPTPARSE APPLICATIVE EXAMPLE"
        <> footer "Thanks for checking this example out!"
    )

This complete definition combines an argument parser with --help and --version support, then adds program descriptions and surrounding help text. section 15.3.1

Execute a ParserInfo

After defining argumentParser :: ParserInfo Args, run it in IO with args <- execParser argumentParser. Missing required options produce usage guidance; supplying -s "Foo" -i 10 yields Args {arg1 = "Foo", arg2 = 10, arg3 = False, arg4 = Nothing}. Optional switches and arguments can then be added without changing their order. section 15.3.1

ParserInfo also supplies the boundary for nested command-line interfaces. A subparser associates command names such as list, add, and toggle with separate ParserInfo values, each carrying its own arguments and help description. section 17.3.2

How Learn Haskell by Example covers it

How deep it goes
Defines construction and execution with examples, but stops short of library internals.
Examples
Uses an argument parser for strings, integers, switches, and optional values, then extends it with list, add, and toggle commands.
What it assumes
Assumes familiarity with Parser definitions and applicative parsing while demonstrating the ParserInfo API from its construction onward.
What you can do after
Build it from scratch by combining a Parser with helper parsers, descriptions, and execParser, including nested commands.
How it connects
Connects ParserInfo to Parser, applicative parsing, generated help text, version parsing, and subparser-based commands.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.