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, config
@ClassOpt
class Opt:
file: str
count: int = config(long=True)
numbers: list = config(long=True, short=True, nargs="+", type=int)
debug: bool = config(long=True, short=True, action="store_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 -n 1 2 3 --debug
Opt(file='example.txt', count=5, numbers=[1, 2, 3], debug=True)
example.txt
You can specify most of the arguments to argparse.ArgumentParser.add_argument in config
(except name_or_flags).
You can also use the long option by default.
from classopt import ClassOpt, config
@ClassOpt(default_long=True)
class Opt:
file: str = config(long=False)
count: int
numbers: list = config(nargs="+", type=int)
debug: bool = config(action="store_true")
if __name__ == "__main__":
opt = Opt.from_args()
print(opt)
$ python example.py example.py --count 5 --numbers 1 2 3 --debug
Opt(file='example.py', count=5, numbers=[1, 2, 3], debug=True)
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.4.tar.gz
(3.9 kB
view hashes)