docopt: A command-line interface parser that will make you smile

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

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

Versions 0.6.0, 0.6.0.1, 0.6.0.2, 0.7.0.0, 0.7.0.1, 0.7.0.2, 0.7.0.3, 0.7.0.4, 0.7.0.5, 0.7.0.6, 0.7.0.7, 0.7.0.8, 0.7.0.8
Change log CHANGELOG.md
Dependencies base (>=4.9 && <5.0), containers (>=0.6.2 && <0.8), parsec (>=3.1.14 && <3.2), template-haskell (>=2.11.0 && <2.22) [details]
License MIT
Copyright (c) 2013-2015 Ryan Artecona
Author Ryan Artecona
Maintainer QBayLogic B.V. <devops@qbaylogic.com>
Category Console
Home page https://github.com/docopt/docopt.hs
Bug tracker https://github.com/docopt/docopt.hs/issues
Source repo head: git clone https://github.com/docopt/docopt.hs.git
this: git clone https://github.com/docopt/docopt.hs.git(tag v0.7.0.8)
Uploaded by PeterLebbing at 2024-02-26T09:11:46Z

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for docopt-0.7.0.8

[back to package description]

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: