doc to argparse driven by docopt
Project description
doc to argparse driven by docopt
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.
'''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=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
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/.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file argopt-0.3.3.tar.gz
.
File metadata
- Download URL: argopt-0.3.3.tar.gz
- Upload date:
- Size: 18.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ead4817636440e09fb6eb064b47448329ab9394436f7f06e5500523b2d97ff88 |
|
MD5 | bde0e53b3330c5179bedbb9102b91ba7 |
|
BLAKE2b-256 | be912a0f80c9f2cce67c30987c8108729a08b33d6d18ebebab997f25a11d02b4 |
File details
Details for the file argopt-0.3.3-py2.py3-none-any.whl
.
File metadata
- Download URL: argopt-0.3.3-py2.py3-none-any.whl
- Upload date:
- Size: 16.4 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f7ee0aee43807ecb051e333eca2544fbb4226334d54e21364a3a45fd8a78729 |
|
MD5 | fd9268c9c6557c2edbb22a02791b2daa |
|
BLAKE2b-256 | 7938f27fcbb7881bd43424193f13f232d66a545a6791d7721b5caea344716776 |