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.


Installation

Latest pypi stable release

PyPI-Status

pip install argopt

Latest development release on github

GitHub-Status

Pull and install in the current directory:

pip install -e git+https://github.com/casperdcl/argopt.git@master#egg=argopt

Changelog

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

Usage

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:
    test.py [options] <x> [<y>...]

Arguments:
    <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:
    print(args)
else:
    print(args.x)

For comparison, the docopt equivalent would be:

'''Example programme description.

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

Arguments:
    <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"]:
    print(args)
else:
    print(args["<x>"])

Advanced usage and examples

See the examples folder.

Documentation

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

  Parameters
  ----------
  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`

  Returns
  -------
  out  : argparser object (default: argparse.ArgumentParser)

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

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

  TODO
  ----
  add_argument_group
  add_mutually_exclusive_group
  (better) subparser support
  (docopt extension) action choices
  (docopt extension) action count
  """

Contributions

To run the testing suite please make sure tox (https://testrun.org/tox/latest/) is installed, then type tox from the command line.

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

$ python setup.py make alltests

To see all options, run:

$ python setup.py make

Licence

Open Source (OSI approved): LICENCE

Copyright (c) 2016-7 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 https://mozilla.org/MPL/2.0/.

Authors

OpenHub-Status

Project details


Download files

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

Source Distribution

argopt-0.3.5.tar.gz (18.9 kB view details)

Uploaded Source

Built Distribution

argopt-0.3.5-py2.py3-none-any.whl (16.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file argopt-0.3.5.tar.gz.

File metadata

  • Download URL: argopt-0.3.5.tar.gz
  • Upload date:
  • Size: 18.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for argopt-0.3.5.tar.gz
Algorithm Hash digest
SHA256 19469f16d510ef7e2d6fd6347677cb27b828eaeac532b7a4ff8dd2e9dddb5338
MD5 ecc2dc384f6a1fc82841803f4e970e0d
BLAKE2b-256 66312fa1198aebc32b8c91e1225baff8fd5ef7f7cb1a398baed61ff8febd4e6e

See more details on using hashes here.

File details

Details for the file argopt-0.3.5-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for argopt-0.3.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4e99ac8cb03dd70607a7aee1275595dc438f78a9c5b84d7354148a25b0b82de9
MD5 278cb6b6abee1791757b8cccf65f2f66
BLAKE2b-256 83d14a11ff18bb8c95c9e7611b30de32ea40f7d092a32df833c4f9b621c4f23e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page