Skip to main content

doc to argparse driven by docopt

Project description

doc to argparse driven by docopt

PyPI-Status PyPI-Versions

Build-Status Coverage-Status Branch-Coverage-Status Codacy-Grade

LICENCE Donate OpenHub-Status

Define your command line interface (CLI) from a docstring (rather than the other way around). Because it’s easy. It’s quick. Painless. Then focus on what’s actually important - using the arguments in the rest of your program.

The problem is that this is not always flexible. Still need all the features of argparse? Now have the best of both worlds… all the extension such as argcomplete or Gooey but with the simple syntax of docopt.


Latest PyPI stable release


pip install argopt

Latest development release on github


Pull and install in the current directory:

pip install -e git+


The list of all changes is available either on Github’s Releases or on crawlers such as


Standard docopt docstring syntax applies. Additionally, some improvements and enhancements are supported, such as type checking and default positional arguments.

'''Example programme description.
You should be able to do
    args = argopt(__doc__).parse_args()
instead of
    args = docopt(__doc__)

Usage: [options] <x> [<y>...]

    <x>                   A file.
    --anarg=<a>           Description here [default: 1e3:int].
    -p PAT, --patts PAT   Or [default: None:file].
    --bar=<b>             Another [default: something] should
                          auto-wrap something in quotes and assume str.
    -f, --force           Force.
from argopt import argopt
__version__ = "0.1.2-3.4"

parser = argopt(__doc__, version=__version__).parse_args()
if args.force:

For comparison, the docopt equivalent would be:

'''Example programme description.

Usage: [options] <x> [<y>...]

    <x>                   A file.
    --anarg=<a>           int, Description here [default: 1e3].
    -p PAT, --patts PAT   file, Or (default: None).
    --bar=<b>             str, Another [default: something] should
                          assume str like everything else.
    -f, --force           Force.
    -h, --help            Show this help message and exit.
    -v, --version         Show program's version number and exit.

from docopt import docopt
__version__ = "0.1.2-3.4"

args = docopt(__doc__, version=__version__)
args["--anarg"] = int(eval(args["--anarg"]))
if args["--patts"]:
    args["--patts"] = open(args["--patts"])
if args["--force"]:


def argopt(doc='', argparser=ArgumentParser,
           logLevel=logging.NOTSET, **_kwargs):
  Note that `docopt` supports neither type specifiers nor default
  positional arguments. We support both here.

  doc  : docopt compatible, with optional type specifiers
      [default: '':str]
  argparser  : Argument parser class [default: argparse.ArgumentParser]
  version  : Version string [default: None:str]
  formatter_class  : [default: argparse.RawDescriptionHelpFormatter]
  logLevel  : [default: logging.NOTSET]
  _kwargs  : any `argparser` initialiser arguments
      N.B.: `prog`, `description`, and `epilog` are automatically
      inferred if not `None`

  out  : argparser object (default: argparse.ArgumentParser)

  Extension syntax example: [default: 1e3:int].

  You should be able to do
      parser = argopt(__doc__)
      args   = parser.parse_args()
  instead of
      args = docopt(__doc__)

  (better) subparser support
  (docopt extension) action choices
  (docopt extension) action count


To run the testing suite please make sure tox ( is installed, then type tox from the command line.

Where tox is unavailable, a Makefile-like setup is provided with the following command:

$ python make alltests

To see all options, run:

$ python make


Open Source (OSI approved): LICENCE

Copyright (c) 2016-8 Casper da Costa-Luis.

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at




Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for argopt, version 0.6.1
Filename, size File type Python version Upload date Hashes
Filename, size argopt-0.6.1-py2.py3-none-any.whl (13.9 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size argopt-0.6.1.tar.gz (19.5 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page