No project description provided
Project description
This is a very simple Python package that allows one to create argparse’s subcommands via function decorators.
Usage
Create a SubDec object:
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:
@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.
In the example below we define a second subcommand, which will be named foo-bar:
@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:
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()
sd.create_parsers(subparsers)
The following is example of how we would call the subcommands:
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:
pip install argparse-subdec
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
Hashes for argparse_subdec-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c54fec3af04d660624e300c87191b8507710a8b23e54261c1121e5698265bd1 |
|
MD5 | c67ff42b973f6bdb58f5af5251cd8619 |
|
BLAKE2b-256 | 07d8fc65de46e79464b7df47843e0d7af4ca2af92c59502afe69221254ff08c9 |