Git Product home page Git Product logo

classopt's Introduction

Welcome to ClassOpt ๐Ÿ‘‹

Version License: MIT Twitter: moisutsu

Arguments parser with class for Python, inspired by StructOpt

Install

pip install classopt

Usage

Import classopt and define the Opt class with decorator.

from classopt import classopt

@classopt(default_long=True)
class Opt:
    file: str
    count: int = 3
    numbers: list[int]
    flag: bool

if __name__ == "__main__":
    opt = Opt.from_args()
    print(opt)
    print(opt.file)

Run with command line arguments.

$ python example.py --file example.txt --numbers 1 2 3 --flag
Opt(file='example.txt', count=3, numbers=[1, 2, 3], flag=True)
example.txt

You can specify most of the arguments to argparse.ArgumentParser.add_argument in config (except name_or_flags).

from classopt import classopt, config

@classopt
class Opt:
    file: str
    count: int = config(long=True)
    numbers: list = config(long=True, short=True, nargs="+", type=int)
    flag: bool = config(long=True, action="store_false")

if __name__ == "__main__":
    opt = Opt.from_args()
    print(opt)
$ python example.py example.txt --count 5 -n 1 2 3 --flag
Opt(file='example.txt', count=5, numbers=[1, 2, 3], flag=False)

Some details

# `default_long=True` is equivalent to `config(long=True)' for all members
# Similarly, you can do `default_short=True`
@classopt(default_long=True)
class Opt:
    # `long=False` overrides `default_long=True`
    file: str = config(long=False)

    # equivalent to `numbers: list = config(nargs="*", type=int)`
    # and `numbers: typing.List[int]`
    numbers: list[int]

    # equivalent to `flag: bool = config(action="store_true")`
    flag: bool

Other Way

You can also define an argument parser by inheriting from ClassOpt.

from classopt import ClassOpt, config

class Opt(ClassOpt):
    file: str
    count: int = config(long=True)
    numbers: list[int] = config(long=True, short="-c")
    flag: bool = config(long=True)

if __name__ == "__main__":
    opt = Opt.from_args()
    print(opt)
    print(opt.file)

Run with command line arguments.

$ python example.py example.txt --count 5 -c 1 2 3 --flag
Opt(file='example.txt', count=5, numbers=[1, 2, 3], flag=True)
example.txt

The inherited method does not support some features and may disappear in the future. So we recommend the decorator method.

Run tests

poetry run pytest

Author

๐Ÿ‘ค moisutsu

Show your support

Give a โญ๏ธ if this project helped you!

๐Ÿ“ License

Copyright ยฉ 2021 moisutsu.
This project is MIT licensed.


This README was generated with โค๏ธ by readme-md-generator

classopt's People

Contributors

hanjinliu avatar hpprc avatar ikanago avatar moisutsu avatar takeshiakehi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

classopt's Issues

The `from_dict` method ignores types defined in the argument class

The from_dict method assigns a value to a member, ignoring the type in the definition of the argument class.

Code

from pathlib import Path
from classopt import classopt

@classopt
class Opt:
    path: Path

args_dict = {"path": "sample.txt"}
opt = Opt.from_dict(args_dict)
print(opt)

Expected

$ python sample.py
Opt(path=PosixPath('sample.txt'))

Actual

$ python sample.py
Opt(path='sample.txt')

Add CI to run unit test

This project does not seem to have any CI to verify which all tests have passes.
If we set up CI to run unit test, โœ…(green check mark) is displayed for commits whose CI passes.
This ensures classopt's behavior under current unit test, so users feels more reliable to use.

I have maintained CI to run unit test and other tasks for the project whose architecture is similar to classopt.
So I'd like to adjust GitHub Actions if you are OK.

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.