Argparse-based way of CLI argument handling boosted by YAML reusable configurations
Project description
configurable_argparse - Argparse-based way of CLI argument handling boosted by YAML reusable configurations
When writing a command-line Python script that needs to take many arguments, usability issues may arise:
- Tediousness: Especially if used often with mostly the same arguments
- Incompleteness: It's easy to miss arguments
- RTFM fatigue: having to often refer to documentation takes away from actually using the software
- Need to write execution scripts: Writing scripts with arguments is sensible but locks users into static configurations
Configurable argparse aims at offloading the "noise" (often used argument configurations) into "recipes" that can be reused, while retaining the ability to override what needs to be adapted (the "signal"), therefore giving the user an experience that allows to efficiently use the program.
Example
This simple example is there show how to use configurable argparse and how it makes using
With argparse
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument(
'-t', '--nb_threads',
type: int, default=4,
help="Number of threads to use"
)
...
exclusive_group = parser.add_mutually_exclusive_group()
exclusive_group.add_argument(
'--verbose',
action='store_true',
help='Show all messages'
)
exclusive_group.add_argument(
'--quiet',
action='store_true',
help='Show no messages'
)
parser.parse_args()
...
def main():
args = get_args()
Usage example:
- Every time:
python example_program.py -t 4 --use_library=libabc --ignore=E123,E321 --add_src=/homes/user/documents/ --quality=0.65 --no_audio --quiet
In this example, if the user wants to change an argument from run to run, the difference is lost in the "noise".
With configurable_argparse
ARGPARSE_PARSER_ARGS = {"prog": "Example program"}
ARGPARSE_ARGUMENTS = [
Argument(
('-t', '--nb_threads'),
{
'type': int,
'default': 4,
'help': "Number of threads to use"
}
),
...
ArgumentExclusiveGroup([
Argument(
'--verbose',
{
'action': 'store_true',
'help': 'Show all messages'
}
),
Argument(
'--quiet',
{
'action': 'store_true',
'help': 'Show no messages'
}
)
])
]
...
def main():
args = get_args(
parser_args=ARGPARSE_PARSER_ARGS,
arguments=ARGPARSE_ARGUMENTS,
yaml_config_base=Path(__file__)
)
Usage example:
- First run:
python example_program.py -t 4 --use_library=libabc --ignore=E123,E321 --add_src=/homes/user/documents/ --quality=0.65 --no_audio --quiet
(save config asusual
) - Subsequent runs:
python example_program.py --use_config=usual --quality=0.55
In this example, if the user wants to change an argument from run to run, the difference is immediately apparent since the "noise" is abstracted away in a usual
configuration.
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 configurable_argparse_DavidRodriguezSoaresCUI-0.0.2.tar.gz
.
File metadata
- Download URL: configurable_argparse_DavidRodriguezSoaresCUI-0.0.2.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c677ed05835d925b3d2ac7ada7bd02fee02adf08d0ed3e39f29c063636ec1e1a |
|
MD5 | 41a108e8f5f6ae1532b7b6888f4a05eb |
|
BLAKE2b-256 | 753ed98e85d01e2f3d88a20e44e46311c08652e07648c67d0c072e45a749ee47 |
File details
Details for the file configurable_argparse_DavidRodriguezSoaresCUI-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: configurable_argparse_DavidRodriguezSoaresCUI-0.0.2-py3-none-any.whl
- Upload date:
- Size: 10.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5021c471e3183ee2695810fb77a6a57049a74a1ef9799b23eb317cb71cddb602 |
|
MD5 | 66f309d64be82366acf6222bc7ee106b |
|
BLAKE2b-256 | bd46ba6fb3a465140c71a34a2b61a6f2bdbbfb264bc7270eddbaa79b88857459 |