Applicative style combines independent computations through an applicative interface. For command-line programs, it builds a typed result by applying a constructor to several argument parsers. section 15.3.1 section 15.3.4
A command-line parser commonly combines:
- strOption for string arguments, option auto for readable types such as integers, switch for Boolean flags, and optional for Maybe-valued arguments. section 15.3.1
- Applicative operators: <$> applies a constructor to the first parser, while <*> supplies subsequent parsed values. section 15.3.1
- Parser descriptions combined with <>; their order is irrelevant because the option metadata forms a monoid-style composition. section 15.3.1
- ParserInfo adds help, version information, and program descriptions before execParser runs the parser. section 15.3.1
Listing 15.13 An example of an argument parser
import Options.Applicative
...
argsParser :: Parser Args
argsParser = Args <$> strArg <*> intArg <*>
boolArg <*> maybeArg #1
where
strArg = #2
strOption $
long "string"
<> short 's'
<> metavar "STRING"
<> help "An example string"
intArg = #3
option auto $
long "integer"
<> short 'i'
<> metavar "INTEGER"
<> help "An example integer"
boolArg = #4
switch $
long "boolean"
<> short 'b'
<> help "An example boolean switch"
maybeArg = #5
optional $
strOption $
long "optional"
<> short 'o'
<> metavar "OPTIONAL"
<> help "An example optional string"
The complete parser constructs an Args value from required, Boolean, and optional command-line fields. section 15.3.1
Parsing a synchronization tool's configuration
A CLI parser can nest applicative construction: CliArgs <$> config <*> isVerbose, with config itself built from path, file-type, recursion, and file-count parsers. Running the tool then executes execParser, passes the configuration to the application, and conditionally prints its log. section 15.3.4

