An open API service indexing awesome lists of open source software.

https://github.com/guludo/python-argparse-subdec

Create argparse subcommands with decorators.
https://github.com/guludo/python-argparse-subdec

argparse decorator python python3

Last synced: 25 days ago
JSON representation

Create argparse subcommands with decorators.

Awesome Lists containing this project

README

          

python-argparse-subdec
######################

This is a very simple Python package that allows one to create argparse_'s
subcommands via function decorators.

Usage
=====

Create a ``SubDec`` object:

.. code:: python

import argparse_subdec

# Create the object to collect subcommands via decorators
sd = argparse_subdec.SubDec()

Now, we can define our subcommands. We define a subcommand by decorating a
function with method calls to ``sd``, like below:

.. code:: python

@sd.add_argument('--option', default='123')
@sd.add_argument('--another')
def foo(args):
print(f'foo subcommand: --option={args.option!r} and --another={args.another!r}')

You can use any method name that you would use in a subparser. In our example
above, we used ``add_argument``, probably most commonly used. When creating
the subparsers, ``sd`` will replay those calls to the created subparser for
the ``foo`` subcommand.

Since using ``sd.add_argument()`` is so common, there is an alias to save some
keystrokes. The example below is the equivalent of the above using the alias:

.. code:: python

@sd.arg('--option', default='123')
@sd.arg('--another')
def foo(args):
print(f'foo subcommand: --option={args.option!r} and --another={args.another!r}')

In the example below we define a second subcommand, which will be named
``foo-bar``:

.. code:: python

@sd.cmd(prefix_chars=':')
@sd.add_argument('positional_arg', type=int)
def foo_bar(args):
print(f'foo-bar subcommand: {args.positional_arg!r}')

Note that we use a special decorator, ``sd.cmd``, which makes ``sd`` pass all
of its arguments down to ``add_parser()`` when creating the subparser for
``foo-bar``.

Once our subcommands are defined, we must call ``sd.create_parsers()`` in
order to effectively create the subparsers:

.. code:: python

parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
sd.create_parsers(subparsers)

The following is example of how we would call the subcommands:

.. code:: python

args = parser.parse_args(['foo', '--another', 'hello'])
args.fn(args) # The subcommand function is assigned to ``args.fn`` by default
# Outputs: foo subcommand: --option='123' and --another='hello'

For more details about the API, check out the subdec_ module.

Install
=======

Via ``pip``:

.. code:: bash

pip install argparse-subdec

.. _argparse: https://docs.python.org/3/library/argparse.html
.. _subdec: argparse_subdec/subdec.py