A utility library to dynamically remove and rename arguments from argparse.ArgumentParser objects.
Project description
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 `TypeError` is raised.
- Presets are applied as parser defaults using `parser.set_defaults(...)`, so actual CLI values still override them.
Example:
```python
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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file argparse_utils_tddschn-0.3.0.tar.gz.
File metadata
- Download URL: argparse_utils_tddschn-0.3.0.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c0922f1bc9dc4d16e45ae7de70857c7a23ae1c47dd2eb5fbdb141c603cc5789c
|
|
| MD5 |
4881063e0b3790106bffaf29ee751ec1
|
|
| BLAKE2b-256 |
53eec20845c1aa01f43841dee67be34f2f29718ff82a1c03962eb8f9d95a5339
|
File details
Details for the file argparse_utils_tddschn-0.3.0-py3-none-any.whl.
File metadata
- Download URL: argparse_utils_tddschn-0.3.0-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c9c0391f215be3d9c150d5de862f8bc21eacd25165812be04d4ade57aad4b600
|
|
| MD5 |
3143045bf9575f40908d357a45d3210d
|
|
| BLAKE2b-256 |
1ebec8d132f402b01487478ccbbd2f27170e9e974a7c9ed394867f2fecd26a24
|