Skip to content

A library for building simple command line interfaces from dataclasses.

License

Notifications You must be signed in to change notification settings

Jeremy-Stafford/datacli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 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"})

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