Coder Social home page Coder Social logo

datacli's Introduction

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

datacli's People

Contributors

jeremy-stafford avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

datacli's Issues

Bug with bool field

Hi,
Thanks for the library! I encountered a bug with bool fields: the returned value is always True. See below

from dataclasses import dataclass
from datacli import datacli

@dataclass
class Test:
    a: bool

print(datacli(Test, argv=['--a', 'False']))

Result:

Test(a=True)

Versions:
dataclass-cli 0.1.1
python 3.10.10

Add help message and env variable defaults

Hey there!

Nice and lean lib for creating CLI arguments from a dataclass!

I'd like to add the options to let fields default to environment variables, add help text and include the env variable in help and error messages for missing parameters.

In fact, I have already done it so the question is more, would a PR be appreciated ;-)

Do not force cli args to "...-..." format

Hi!

Thanks for that nice lib, very useful! The only issue I encountered so far is this line.
yield "--" + field.name.replace("_", "-")

Is there a reason to forbid underscores in cli args?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.