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", "required": True, "metavar": "FILE"},
    )
    verbose: bool = field(
        metadata={"help": "Enable verbose output"},
    )
    tags: list[str] = field(
        default_factory=list,
        metadata={"help": "One or more tags"},
    )

parser = ArgumentParser()
Options.register_cli_args(parser)
args = parser.parse_args()
options = Options.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).
  • 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.3.tar.gz (16.7 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.3-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ddargparse-0.1.3.tar.gz
  • Upload date:
  • Size: 16.7 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.3.tar.gz
Algorithm Hash digest
SHA256 b254c32561d48642133733f90e81bd7293001e28882e2f864ab1e1bd9a5b3a46
MD5 4513b6e48c0dfe967ccf64a5a3c273a7
BLAKE2b-256 8f9e9290b3f354a1495245cc8ad7e68cc0313d72b1a8c7864978cfb7d072350d

See more details on using hashes here.

Provenance

The following attestation bundles were made for ddargparse-0.1.3.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.3-py3-none-any.whl.

File metadata

  • Download URL: ddargparse-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 4.1 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 06b85ac09a7a74b2f5059d64e84f3fb403e1e4d5427e16903f41c07fed8a341a
MD5 c8692d5e176921e662473f06b2e375a7
BLAKE2b-256 a37e06be56df2bb6f56e01ab04efd90e708d0fafa35c92f1f9be7b506d7ab13e

See more details on using hashes here.

Provenance

The following attestation bundles were made for ddargparse-0.1.3-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