simple-get-opt-0.5: A simple library for processing command-line options.
Safe HaskellSafe-Inferred
LanguageHaskell2010

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
  { progDescription = [ "A useful utility." ]

  , 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 }

  , progArgOrder = Permute
  }

Here is what the usage information looks like:

*Main> dumpUsage options
A useful utility.

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

Basic functionality

getOpts :: a -> 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 :: a -> 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.

getOptsFrom :: a -> OptSpec a -> [String] -> Either GetOptException a Source #

Process the given command line options according to the given spec. The options will be permuted to get flags. Returns errors on the Left.

data OptSpec a Source #

Specification of a collection of options, described by type a.

Constructors

OptSpec 

Fields

data OptDescr a Source #

Describe an option.

type OptSetter a = a -> Either String a Source #

Manipulate options of type a, with support for errors.

data ArgDescr a Source #

Describe an option argumnet.

Constructors

NoArg (OptSetter a)

This option does not take an argument.

ReqArg String (String -> OptSetter a)

This option has a required argument. The string describes the type of the argument.

OptArg String (Maybe String -> OptSetter a)

This option has an optional argument. The string describes the type of the argument.

data ArgOrder a #

What to do with options following non-options

Constructors

RequireOrder

no option processing after first non-option

Permute

freely intersperse options and non-options

ReturnInOrder (String -> a)

wrap non-options into options

Instances

Instances details
Functor ArgOrder

Since: base-4.6.0.0

Instance details

Defined in System.Console.GetOpt

Methods

fmap :: (a -> b) -> ArgOrder a -> ArgOrder b #

(<$) :: a -> ArgOrder b -> ArgOrder a #

optSpec :: OptSpec a Source #

A default empty specification. The default argument order is Permute.

Information and error reporting.

dumpUsage :: OptSpec a -> IO () Source #

Show the program's usage information on stderr.

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.

Direct interaction with GetOpt