The docopt package

[Tags: library, mit]

Docopt parses command-line interface usage text that adheres to a familiar syntax, and from it builds a command-line argument parser that will ensure your program is invoked correctly with the available options specified in the usage text. This allows the developer to write a usage text and get an argument parser for free.


[Skip to ReadMe]

Properties

Versions0.6.0, 0.6.0.1, 0.6.0.2, 0.7.0.0, 0.7.0.1, 0.7.0.2
Change logCHANGELOG.md
Dependenciesbase (==4.*), containers, parsec (==3.1.*), template-haskell (>=2.7 && <3.0), th-lift (>=0.7 && <1.0) [details]
LicenseMIT
Copyright(c) 2013-2015 Ryan Artecona
AuthorRyan Artecona
Maintainerryanartecona@gmail.com
CategoryConsole
Home pagehttps://github.com/docopt/docopt.hs
Bug trackerhttps://github.com/docopt/docopt.hs/issues
UploadedMon May 18 00:57:44 UTC 2015 by ryanartecona
DistributionsNixOS:0.7.0.2
Downloads806 total (63 in last 30 days)
Votes
0 []
StatusDocs available [build log]
Last success reported on 2015-05-19 [all 1 reports]

Modules

[Index]

Flags

NameDescriptionDefault
template-haskellBuild with QuasiQuoter usage parsers, which requires Template HaskellEnabled

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Maintainers' corner

For package maintainers and hackage trustees

Readme for docopt-0.7.0.2

Docopt.hs

A Haskell port of python's docopt.


Want a command-line interface without building a parser?

How about writing your help text first, and getting a parser for free!

Save your help text to a file (i.e. USAGE.txt):

Usage:
  myprog cat <file>
  myprog echo [--caps] <string>

Options:
  -c, --caps    Caps-lock the echoed argument

Then, in your Myprog.hs:

{-# LANGUAGE QuasiQuotes #-}
import Control.Monad (when)
import Data.Char (toUpper)
import System.Environment (getArgs)
import System.Console.Docopt

patterns :: Docopt
patterns = [docoptFile|USAGE.txt|]

getArgOrExit = getArgOrExitWith patterns

main = do
  args <- parseArgsOrExit patterns =<< getArgs

  when (args `isPresent` (command "cat")) $ do
    file <- args `getArgOrExit` (argument "file")
    putStr =<< readFile file

  when (args `isPresent` (command "echo")) $ do
    let charTransform = if args `isPresent` (longOption "caps")
                          then toUpper
                          else id
    string <- args `getArgOrExit` (argument "string")
    putStrLn $ map charTransform string

That's it! No Template Haskell, no unreadable syntax, no learning yet another finicky API. Write the usage patterns you support, and docopt builds the appropriate option parser for you (internally using parsec). If your user invokes your program correctly, you query for the arguments they provided. If the arguments provided do not match a supported usage pattern, you guessed it: docopt automatically prints the help text and exits!

Installation

cabal sandbox init
cabal install docopt

API Reference

See the package on hackage

Help text format

Docopt only cares about 2 parts of your help text:

Usage Patterns

Option descriptions

Option descriptions establish:

Rules:


Differences from reference python implementation: