Arguments parser with class for Python, inspired by StructOpt
Project description
Welcome to ClassOpt 👋
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
class Opt:
arg_int: int
arg_str: str
if __name__ == "__main__":
opt = Opt.from_args()
print(opt.arg_int, opt.arg_str)
Run with command line arguments.
$ python main.py --arg_int 5 --arg_str hello
5 hello
Advanced Usage
ClassOpt
internally uses the standard libraries dataclasses and argparse.
And you can specify the argument of argparse.ArgumentParser.add_argument
with the option
.
from classopt import ClassOpt, option
@ClassOpt
class Opt:
without_hyphen: str = option(name_or_flags="without_hyphen")
short_arg: str = option(name_or_flags="-s")
default_int: int = option(default=3)
store_true: bool = option(action="store_true")
nargs: list = option(nargs="+", type=int)
if __name__ == "__main__":
opt = Opt.from_args()
print(opt)
$ python main.py positional_arguments -s short_arg --store_true --nargs 1 2 3
Opt(positional_arguments='positional_arguments', short_arg='short_arg', default_int=3, store_true=True, nargs=[1, 2, 3])
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
classopt-0.1.2.tar.gz
(3.5 kB
view hashes)