Argparse Utils (tddschn)
Utilities for dynamically removing or renaming argparse arguments without rewriting the original CLI definition.
Features
- Remove an option (including from mutually exclusive groups) by dest or option string.
- Replace an argument's option strings atomically with conflict detection.
Installation
pip install argparse-utils-tddschn
Usage
import argparse
from argparse_utils import remove_argument, replace_argument_names
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config")
parser.add_argument("--dry-run", action="store_true")
remove_argument(parser, "--dry-run")
replace_argument_names(parser, "--config", ["-C", "--configuration"])
Presetting Arguments
Sometimes you want to provide default argument values programmatically, for example when centralizing configuration or writing tests. The preset_arguments helper lets you do this by pretending the supplied tokens were passed on the command line.
Important points:
- Provide a sequence of tokens (not a single string) such as
['--config', 'foo.ini', '--dry-run']. - Flags and value-style options are supported; positional arguments are supported too.
- Unknown tokens will raise a
ValueError. - If you pass a raw string instead of a token sequence, a
TypeErroris raised. - Presets are applied as parser defaults using
parser.set_defaults(...), so actual CLI values still override them.
Example:
from argparse import ArgumentParser
from argparse_utils import preset_arguments
parser = ArgumentParser(prog="prog")
parser.add_argument("-c", "--config")
parser.add_argument("--dry-run", action="store_true")
# Pretend the user passed these on the command line
preset_arguments(parser, ["--config", "foo.ini", "--dry-run"])
args = parser.parse_args([])
assert args.config == "foo.ini"
assert args.dry_run is True
# CLI still wins over preset
args = parser.parse_args(["--config", "bar.ini"])
assert args.config == "bar.ini"
assert args.dry_run is True
Development
python -m venv .venv
source .venv/bin/activate
pip install -e .[dev]
pytest
License
MIT
Metadata
Release files for argparse-utils-tddschn 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| argparse_utils_tddschn-0.3.1.tar.gz | 6.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| argparse_utils_tddschn-0.3.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.7 kB
Release files / argparse_utils_tddschn-0.3.1.tar.gz
| Download URL | argparse_utils_tddschn-0.3.1.tar.gz |
|---|---|
| Size | 6.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
08c6ce1bb213cfcfbc2150413f6083329fa4d6e4c00d8aae81e136d2c2eb5ae2
|
|
BLAKE2b-256 checksum How to use checksums |
3c9f5a4da36c06865b5a2512a5f13c85b3ed2ad2111d8fa320b26576705b16b1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.0
|
Release files / argparse_utils_tddschn-0.3.1-py3-none-any.whl
| Download URL | argparse_utils_tddschn-0.3.1-py3-none-any.whl |
|---|---|
| Size | 4.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f023abee16770a4e6b7b17b56310e5dc4f393e68340c24be839c0784ddc19f58
|
|
BLAKE2b-256 checksum How to use checksums |
3c2d6c21758733824551925b3bd161cb3412989cf54670586a4eb085c495bee6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.11.0
|