getopt-generics-0.4.1: Simple command line argument parsing

Safe HaskellNone




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 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:

  • ExitFailure 1 in case of invalid options. Error messages are written to stderr.
  • ExitSuccess in case --help is given. (ExitSuccess behaves like a normal exception, except that -- if uncaught -- the process will exit with exit-code 0.) Help output is written to stdout.

modifiedGetArguments :: forall a. (Generic a, HasDatatypeInfo a, All2 Option (Code a)) => [Modifier] -> IO a Source

Like getArguments but allows you to pass in Modifiers.

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 Modifiers.

Does not throw any exceptions.

data Result a Source

Type to wrap results from the pure parsing functions.


Success a

The CLI was used correctly and a value of type a was successfully constructed.

Errors [String]

The CLI was used incorrectly. The Result contains a list of error messages.

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 --help. The Result contains the help message.


Customizing the CLI

data Modifier Source

Modifiers can be used to customize the command line parser.


AddShortOption String Char

AddShortOption fieldName c adds the Char c as a short option for the field addressed by fieldName.

RenameOption String String

RenameOption fieldName customName renames the option generated through the fieldName by customName.

UseForPositionalArguments String

UseForPositionalArguments fieldName fills the field addressed by fieldName with the positional arguments (i.e. arguments that don't correspond to a flag). The field has to have type [String].

deriveShortOptions :: (HasDatatypeInfo a, SingI (Code a)) => Proxy a -> [Modifier] Source

Derives AddShortOptions for all fields of the datatype that start with a unique character.

Available Field Types

class Typeable a => Option a where Source

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 Maybes of custom types is not very straightforward.)

Minimal complete definition

argumentType, parseArgument


argumentType :: Proxy a -> String Source

Name of the argument type, e.g. "bool" or "integer".

parseArgument :: String -> Maybe a Source

Parses a String into an argument. Returns Nothing on parse errors.

_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.