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.
Step 1: Option dataclass definition
from argparse import ArgumentParser
from dataclasses import dataclass, field
import ddargparse
@dataclass
class Options(ddargparse.OptionsBase):
inputfile: 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"},
)
@dataclass
class DoSomethingOptions(ddargparse.OptionsBase):
threshold: str = field(metadata={"help": "Some threshold"})
Step 2: CLI argument parser declaration
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
DoSomethingOptions.register_cli_args(subparser)
Step 3: CLI argument parsing and option dataclass instantiation
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_argumentcalls, no need for thetypeargument. - 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| Nonein type annotation). - Choose between append-style (
--arg item1 --arg item2 --arg item3) and nargs-style (default,--arg item1 item2 item3) list arguments viaregister_cli_args(..., list_append=True|False). - 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
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 ddargparse-0.2.0.tar.gz.
File metadata
- Download URL: ddargparse-0.2.0.tar.gz
- Upload date:
- Size: 17.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a1e938c4eafc93b588d8c59b197d011a128531243b34714460931aca2f3300b5
|
|
| MD5 |
96e60a89dd7055096ee5eb127d6ef920
|
|
| BLAKE2b-256 |
7684e29181ee8ad0826ac1ab3430522bf23926ee0165ed90c6b90e00975fc856
|
Provenance
The following attestation bundles were made for ddargparse-0.2.0.tar.gz:
Publisher:
release-please.yml on koesterlab/ddargparse
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ddargparse-0.2.0.tar.gz -
Subject digest:
a1e938c4eafc93b588d8c59b197d011a128531243b34714460931aca2f3300b5 - Sigstore transparency entry: 1146375794
- Sigstore integration time:
-
Permalink:
koesterlab/ddargparse@9aad7c301efc28bbe30524a97ead04135befe53e -
Branch / Tag:
refs/heads/main - Owner: https://github.com/koesterlab
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-please.yml@9aad7c301efc28bbe30524a97ead04135befe53e -
Trigger Event:
push
-
Statement type:
File details
Details for the file ddargparse-0.2.0-py3-none-any.whl.
File metadata
- Download URL: ddargparse-0.2.0-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c49ebdffab30b645934d84b38efa141527e36c65f9411cd11335767f1d85e6e2
|
|
| MD5 |
d492a1e912032b1118acc0494c63872a
|
|
| BLAKE2b-256 |
3ecc1ca51bb724f1b68fec48229cff323aceb7b66d25feec894aeb906795b879
|
Provenance
The following attestation bundles were made for ddargparse-0.2.0-py3-none-any.whl:
Publisher:
release-please.yml on koesterlab/ddargparse
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ddargparse-0.2.0-py3-none-any.whl -
Subject digest:
c49ebdffab30b645934d84b38efa141527e36c65f9411cd11335767f1d85e6e2 - Sigstore transparency entry: 1146375835
- Sigstore integration time:
-
Permalink:
koesterlab/ddargparse@9aad7c301efc28bbe30524a97ead04135befe53e -
Branch / Tag:
refs/heads/main - Owner: https://github.com/koesterlab
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-please.yml@9aad7c301efc28bbe30524a97ead04135befe53e -
Trigger Event:
push
-
Statement type: