================
|PyPI| |PyPI| |PyPI version| |GitHub release| |Build Status| |codecov|
|GitHub license|
Parser with automatic creation of parsers and subparsers for paths.
Installation
------------
Supported versions of python: **``3.3+``** (because of
inspect.Signature, which was introduced in python 3.3)
.. code:: shell
pip install argparse-autogen
Usage
-----
``argparse_autogen.EndpointParser`` is intended to replace basic
``argparse.ArgumentParser``. It extends subparsers creation logic, and
adds a new special method ``add_endpoint``.
Simple example:
.. code:: python
import argparse_autogen
class MyCli():
def do_stuff(self, target, force=False):
"""
This does cool stuff!
:param str target: Target to execute a cool stuff
:param bool force: Force doing cool stuff
"""
print(target, force)
cli = MyCli()
parser = argparse_autogen.EndpointParser()
parser.add_endpoint('do_stuff', cli.do_stuff)
parser.parse_and_call(['do_stuff', 'my target']) # this will print "my target false"
parser.parse_and_call(['do_stuff', '--force', 'my target']) # this will print "my target true"
``add_endpoint`` method is clever enough to parse methods docstring and
add corresponding helps in arguments. For example,
``parser.parse_args(['do_stuff', '--help'])`` in above example will show
something like
::
usage: example.py do_stuff [-h] [--force]
This does cool stuff!
optional arguments:
-h, --help show this help message and exit
--force Force doing cool stuff
This magic is done by ``argparse_autogen.autospec`` function. It
introspects function signature, and adds corresponding argparse
arguments to parser. ``**kwargs`` are supported and can be passed as
``[key=value [key=value ...]]``. You can override argument settings by
passing ``argument_overrides`` option to ``add_endpoint``. This must be
a ``dict[str, dict]`` where keys are parameter name, and values are
parameters to override defaults passed to ``parser.add_argument``
More endpoint examples
----------------------
Nested class and complex paths:
.. code:: python
import argparse_autogen
class MyCli():
def __init__(self):
self.users = self.Users()
self.groups = self.Groups()
class Users():
def get(self, user_id): pass
def list(self, **filter): pass
def set_roles(self, user_id, *role): pass
def update(self, user_id, **fields): pass
class Groups():
def get(self, group_id): pass
cli = MyCli()
parser = argparse_autogen.EndpointParser()
parser.add_endpoint('users.get', cli.users.get, argument_overrides={'user_id': {'help': 'Users id'}})
parser.add_endpoint('users.list', cli.users.list)
parser.add_endpoint(cli.users.update)
# this will use __qualname__ of update func as path, lowercased and trailing and ending underscores removed.
# The first item of qualname is skipped, so it would be `users.update`, not `mycli.users.update`
# Alternatively, you can use autogeneration of paths and endpoints:
# parser.generate_endpoints(cli.users, root_path='users', endpoint_kwargs={'users.get': {'argument_overrides': {'user_id': {'help': 'Users id'}}}})
# Will create endpoints from class methods.
groups_get_parser = parser.add_endpoint('groups get', cli.groups.get, autospec=False)
groups_get_parser.add_argument('group_id', help='Group id')
users_parser = parser.get_endpoint_parser('users')
users_parser.description = 'Users operations'
parser.parse_and_call()
History
-------
1.2 (2017-03-01)
~~~~~~~~~~~~~~~~
- Ability to automatically generate path from func's qualname
1.1 (2017-02-28)
~~~~~~~~~~~~~~~~
- Filter args from func signature in call method #1
1.0 (2017-02-26)
~~~~~~~~~~~~~~~~
- First release
0.1 (2017-02-25)
~~~~~~~~~~~~~~~~
- Initial commit
.. |PyPI| image:: https://img.shields.io/pypi/status/argparse-autogen.svg
:target: https://github.com/sashgorokhov/argparse-autogen
.. |PyPI| image:: https://img.shields.io/pypi/pyversions/argparse-autogen.svg
:target: https://github.com/sashgorokhov/argparse-autogen
.. |PyPI version| image:: https://badge.fury.io/py/argparse-autogen.svg
:target: https://badge.fury.io/py/argparse-autogen
.. |GitHub release| image:: https://img.shields.io/github/release/sashgorokhov/argparse-autogen.svg
:target: https://github.com/sashgorokhov/argparse-autogen
.. |Build Status| image:: https://travis-ci.org/sashgorokhov/argparse-autogen.svg?branch=master
:target: https://travis-ci.org/sashgorokhov/argparse-autogen
.. |codecov| image:: https://codecov.io/gh/sashgorokhov/argparse-autogen/branch/master/graph/badge.svg
:target: https://codecov.io/gh/sashgorokhov/argparse-autogen
.. |GitHub license| image:: https://img.shields.io/badge/license-MIT-blue.svg
:target: https://raw.githubusercontent.com/sashgorokhov/argparse-autogen/master/LICENSE
Metadata
Release files for argparse-autogen 1.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| argparse-autogen-1.4.tar.gz | 5.8 kB | Without provenance | Details for argparse-autogen-1.4.tar.gz |
Release files / argparse-autogen-1.4.tar.gz
| Download URL | argparse-autogen-1.4.tar.gz |
|---|---|
| Size | 5.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
46f83e11f0e3fb0456b3561c0e5acc6cdc0ae931c92dbc9bd0bb0878a07c30e5
|
|
BLAKE2b-256 checksum How to use checksums |
1007cc6ca682a7919b57cd42e73bba8052bce4de110bf586c2d18843ddb9782b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for argparse-autogen 1.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.