Safe Haskell | None |
---|---|
Language | Haskell2010 |
getopt-generics
tries to make it very simple to create command line
argument parsers. An introductory example can be found in the
README.
- getArguments :: forall a. (Generic a, HasDatatypeInfo a, All2 Option (Code a)) => IO a
- modifiedGetArguments :: forall a. (Generic a, HasDatatypeInfo a, All2 Option (Code a)) => [Modifier] -> IO a
- parseArguments :: forall a. (Generic a, HasDatatypeInfo a, All2 Option (Code a)) => String -> [Modifier] -> [String] -> Result a
- data Result a
- = Success a
- | Errors [String]
- | OutputAndExit String
- data Modifier
- deriveShortOptions :: (HasDatatypeInfo a, SingI (Code a)) => Proxy a -> [Modifier]
- class Option a where
- argumentType :: Proxy a -> String
- parseArgument :: String -> Maybe a
- _toOption :: ArgDescr (FieldState a)
- _emptyOption :: String -> FieldState a
- _accumulate :: a -> a -> a
IO API
getArguments :: forall a. (Generic a, HasDatatypeInfo a, All2 Option (Code a)) => IO a Source
Parses command line arguments (gotten from withArgs
) and returns the
parsed value. This function should be enough for simple use-cases.
May throw the following exceptions:
in case of invalid options. Error messages are written toExitFailure
1stderr
.
in caseExitSuccess
--help
is given. (
behaves like a normal exception, except that -- if uncaught -- the process will exit with exit-codeExitSuccess
0
.) Help output is written tostdout
.
modifiedGetArguments :: forall a. (Generic a, HasDatatypeInfo a, All2 Option (Code a)) => [Modifier] -> IO a Source
Like getArguments
but allows you to pass in Modifier
s.
Pure API
parseArguments :: forall a. (Generic a, HasDatatypeInfo a, All2 Option (Code a)) => String -> [Modifier] -> [String] -> Result a Source
Pure variant of getArguments
. Also allows to declare Modifier
s.
Does not throw any exceptions.
Type to wrap results from the pure parsing functions.
Success a | The CLI was used correctly and a value of type |
Errors [String] | The CLI was used incorrectly. The It can also happen that the data type you're trying to use isn't supported. See the README for details. |
OutputAndExit String | The CLI was used with |
Customizing the CLI
Modifier
s can be used to customize the command line parser.
AddShortOption String Char |
|
RenameOption String String |
|
deriveShortOptions :: (HasDatatypeInfo a, SingI (Code a)) => Proxy a -> [Modifier] Source
Derives AddShortOption
s for all fields of the datatype that start with a
unique character.
Available Field Types
Type class for all allowed field types.
Implementing custom instances to allow different types is possible. In the
easiest case you just implement argumentType
and parseArgument
(the
minimal complete definition).
(Unfortunately implementing instances for lists or Maybe
s of custom types
is not very straightforward.)
argumentType :: Proxy a -> String Source
Name of the argument type, e.g. "bool" or "integer".
parseArgument :: String -> Maybe a Source
_toOption :: ArgDescr (FieldState a) Source
This is meant to be an internal function.
_emptyOption :: String -> FieldState a Source
This is meant to be an internal function.
_accumulate :: a -> a -> a Source
This is meant to be an internal function.