Skip to main content

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:

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

argparse_utils_tddschn-0.3.1.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

argparse_utils_tddschn-0.3.1-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file argparse_utils_tddschn-0.3.1.tar.gz.

File metadata

  • Download URL: argparse_utils_tddschn-0.3.1.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

Hashes for argparse_utils_tddschn-0.3.1.tar.gz
Algorithm Hash digest
SHA256 08c6ce1bb213cfcfbc2150413f6083329fa4d6e4c00d8aae81e136d2c2eb5ae2
MD5 ba991236f50e2ab955cd99e787c4459e
BLAKE2b-256 3c9f5a4da36c06865b5a2512a5f13c85b3ed2ad2111d8fa320b26576705b16b1

See more details on using hashes here.

File details

Details for the file argparse_utils_tddschn-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for argparse_utils_tddschn-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f023abee16770a4e6b7b17b56310e5dc4f393e68340c24be839c0784ddc19f58
MD5 5ac22f2f828921e9947551c29a82ed59
BLAKE2b-256 3c2d6c21758733824551925b3bd161cb3412989cf54670586a4eb085c495bee6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page