MyNixOS website logo
Description

Hydrogen Command Line Arguments Parser.

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.

Metadata

Version

0.17

License

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows