Skip to main content

argparse_dataclasses

In this package we'll place some boilerplate code that helps:

  • to convert dataclasses to argparse commandline argument parsers;
  • to convert argparse commandline arguments to dataclasses.

Why so?

Dozens of times I had to create settings classes for my Python console scripts. Here's some utility boilerplate code that helps to create commandline parsers for dataclasses in a couple of lines = )

How to install

python3 -m pip install argparse-dataclasses

How to use

At first, let's create a simple script simple_example.py, in which we:

  • import some stuff from our module,
  • declare a simple dataclass
  • and inherit it from our CmdParsingMixin (this adds support for automatic argparse.ArgumentParser creation.

simple_example.py

from dataclasses import dataclass, field
from argparse_dataclasses import CmdParsingMixin


@dataclass
class SimpleSettings(CmdParsingMixin): #
    name: str
    num_experiments: int = field(default=1)


settings = SimpleSettings.build_from_commandline()

print(settings)  # That's all, folks!

And let's run this script from commandline:

    $ python3 simple_example.py

It will give us the error message.

    usage: simple_example.py [-h] --name NAME [--num-experiments NUM_EXPERIMENTS]
    simple_example.py: error: the following arguments are required: --name

Okay, just as planned. As we can see, we've forgot a required commandline argument. Let's try again

    $ python3 simple_example.py --name=DJ_BLYATMAN --num-experiments=33

This will print us the settings instance:

SimpleSettings(name='DJ_BLYATMAN', num_experiments=33)

Be careful: use - in commandline argument names and _ in Python dataclass field names. It does make sense!

Our code works with argparse.ArgumentParser under the hood, so it also supports --help commandline argument to explicitly order help message:

    $ python3 simple_example.py --help

It will output the following:

usage: simple_example.py [-h] --name NAME [--num-experiments NUM_EXPERIMENTS]

Creating SimpleSettings instance from commandline args...

optional arguments:
  -h, --help            show this help message and exit
  --name NAME
  --num-experiments NUM_EXPERIMENTS

Metadata

Release files for argparse-dataclasses 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for argparse-dataclasses 0.1.4
File Size Uploaded
argparse_dataclasses-0.1.4.tar.gz 3.8 kB Details

Release files / argparse_dataclasses-0.1.4.tar.gz

Download URL argparse_dataclasses-0.1.4.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
04a9a4421cb42fe3bb197193522e29c52356665714920dad5fe0f60f3c6bf54c
BLAKE2b-256 checksum
How to use checksums
87510d52c6a360ff9fd641845a20ed176fbb9daed4b051d5ca300f9a71bd356c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

Release history Release notifications | RSS feed

This release

0.1.4 This release

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1

1 release file

0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page