haskell-options: An easy-to-use command-line option parser for Haskell

The options package lets library and application developers easily work with command-line options.

Code: https://john-millikin.com/code/haskell-options (GitHub mirror)

Examples

The following example is a full program that can accept two options, --message and --quiet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import Control.Applicative
import Options

data MainOptions = MainOptions
	{ optMessage :: String
	, optQuiet :: Bool
	}

instance Options MainOptions where
	defineOptions = pure MainOptions
		<*> simpleOption "message" "Hello world!"
		    "A message to show the user."
		<*> simpleOption "quiet" False
		    "Whether to be quiet."

main :: IO ()
main = runCommand $ \opts args -> do
	if optQuiet opts
		then return ()
		else putStrLn (optMessage opts)
1
2
3
4
5
6
$ ./hello
Hello world!
$ ./hello --message='ciao mondo'
ciao mondo
$ ./hello --quiet
$

In addition, this library will automatically create documentation options such as --help and --help-all:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
$ ./hello --help
Help Options:
  -h, --help
    Show option summary.
  --help-all
    Show all help options.

Application Options:
  --message :: text
    A message to show the user.
    default: "Hello world!"
  --quiet :: bool
    Whether to be quiet.
    default: false