Skip to main content

Parse command-line arguments into a dataclass

Project description

argparcel

image image image Actions status

A minimalist library to parse command-line arguments into a dataclass.

Example usage

# examples/example_0.py
import dataclasses

import argparcel


@dataclasses.dataclass(kw_only=True, frozen=True, slots=True)
class _Args:
    a: int
    b: float

    # A `bool` argument will create a linked pair of flags `--c` and `--no-c`.
    c: bool

    # A command line argument will be optional if and only if a default value is
    # provided in the corresponding dataclass field.
    d: str | None = None


if __name__ == "__main__":
    print(argparcel.parse(_Args))
$ uv run examples/example_0.py --help
usage: example_0.py [-h] --a A --b B --c | --no-c [--d D]

options:
  -h, --help   show this help message and exit
  --a A
  --b B
  --c, --no-c
  --d D

$ uv run examples/example_0.py --a 2 --b 3.2 --c
_Args(a=2, b=3.2, c=True, d=None)

$ uv run examples/example_0.py --a 2 --b 3.2 --no-c
_Args(a=2, b=3.2, c=False, d=None)

$ uv run examples/example_0.py --a 2 --b 3.2 --no-c  --d moo
_Args(a=2, b=3.2, c=False, d='moo')

We also support:

  • Literal and Enums forcing specific choices
  • conversion to types whose __init__ accepts a string, e.g. pathlib.Path
  • 'help' can be provided too
# examples/example_1.py
import dataclasses
import enum
import pathlib 
from typing import Literal

import argparcel


class Bird(enum.Enum):
    puffin = enum.auto()
    lark = enum.auto()


@dataclasses.dataclass(kw_only=True, frozen=True, slots=True)
class _Args:
    # Using a `Literal` will force a choice between 1, 2, or 3.
    a: Literal[1, 2, 3]

    # An enum will force a choice between the names of the enum elements.
    b: Bird = Bird.puffin

    # A `Path` can be automatically converted from a string. Here we also specify a
    # 'help' message by using a 'docstring' for the field.
    c: pathlib.Path | None = None
    """An important path."""


if __name__ == "__main__":
    print(argparcel.parse(_Args))
$ uv run examples/example_1.py --help
usage: example_1.py [-h] --a {1,2,3} [--b {puffin,lark}] [--c C]

options:
  -h, --help         show this help message and exit
  --a {1,2,3}
  --b {puffin,lark}
  --c C              An important path.

$ uv run examples/example_1.py --a 2
_Args(a=2, b=<Bird.puffin: 1>, c=None)

$ uv run examples/example_1.py --a 2 --b lark --c /somewhere/to/go
_Args(a=2, b=<Bird.lark: 2>, c=PosixPath('/somewhere/to/go'))

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

argparcel-0.0.3.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

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

argparcel-0.0.3-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file argparcel-0.0.3.tar.gz.

File metadata

  • Download URL: argparcel-0.0.3.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.7.2

File hashes

Hashes for argparcel-0.0.3.tar.gz
Algorithm Hash digest
SHA256 dcc899e8189088ada0f0f7a97630004bfc8453039c0b082f3793c962d5cb493e
MD5 e1e45394851ae244b9664c5856bd9cc2
BLAKE2b-256 008853d79ef80a29c8e0323f9e694df1c974397a926105f56504fc82d78e35fe

See more details on using hashes here.

File details

Details for the file argparcel-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: argparcel-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.7.2

File hashes

Hashes for argparcel-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9322c90e89ab53e5ad69e3262fcdfe37f4cb229e56f549a5d963bc8576f1f5a8
MD5 47f06b37d76ac56d194131760353c92f
BLAKE2b-256 8f5b7cec53c1e03838499effa8bff4014afe83dd2e932bb181e7eee8216813a9

See more details on using hashes here.

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