https://github.com/moisutsu/classopt
Arguments parser with class for Python, inspired by StructOpt
https://github.com/moisutsu/classopt
argument-parser python
Last synced: 4 months ago
JSON representation
Arguments parser with class for Python, inspired by StructOpt
- Host: GitHub
- URL: https://github.com/moisutsu/classopt
- Owner: moisutsu
- License: mit
- Created: 2021-07-04T02:18:04.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-09-17T07:20:21.000Z (over 2 years ago)
- Last Synced: 2025-03-12T16:44:58.800Z (11 months ago)
- Topics: argument-parser, python
- Language: Python
- Homepage: https://pypi.org/project/classopt/
- Size: 85 KB
- Stars: 61
- Watchers: 1
- Forks: 4
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Welcome to ClassOpt 👋
> Arguments parser with class for Python, inspired by [StructOpt](https://github.com/TeXitoi/structopt)
## Install
```sh
pip install classopt
```
## Usage
Import `classopt` and define the Opt class with decorator.
```python
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.
```bash
$ 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](https://docs.python.org/ja/3/library/argparse.html#argparse.ArgumentParser.add_argument) in `config` (except name_or_flags).
```python
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)
```
```bash
$ 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
```python
# `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`.
```python
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.
```bash
$ 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
```sh
poetry run pytest
```
## Author
👤 **moisutsu**
* Twitter: [@moisutsu](https://twitter.com/moisutsu)
* Github: [@moisutsu](https://github.com/moisutsu)
## Show your support
Give a ⭐️ if this project helped you!
## 📝 License
Copyright © 2021 [moisutsu](https://github.com/moisutsu).
This project is [MIT](https://github.com/moisutsu/classopt/blob/main/LICENSE) licensed.
***
_This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_