| Safe Haskell | Safe |
|---|---|
| Language | Haskell2010 |
SimpleGetOpt
Description
Provides support for processing command-line arguments. This is a simple wrapper around get-opt. Here is an example of a typical usage:
data Settings = Settings
{ verbose :: Bool
, inPar :: Int
, files :: [String]
}
options :: OptSpec Settings
options = OptSpec
{ progDefaults = Settings { verbose = False
, inPar = 1
, files = []
}
, progOptions =
[ Option ['v'] ["verbose"]
"Display more information while working."
$ NoArg $ \s -> Right s { verbose = True }
, Option ['p'] ["par"]
"Process that many files at once."
$ ReqArg "NUM" $ \a s ->
case readMaybe a of
Just n | n > 0 -> Right s { inPar = n }
_ -> Left "Invalid value for `par`"
]
, progParamDocs =
[ ("FILES", "The files that need processing.") ]
, progParams = \p s -> Right s { files = p : files s }
}Here is what the usage information looks like:
*Main> dumpUsage options Parameters: FILES The files that need processing. Flags: -v --verbose Display more information while working. -p NUM --par=NUM Process that many files at once.
Synopsis
- getOpts :: OptSpec a -> IO a
- getOptsX :: OptSpec a -> IO a
- data OptSpec a = OptSpec {
- progDefaults :: a
- progOptions :: [OptDescr a]
- progParamDocs :: [(String, String)]
- progParams :: String -> OptSetter a
- data OptDescr a = Option {
- optShortFlags :: [Char]
- optLongFlags :: [String]
- optDescription :: String
- optArgument :: ArgDescr a
- type OptSetter a = a -> Either String a
- data ArgDescr a
- data GetOptException = GetOptException [String]
- dumpUsage :: OptSpec a -> IO ()
- reportUsageError :: OptSpec a -> [String] -> IO b
- usageString :: OptSpec a -> String
Basic functionality
getOpts :: OptSpec a -> IO a Source #
Get the command-line options and process them according to the given spec. The options will be permuted to get flags. On failure, print an error message on standard error and exit.
getOptsX :: OptSpec a -> IO a Source #
Get the command-line options and process them according to the given spec.
The options will be permuted to get flags.
Throws a GetOptException if some problems are found.
Specification of a collection of options, described by type a.
Constructors
| OptSpec | |
Fields
| |
Describe an option.
Constructors
| Option | |
Fields
| |
type OptSetter a = a -> Either String a Source #
Manipulate options of type a, with support for errors.
Describe an option argumnet.
Constructors
| NoArg (OptSetter a) | This option does not take an argument. |
| ReqArg String (String -> OptSetter a) | This optoin has a required arugment. The string describes the type of the argument. |
| OptArg String (Maybe String -> OptSetter a) | This optoin has an optional arugment. The string describes the type of the argument. |
data GetOptException Source #
Constructors
| GetOptException [String] |
Instances
| Show GetOptException Source # | |
Defined in SimpleGetOpt Methods showsPrec :: Int -> GetOptException -> ShowS # show :: GetOptException -> String # showList :: [GetOptException] -> ShowS # | |
| Exception GetOptException Source # | |
Defined in SimpleGetOpt Methods toException :: GetOptException -> SomeException # | |
Information and error reporting.
reportUsageError :: OptSpec a -> [String] -> IO b Source #
Print the given messages on stderr and show the program's usage info,
then exit.
usageString :: OptSpec a -> String Source #
A string descibing the options.