Skip to main content

doc to argparse driven by docopt

Project description

doc to argparse driven by docopt

PyPi Status Build Status Coverage Status Branch Coverage 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

pip install argopt

Latest development release on github

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.

from argopt import argopt


def main(doc):
    parser = argopt(doc, version='0.1.2-3.4')
    # parser.print_help()

    args = parser.parse_args()
    print (args)


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

Usage:
    test.py [-h | 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.
'''
    main(doc)

Advanced usage and examples

See the examples folder.

Documentation

def argopt(doc='', argparser=argparse.ArgumentParser, **_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]
  _kwargs  : any `argparser` initialiser arguments


  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

OSI approved.

Copyright (c) 2016 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

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 Distributions

argopt-0.3.1.zip (24.5 kB view details)

Uploaded Source

argopt-0.3.1.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

argopt-0.3.1-py2.py3-none-any.whl (15.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file argopt-0.3.1.zip.

File metadata

  • Download URL: argopt-0.3.1.zip
  • Upload date:
  • Size: 24.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for argopt-0.3.1.zip
Algorithm Hash digest
SHA256 b0a23226b7fe60ecdaa185d7c945eb3e0235d06ef42e22bf5f0aaaf6e4429e30
MD5 b5d66e01f0d93fa1b4db36796282ef25
BLAKE2b-256 9bbefa7323c7356c031ad42186bc95fff82f93fa2e17f2d039b546239aa46d13

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for argopt-0.3.1.tar.gz
Algorithm Hash digest
SHA256 4a5ea5d88949397b728a6d62503e30f7bdc94bd33836cce94f867ab3365fd028
MD5 4ace3091eb421da2c783ecb4d4114c4f
BLAKE2b-256 71bbfbdb5c58502d8e475070f7679297aa0646415137d09487ab05f7db238d00

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for argopt-0.3.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 efe81219075f532cb42d0f853d5b1d9c58ed8ddb9ea47dde9b16801cd97df1ad
MD5 7a2d85ae193c4f7168225508fad77216
BLAKE2b-256 201aed52d1d822177c4564426bd742eeaa8f0cb25e2de4be299551e209d6b4f3

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