Skip to main content

Interactive CLI prompts for nested dataclasses.

Project description

dc-input

PyPI License

A tiny, dependency-free library that lets you interactively fill dataclass instances via the command line. Useful for quick data entry, prototyping, or structured configuration; integrates easily with your own CLI tools.

Features

  • Supports defaults and nested dataclasses
  • Supports custom parsers
  • Structures prompts to flow naturally through nested structures

Installation

pip install dc_input

Usage

from dataclasses import dataclass
from datetime import date

from dc_input import from_dataclass


# --- Setup custom parsers as required  ---
def _parse_bool(s: str) -> bool:
    if s.lower() in ("y", "yes", "1", "true", "t"):
        return True
    elif s.lower() in ("n", "no", "0", "false", "f"):
        return False
    raise ValueError(f"Can't parse {s!r}")


parsers = {
    bool: _parse_bool,
    date: lambda s: date.fromisoformat(s)
}


# --- Example dataclass schema with nesting and defaults ---
@dataclass
class Address:
    city: str
    postal_code: str
    country: str = "Germany"


@dataclass
class Employee:
    name: str
    birthdate: date
    address: Address
    active: bool = True


# --- Use the CLI to fill in all values interactively ---
if __name__ == "__main__":
    employee = from_dataclass(Employee, parsers=parsers)

    print("\n--- Final result ---")
    print(employee)

Interactive Session Example

name (str): Alice
birthdate (date): 1982-07-08
active (bool, default=True): 0

address.city (str): Beriln    
address.postal_code (str): ..   > 💡 Undo previous input.

address.city (str): Berlin
address.postal_code (str): 10084
address.country (str, default=Germany):   > 💡 Accept default value.

New data:   > 💡 Edit values as needed. 
[0] name (str): Alice
[1] birthdate (date): 1982-07-08
[2] active (bool): False
[3] address.city (str): Berlin
[4] address.postal_code (str): 10084
[5] address.country (str): Germany

Change value? (n / {index} {new_value}): 4 10085

New data:
[0] name (str): Alice
[1] birthdate (date): 1982-07-08
[2] active (bool): False
[3] address.city (str): Berlin
[4] address.postal_code (str): 10085
[5] address.country (str): Germany

Change value? (n / {index} {new_value}): n   > 💡 Session complete.

Final Result

Employee(
    name='Alice', 
    birthdate=datetime.date(1982, 7, 8), 
    address=Address(
        city='Berlin', 
        postal_code='10085', 
        country='Germany'
    ), 
    active=False
)

Planned Features

  • Support for attrs and pydantic
  • Optional JSON/YAML import/export
  • Better colorized CLI output

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

dc_input-0.1.1.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

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

dc_input-0.1.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file dc_input-0.1.1.tar.gz.

File metadata

  • Download URL: dc_input-0.1.1.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for dc_input-0.1.1.tar.gz
Algorithm Hash digest
SHA256 10602d28ed4e1091d9086cd7b8854b259c50f964dc2ca44e4433b5f6ba85a547
MD5 bba2366370a4d0d017d12eb4df44cc45
BLAKE2b-256 fd8e3feffb44bad3969ab5342858011d04990b408b9d13576b2b566a8b472a58

See more details on using hashes here.

File details

Details for the file dc_input-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: dc_input-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for dc_input-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5a32c1a23c224e90b37c0915ade756dc9036e40e0ad828e11978676d8aed7ac3
MD5 b39e6461a1b5abe6f56b0463a5ffcbdc
BLAKE2b-256 f39d551231ada6552aaebed264413132a2914e7889ae0e64e43a6ca03a448717

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