hydrogen-cli-args: Hydrogen Command Line Arguments Parser

[ language, library, mit ] [ Propose Tags ] [ Report a vulnerability ]

Modules

[Index]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.9, 0.10, 0.11, 0.12, 0.14, 0.17 (info)
Change log CHANGELOG.md
Dependencies base (>=4.7 && <5), containers (>=0.5 && <0.6), hydrogen-multimap (==0.1), hydrogen-prelude (==0.17) [details]
License MIT
Author Julian Fleischer
Maintainer julian@scravy.de
Category Language
Home page https://scravy.de/hydrogen-cli-args/
Source repo head: git clone https://github.com/scravy/hydrogen-cli-args
Uploaded by JulianFleischer at 2015-03-22T11:43:23Z
Distributions
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 3282 total (10 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2015-03-22 [all 1 reports]

Readme for hydrogen-cli-args-0.17

[back to package description]

hydrogen-cli-args

Build Status

An easy to use command line arguments parser.

main = do

    (options, switches, args) <- getOpts [
        'V' ~: switch "version"
      , 'h' ~: switch "help"
      ,        optarg "inFile"
      ,        optarg "outFile"
      , 'v' ~: switch "verbose"
      , 'f' ~: optarg "flag"
      , 'D' ~: optarg "config"
      ]

    when (switches ? "version") $ println "Example v1.0"

    when (switches ? "help") showManual

    let flags  = options ! "flag"
        config = options ! "config"

    ...

This program will accept arguments like that:

-h --version -DHELLLO --config=SOME_CONFIG -f flagvalue
  • options in the above example is a MultiMap String String
  • switches is a Set String
  • args contains the remaining arguments as a [String].

If an optional argument, defined by optarg is given (by its short alias or by its long name) it will show up in the options MultiMap. Note that you can check for a key beings set with (?) and retrieve all associated values with (!). Also note that (!) will always return a list, but possibly en empty one (if no option was given).

Long options can be given as --key value or as --key=value.

Short options can be given as -D value as well as -Dvalue.

If a switch, defined by switch is given, it will show up in the switches Set. You can query for whether a switch is set or not with (?).

Switches can be combined, i.e. -hv is the same as -h -v.

If -- is supplied as an argument, no options are evaluated beyond this point. Any unknown or malformed option (-x, --xxxx) will be treated as an argument.

API

Define options

switch :: String -> Option

Defines a command line switch with the given long name.

optarg :: String -> Option

Defines a command line option with the given long name.

alias ~: option :: Char -> Option -> Option

Defines a shorthand for the given option.

option ~? check :: Option -> (String -> Bool) -> Option

Defines a check which the optional arguments' value has to pass.

option ~= pattern :: Option -> String -> Option

Defines a pattern which the optional arguments' value must match.

Get options and arguments

type OptArgs = (MultiMap String String, Set String, [String])

getOpts :: [Options] -> IO OptArgs

getOpts' :: [Options] -> [String] -> OptArgs

Query MultiMaps / Sets

Part of hydrogen-prelude.