argparse wrapper around decorations
Project description
argparse_decoration
A argparse wrapper through decorations
30 sec tutorial
Given test.py
with:
#!/bin/python3
from argparse_decorations import init, Command, parse_and_run
init()
@Command('mycommand')
def handler():
print('Hello!')
parse_and_run()
then ./test.py
produces:
usage: test.py [-h] {mycommand} ...
Help message
positional arguments:
{mycommand}
options:
-h, --help show this help message and exit
and ./test.py mycommand
:
Hello!
1 min tutorial
The program:
from argparse_decorations import init, Command, SubCommand, Argument, parse_and_run
init()
@Command('mycommand')
@SubCommand('add')
@Argument('a', type=int)
@Argument('b', type=int)
def add_handler_but_could_be_any_identifier(a, b):
print(a + b)
parse_and_run()
when called ./test.py mycommand add 1 2
produces:
3
@Command
and @SubCommand
will make a call to ArgumentParser.add_parser()
and @Argument
will call ArgumentParser.add_argument
These calls are made 'as-is' passing every *args
and **kwargs
passed on decorations to commands
A complete example whose explore all possibilities:
@Command('math')
@SubCommand('add')
@Argument('valueA', type=int)
@Argument('valueB', type=int)
def add_handler(valueA, valueB):
print('Adding args ' + str(valueA) + ' and ' + str(valueB))
print(valueA + valueB)
@Command('math')
@SubCommand('sub')
@Argument('valueA', type=int)
@Argument('valueB', type=int)
def subtract_handler(valueA, valueB):
print('Subtracting args ' + str(valueA) + ' and ' + str(valueB))
print(valueA - valueB)
@Command('math')
@SubCommand('mul')
@Argument('valueA', type=int)
@Argument('valueB', type=int)
def subtract_handler(valueA, valueB):
print('Multiplying args ' + str(valueA) + ' and ' + str(valueB))
print(valueA * valueB)
@Command('math')
@SubCommand('pi')
def pi():
print('3.1415926535979')
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file argparse_decorations-0.0.8.tar.gz
.
File metadata
- Download URL: argparse_decorations-0.0.8.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b3c4fa1ceeaddb9777968c6f5d4e51ae8a5480b18ad3de5bccab2fd46ce60ec |
|
MD5 | 3be6082ccbb72f014b0f29faececfebf |
|
BLAKE2b-256 | f67425c50b09e1f575c57e46bbafdb2af0c7c0ddce6e1775065d6680cd568f2a |