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

Safe HaskellNone
LanguageHaskell2010

Language.Syntax.CmdArgs

Contents

Synopsis

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

Constructors

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

Instances

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.

Constructors

NoArg a

no argument expected

ReqArg (String -> a) String

option requires argument

OptArg (Maybe String -> a) String

optional argument

Instances

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 :: Monad m => [OptDescr a] -> (String -> a) -> ParserT [String] m [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)