Safe Haskell | Safe-Infered |
---|
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 System.Console.MultiArg.SampleParser.
- data Intersperse
- data OptSpec a = OptSpec {}
- data ArgSpec a
- data Exceptional e a
- data Error = Error [Message] Location
- data Message
- getArgs :: IO [String]
- parse :: Intersperse -> [OptSpec a] -> (String -> a) -> [String] -> Exceptional Error [a]
Interspersion control
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.
Intersperse | Additional options are allowed on the command line after
encountering the first positional argument. For example, if |
StopOptions | No additional options will be parsed after encountering the
first positional argument. For example, if |
Option specifications
Specifies options for the parseOption
function. Each OptSpec
represents one command-line option.
OptSpec | |
|
Specifies how many arguments each option takes. As with
ArgDescr
, there are (at least) two ways to
use this type. You can simply represent each possible option using
different data constructors in an algebraic data type. Or you can
have each ArgSpec yield a function that transforms a record. For an
example that uses an algebraic data type, see
System.Console.MultiArg.SampleParser.
NoArg a | This option takes no arguments |
OptionalArg (Maybe String -> a) | 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 |
OneArg (String -> a) | This option takes one argument. Here, if option |
TwoArg (String -> String -> a) | This option takes two arguments. Parsed similarly to |
VariableArg ([String] -> a) | 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 |
Exceptions
data Exceptional e a
Like Either
, but explicitly intended for handling of exceptional results.
In contrast to Either
we do not support fail
.
Calling fail
in the Exceptional
monad is an error.
This way, we do not require that an exception can be derived from a String
,
yet, we require no constraint on the exception type at all.
Monad (Exceptional e) | |
Functor (Exceptional e) | |
MonadFix (Exceptional e) | |
Applicative (Exceptional e) | |
(Eq e, Eq a) => Eq (Exceptional e a) | |
(Show e, Show a) => Show (Exceptional e a) |
An Error contains a list of Messages and a String indicating where the error happened.
Error messages.
Expected String | The parser expected to see one thing, but it actually saw something else. The string indicates what was expected. |
StrMsg String | The |
Replaced String | A previous list of error messages was replaced with this error message. |
UnknownError | Any other error; used by |
Get command line arguments
Gets the command-line arguments supplied by the program's
user. If the base
package is older than version 4.4, then this
function assumes the command line is encoded in UTF-8, which is
true for many newer Unix systems; however, many older systems may
use single-byte encodings like ISO-8859. In such cases, this
function will give erroneous results.
If the base
package is version 4.4.0 or newer, this function
simply uses the getArgs that comes with base
. That getArgs
detects the system's default encoding and uses that, so it should
give accurate results on most systems.
The parser
:: Intersperse | What to do after encountering the first positional argument |
-> [OptSpec a] | All possible options |
-> (String -> a) | How to handle positional arguments. This function is applied to the appropriate string every time the parser encounters a positional argument. |
-> [String] | The command line to parse. This function correctly handles
Unicode strings; however, because |
-> Exceptional Error [a] |
Parse a command line.