multiarg-0.2.0.0: Combinators to build command line parsers

Safe HaskellSafe-Infered

System.Console.MultiArg.SimpleParser

Description

A simple command line parser that can parse options that take an optional argument, one or two arguments, or a variable number of arguments. For sample code that uses this parser, see SampleParser.

Synopsis

Documentation

data OptSpec Source

Specifies each option that your program accepts.

Constructors

OptSpec 

Fields

longOpt :: String

Each option must have at least one long option, which you specify here. Your program's users specify long options by preceding them with two dashes, such as --verbose. When writing your code you omit the dashes, so you would specify verbose here; including the dashes in your code results in a runtime error.

shortOpts :: [Char]

Additional, synonymous short options may be specified here. For instance if you want your users to be able to specify -v in addition to --verbose, include v in this list.

longOpts :: [String]

Additional synonymous long options may be specified here. For instance, if you specified quiet for longOpt, you might want to include silent in this list.

argSpec :: Args

Specifies what arguments, if any, this option takes.

Instances

data Intersperse Source

What to do after encountering the first non-option, non-option-argument word on the command line? In either case, no more options are parsed after a stopper.

Constructors

Intersperse

Additional options are allowed on the command line after encountering the first positional argument. For example, if a and b are options, in the command line -a posarg -b, b will be parsed as an option. If b is not an option and the same command line is entered, then -b will result in an error because -b starts with a hyphen and therefore "looks like" an option.

StopOptions

No additional options will be parsed after encountering the first positional argument. For example, if a and b are options, in the command line -a posarg -b, b will be parsed as a positional argument rather than as an option.

data Result Source

Holds the result of command line parsing. Each option (along with its option arguments) or positional argument is assigned to its own Result.

Constructors

PosArg 

Fields

posArg :: String
 
Stopper 
Option 

Fields

label :: String

Each option must have at least one long option. So that you can distinguish one option from another, the name of that long option is returned here.

args :: Args
 

Instances

data Args Source

This datatype does dual duty. When part of an OptSpec, you use it to specify how many arguments, if any, an option takes. When you use it for this purpose, it only matters which data constructor you use; the fields can be any value, even undefined.

When part of a Result, Args indicates what arguments the user supplied to the option.

Constructors

NoArg

This option takes no arguments

OptionalArg

This option takes an optional argument. As noted in "The Tao of Option Parsing", optional arguments can result in some ambiguity. (Read it here: http://optik.sourceforge.net/doc/1.5/tao.html) If option a takes an optional argument, and b is also an option, what does -ab mean? SimpleParser resolves this ambiguity by assuming that b is an argument to a. If the user does not like this, she can specify -a -b (in such an instance -b is not parsed as an option to -a, because -b begins with a hyphen and therefore "looks like" an option.) Certainly though, optional arguments lead to ambiguity, so if you don't like it, don't use them :)

Fields

oArg :: Maybe String
 
OneArg

This option takes one argument. Here, if option a takes one argument, -a -b will be parsed with -b being an argument to option a, even though -b starts with a hyphen and therefore "looks like" an option.

Fields

sArg1 :: String
 
TwoArg

This option takes two arguments. Parsed similarly to OneArg.

Fields

tArg1 :: String
 
tArg2 :: String
 
VariableArg

This option takes a variable number of arguments--zero or more. Option arguments continue until the command line contains a word that begins with a hyphen. For example, if option a takes a variable number of arguments, then -a one two three -b will be parsed as a taking three arguments, and -a -b will be parsed as a taking no arguments. If the user enters -a as the last option on the command line, then the only way to indicate the end of arguments for a and the beginning of positional argments is with a stopper.

Fields

vArgs :: [String]
 

Instances

noArg :: ArgsSource

Specify that this option takes no arguments.

optionalArg :: ArgsSource

Specify that this option takes an optional argument.

oneArg :: ArgsSource

Specify that this option takes one argument.

twoArg :: ArgsSource

Specify that this option takes two arguments.

variableArg :: ArgsSource

Specify that this option takes a variable number of arguments.

data SimpleError Source

A simple type that is already an instance of Error.

getArgs :: IO [String]

Computation getArgs returns a list of the program's command line arguments (not including the program name).

parseSource

Arguments

:: Intersperse 
-> [OptSpec] 
-> [String]

The command line to parse. This function correctly handles Unicode strings; however, because getArgs does not always correctly handle Unicode strings, consult the documentation in GetArgs and consider using the functions in there if there is any chance that you will be parsing command lines that have non-ASCII strings.

-> Either SimpleError [Result] 

Parse a command line.