Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gabotechs/pyargparse
Awesome configuration parser for Python programs which can parse from command line, environment variables and .yml files with a single class definition providing type hints for the parsed arguments
https://github.com/gabotechs/pyargparse
Last synced: 29 days ago
JSON representation
Awesome configuration parser for Python programs which can parse from command line, environment variables and .yml files with a single class definition providing type hints for the parsed arguments
- Host: GitHub
- URL: https://github.com/gabotechs/pyargparse
- Owner: gabotechs
- License: mit
- Created: 2021-05-17T19:32:24.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-07-26T15:43:07.000Z (over 3 years ago)
- Last Synced: 2024-04-29T23:08:22.402Z (7 months ago)
- Language: Python
- Homepage:
- Size: 25.4 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# PyArgParse
Parse arguments by just defining a typed class that can parse
from commandline arguments, environment variables, and/or yml config files.## Install
```shell
pip install pyargparse
```
## UsageDefine a class that inherits from PyArgs class
```python
from typing import Optional
from pyargparse import PyArgsclass Args(PyArgs):
mandatory_string: str
optional_integer: Optional[int]
default_float: float = 0.5args = Args()
print(args)
```
launch the script with your configuration
```shell
python3 script.py --mandatory-string=foo --optional-integer=1
```
or
```shell
MANDATORY_STRING=foo DEFAULT_FLOAT=1.0 python3 script.py
```
or
```shell
echo "
mandatory_string: foo
optional_integer: 2
default_float: 1.0
" > config.yml
python3 script.py
```The priority is: CLI > ENV > YML
If you don't want to have all that possibilities for parsing arguments, there are more parsing classes
available:```python
from pyargparse import PyArgs, CliArgs, CliEnvArgs, CliYmlArgs, EnvArgs, EnvYmlArgs, YmlArgsCliArgs # only parse from command line
CliEnvArgs # parse from command line and from environment variables
CliYmlArgs # parse from command line and a yml config file
EnvArgs # parse only from environment variables
EnvYmlArgs # parse from environment variables and a yml config file
YmlArgs # parse only from yml config file
PyArgs # parse from everything!
```