Skip to content

A library for building simple command line interfaces from dataclasses.

License

Notifications You must be signed in to change notification settings

willhaben/datacli

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

datacli

datacli is a library for building simple command line interfaces from dataclasses. It is built on argparse and has no dependencies.

Usage

A picture code snippet speaks a thousand words:

# person.py

from dataclasses import dataclass, field
from datacli import datacli

@dataclass
class Person:
    name: str
    age: int = 30

args = datacli(Person)
print(args)
$ python person.py --name "Jeremy"
Person(name="Jeremy", age=30)
$ python person.py --name "Jeremy" --age 20
Person(name="Jeremy", age=20)
$ python person.py --help
usage: test.py [-h] --name NAME [--age AGE]

optional arguments:
  -h, --help   show this help message and exit
  --name NAME
  --age AGE

Field names

Short and long field names (-a or --foo-bar) can be modified through the data-class field metadata, for example,

# person.py

from dataclasses import dataclass, field
from datacli import datacli

@dataclass
class Person:
    name: str = field(metadata={"short_name": "-n"})
    age: int = field(metadata={"short_name": "-a", "long_name": "--years"})

args = datacli(Person)
print(args)
$ python person.py -n "Jeremy" -a 20
Person(name="Jeremy", age=20)
$ python person.py --name "Jeremy" --years 30
Person(name="Jeremy", age=30)
$ python person.py --help
usage: test.py [-h] --name NAME --age AGE

optional arguments:
  -h, --help   show this help message and exit
  -n, --name NAME
  -a, --age YEARS

If you want to use defaults with field names, you should use the default or default_factory arguments to field(), for example,

age: int = field(default=20, metadata={"short_name": "-a"})

Help Text

Add custom help messages through field(metadata={"help": <help text>})

Environment variables

Link CLI arguments to environment variables and add defaults via field(default_factory=from_env_var(<var_name>, [default=<some_default>])). If default is omitted then the value is mandatory and datacli will fail with a helpful error message if neither the parameter nor the environment variable are provided. Setting the from_env_var as a default factory also expands the help text to give the name of the connected environment variable.

About

A library for building simple command line interfaces from dataclasses.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 100.0%