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(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
Metadata
Release files for classopt 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| classopt-0.2.1.tar.gz | 6.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| classopt-0.2.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.9 kB
Release files / classopt-0.2.1.tar.gz
| Download URL | classopt-0.2.1.tar.gz |
|---|---|
| Size | 6.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6c540b8fa6ef409a97dfe90fede83b4feaa6384f1e53a2fb241ca986d6137a32
|
|
BLAKE2b-256 checksum How to use checksums |
c4be32acf247096c1a6a861cfc3c18ebafb6a8ba7f9cf6dccece6144f8634ec2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.13 CPython/3.9.1 Darwin/22.1.0
|
Release files / classopt-0.2.1-py3-none-any.whl
| Download URL | classopt-0.2.1-py3-none-any.whl |
|---|---|
| Size | 6.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
26419443f3a8fee90e73bcff15f608998ae9ad754c61a8903c0713289826c2d4
|
|
BLAKE2b-256 checksum How to use checksums |
4f5f7852c431b850a453d203dc4f3c3f71f949075c9cb523f86927c7fb7d1cf4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.13 CPython/3.9.1 Darwin/22.1.0
|