Skip to main content

A wrapper around argparse to get command line argument parsers from dataclasses

Project description

dataparsers

A wrapper around argparse to get command line argument parsers from dataclasses.

Installation

   pip install dataparsers

Basic usage

Create a dataclass describing your command line interface, and call parse() with the class:

# prog.py
from dataclasses import dataclass
from dataparsers import parse

@dataclass
class Args:
    foo: str
    bar: int = 42

args = parse(Args)
print("Printing `args`:")
print(args)

The dataclass fields that have a "default" value are turned into optional arguments, while the non default fields will be positional arguments.

The script can then be used in the same way as used with argparse:

$ python prog.py -h
usage: prog.py [-h] [--bar BAR] foo

positional arguments:
  foo

options:
  -h, --help  show this help message and exit
  --bar BAR

And the resulting type of args is Args (recognized by type checkers and autocompletes):

$ python prog.py test --bar 12
Printing `args`:
Args(foo='test', bar=12)

Argument specification

To specify detailed information about each argument, call the arg() function on the dataclass fields:

# prog.py
from dataclasses import dataclass
from dataparsers import parse, arg

@dataclass
class Args:
    foo: str = arg(help="foo help")
    bar: int = arg(default=42, help="bar help")

args = parse(Args)

It allows to customize the interface:

$ python prog.py -h
usage: prog.py [-h] [--bar BAR] foo

positional arguments:
  foo         foo help

options:
  -h, --help  show this help message and exit
  --bar BAR   bar help

In general, the arg() function accepts all parameters that are used in the original add_argument() method (with few exceptions) and some additional parameters. The default keyword argument used above makes the argument optional (i.e., passed with flags like --bar) except in some specific situations.

One parameter of add_argument() that are not possible to pass to arg() is the dest keyword argument. That's because the name of the class attribute is determined by the dataclass field name. So, it is unnecessary to pass the dest parameter, since it doesn't makes sense in this situation.

Documentation

For more information, see the documentation.

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

dataparsers-0.1.0.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

dataparsers-0.1.0-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

File details

Details for the file dataparsers-0.1.0.tar.gz.

File metadata

  • Download URL: dataparsers-0.1.0.tar.gz
  • Upload date:
  • Size: 2.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.7 Windows/10

File hashes

Hashes for dataparsers-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6ff51586da61100babcd4ceab84a700dd0d3dab3e40efa4dd0d4f074a2850d34
MD5 c025901bc8e336644d5ffe29692f7b6b
BLAKE2b-256 7c9aedeb73339f94f65a87e87a82318585f66d74ebd8d4eb01fe27e9f6cc7d7a

See more details on using hashes here.

File details

Details for the file dataparsers-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: dataparsers-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.11.7 Windows/10

File hashes

Hashes for dataparsers-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8e2aefec1aea3b083230fb1960612894861ebb36f0ed86575029f0aca77fde3d
MD5 54ef6d5cf1ef5c4ce7b2cfb663453e92
BLAKE2b-256 692ee6e19b2f3307fc42e33b21e090b3fb3cd516dc6861aae1a98a65e31f101f

See more details on using hashes here.

Supported by

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