Skip to main content

Dataclass driven argparse handling

Project description

ddargparse

A small Python library that simplifies command-line argument parsing by leveraging dataclasses. It allows developers to define their command-line interfaces using dataclass fields, making the code more concise and easier to maintain. With ddargparse, you can easily create complex command-line applications with minimal boilerplate code. It puts a particular focus on properly handling type annotations and defaults, minimizing additional required annotation and maintaining interoperability with the standard argparse.

Installation

pip install ddargparse

Usage

Subclass OptionsBase and annotate fields with standard argparse metadata via the dataclasses.field function (help, required, positional, metavar). Then call register_cli_args to populate an ArgumentParser and from_cli_args to instantiate your options from the parsed result.

from argparse import ArgumentParser
from dataclasses import dataclass, field
import ddargparse

@dataclass
class Options(ddargparse.OptionsBase):
    input: str = field(
        metadata={"help": "Input file", "positional": True, "metavar": "FILE"},
    )
    verbose: bool = field(
        metadata={"help": "Enable verbose output"},
    )
    tags: list[str] = field(
        default_factory=list,
        metadata={"help": "One or more tags"},
    )

class DoSomethingdOptions(ddargparse.OptionsBase):
    threshold: str = field(metadata={"help": "Some threshold"})

parser = ArgumentParser()
# register dataclass options as argparse parser arguments
Options.register_cli_args(parser)
subparsers = parser.add_subparsers(dest="subcommand")
subparser = subparsers.add_parser("do-something")
# register dataclass options as argparse subparser arguments
DoSomethingdOptions.register_cli_args(subparser)

args = parser.parse_args()
# obtain instance of dataclass with global options
options = Options.from_cli_args(args)
match args.subcommand:
    case "do-something":
        # obtain instance of dataclass with subcommand options
        do_something_options = DoSomethingOptions.from_cli_args(args)

Features

  • Declare CLI arguments as typed dataclass fields — no repetitive add_argument calls, no need for the type argument.
  • Booleans (store_true / store_false), and list arguments (nargs="+") are inferred automatically from the dataclass field definitions.
  • Custom parse methods: define a parse_<field_name> classmethod to override the argument type converter.
  • Mark options as positional ("positional": True).
  • Automatic and natural inference whether option is required (no field(default=...) and no | None in type annotation).
  • Pass arbitrary argparse argument options via field(metadata={...}).
  • Seamless integration with standard argparse API.
  • No additional dependencies.

Requirements

  • Python ≥ 3.11

License

See LICENSE.

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

ddargparse-0.1.4.tar.gz (17.0 kB view details)

Uploaded Source

Built Distribution

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

ddargparse-0.1.4-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file ddargparse-0.1.4.tar.gz.

File metadata

  • Download URL: ddargparse-0.1.4.tar.gz
  • Upload date:
  • Size: 17.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ddargparse-0.1.4.tar.gz
Algorithm Hash digest
SHA256 c396b04a5eba6ec42178f15f3a991662fb6a78e5858f9b1ed2678aa697dc0d3c
MD5 273d507abcf2f7daef288901a397c151
BLAKE2b-256 027fcebc1ff02962b69b7e128b92cd52d3880952f9a07f39cfd51b56fe1fee6a

See more details on using hashes here.

Provenance

The following attestation bundles were made for ddargparse-0.1.4.tar.gz:

Publisher: release-please.yml on koesterlab/ddargparse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ddargparse-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: ddargparse-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ddargparse-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 7912d1188e6fd2816d002463e89c1a67deb5ae38a96e508dd0ad2089d41035d2
MD5 db5cfaa4cf785377cca68669740c8169
BLAKE2b-256 91a56e31e8b86b38af14eb63abbe5450d213be15e13be47d952f18137a189eea

See more details on using hashes here.

Provenance

The following attestation bundles were made for ddargparse-0.1.4-py3-none-any.whl:

Publisher: release-please.yml on koesterlab/ddargparse

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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