Skip to main content

doc to argparse driven by docopt

Project description

doc to argparse driven by docopt

Py-Versions PyPI Conda-Forge

Build-Status Coverage-Status Branch-Coverage-Status Codacy-Grade Libraries-Rank PyPI-Downloads

LICENCE OpenHub-Status Gift-Casper

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 PyPI-Downloads Libraries-Dependents

pip install argopt

Latest development release on GitHub

GitHub-Status GitHub-Stars GitHub-Commits GitHub-Forks GitHub-Updated

Pull and install:

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

Latest Conda release

Conda-Forge

conda install -c conda-forge argopt

Changelog

The list of all changes is available on the Releases page: GitHub-Status.

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

Py-Versions README-Hits

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

GitHub-Commits GitHub-Issues GitHub-PRs OpenHub-Status

All source code is hosted on GitHub. Contributions are welcome.

LICENCE

Open Source (OSI approved): LICENCE

Authors

OpenHub-Status

We are grateful for all GitHub-Contributions.

README-Hits

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.6.2.tar.gz (22.5 kB view details)

Uploaded Source

Built Distribution

argopt-0.6.2-py2.py3-none-any.whl (13.8 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: argopt-0.6.2.tar.gz
  • Upload date:
  • Size: 22.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.7.9

File hashes

Hashes for argopt-0.6.2.tar.gz
Algorithm Hash digest
SHA256 55913a15099d4cecec5493e1b8976a90bf48a0ad3d174d42a44ba487ac385abf
MD5 5a1db2e95b5a7880573c1fc9d037128b
BLAKE2b-256 c787e8a8ba40d1a606eb6a021b3918f751a3caf1c92ea9a9f134fbbfe4fad29b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: argopt-0.6.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 13.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.7.9

File hashes

Hashes for argopt-0.6.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 53f8a79c944a9652ca55e7f87aa720e1324bdc28cf25914242a1780a1e6e4f83
MD5 127edea83e6c3cf1b2815c222d139677
BLAKE2b-256 3c55e82eada23c2281466b0363149adfbc54d05d7aba8f7b8d7ce05921e12266

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