definitive-parser-1.2: A parser combinator library for the Definitive framework

Safe HaskellNone




Exported modules

Preprocessing command-line arguments

data OptDescr a

Each OptDescr describes a single option.

The arguments to Option are:

  • list of short option characters
  • list of long option strings (without "--")
  • argument descriptor
  • explanation of option for user


Option [Char] [String] (ArgDescr a) String 

data ArgDescr a

Describes whether an option takes an argument or not, and if so how the argument is injected into a value of type a.


NoArg a

no argument expected

ReqArg (String -> a) String

option requires argument

OptArg (Maybe String -> a) String

optional argument

usageInfo :: String -> [OptDescr a] -> String

Return a string describing the usage of a command, derived from the header (first argument) and the options described by the second argument.

tokenize :: [OptDescr a] -> (String -> a) -> ParserT [String] (WriterT String Id) [a]Source

Create a Parser that preprocesses the command-line arguments, splitting options and their arguments into a user-defined data type.

Example usage

This module is intended to provide simple parsing functionality to the handling of command-line arguments. Here is an example of how this module may be used.

data Option = Help | Version | Other String
           deriving Eq
options = [
  Option ['h'] ["help"] (NoArg Help) "Display this menu.",
  Option ['v'] ["version"] (NoArg Version) "Show the version of this program"

mainAxiom = single Help >> lift (putStrLn (usageInfo options))
          <+> single Version >> lift (putStrLn "Version: 1.0")

main = void $ do
    getArgs >>= (mainAxiom <*< tokenize options Other)