argparsenv 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
argparsenv-1.0.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 snaeil
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: argparsenv
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Extends python's argparser with environment variables
|
|
5
|
+
License: MIT
|
|
6
|
+
Author: snaeil
|
|
7
|
+
Author-email: schnegel@posteo.de
|
|
8
|
+
Requires-Python: >=3.11,<4.0
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Requires-Dist: overrides (>=7.7.0,<8.0.0)
|
|
14
|
+
Requires-Dist: python-dotenv (>=1.0.1,<2.0.0)
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# argparsenv
|
|
18
|
+
|
|
19
|
+
This project extends python's argparse module to allow for environment variable overrides of command line arguments.
|
|
20
|
+
|
|
21
|
+
The rule for environment variable overrides is as follows:
|
|
22
|
+
|
|
23
|
+
1. If the argument is provided on the command line, the environment variable is ignored.
|
|
24
|
+
2. If the argument is not provided on the command line, the environment variable is used if it is set.
|
|
25
|
+
3. If the argument is not provided on the command line and the environment variable is not set, the default value is used.
|
|
26
|
+
4. If the argument is environment variable is set both on the command line and in the environment, the command line value is used.
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
Stable release version:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pip install argparsenv
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Latest development version:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
pip install git+https://github.com/snaeil/argparsenv
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Usage
|
|
43
|
+
|
|
44
|
+
This module builds on top of python's standard library
|
|
45
|
+
[argparse](https://docs.python.org/3/library/argparse.html).
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
env_arg_parser = ArgumentParser()
|
|
49
|
+
env_arg_parser.add_argument(
|
|
50
|
+
"--port",
|
|
51
|
+
dest="port",
|
|
52
|
+
env_var="MY_APP_DB_PORT",
|
|
53
|
+
default="3306",
|
|
54
|
+
)
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
With this ArgumentParser instance, the port argument can be set in three ways:
|
|
58
|
+
|
|
59
|
+
1. On the command line:
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
python my_app.py --port 3307
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
2. As an environment variable (this can also be done by using a `.env` file):
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
export MY_APP_DB_PORT=3307
|
|
69
|
+
python my_app.py
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
3. Using the default value (here `3306`):
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
python my_app.py
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Contributing
|
|
79
|
+
|
|
80
|
+
Contributions are welcome!
|
|
81
|
+
For feature requests, bug reports or questions, please open an issue.
|
|
82
|
+
For code contributions, please open a pull request.
|
|
83
|
+
|
|
84
|
+
Please make sure to install `pre-commit` before making changes to the code (this enables
|
|
85
|
+
checks for code formatting, linting, etc. before committing changes):
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
pip install pre-commit
|
|
89
|
+
cd path/to/argparsenv
|
|
90
|
+
pre-commit install
|
|
91
|
+
```
|
|
92
|
+
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# argparsenv
|
|
2
|
+
|
|
3
|
+
This project extends python's argparse module to allow for environment variable overrides of command line arguments.
|
|
4
|
+
|
|
5
|
+
The rule for environment variable overrides is as follows:
|
|
6
|
+
|
|
7
|
+
1. If the argument is provided on the command line, the environment variable is ignored.
|
|
8
|
+
2. If the argument is not provided on the command line, the environment variable is used if it is set.
|
|
9
|
+
3. If the argument is not provided on the command line and the environment variable is not set, the default value is used.
|
|
10
|
+
4. If the argument is environment variable is set both on the command line and in the environment, the command line value is used.
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
Stable release version:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pip install argparsenv
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Latest development version:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pip install git+https://github.com/snaeil/argparsenv
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
This module builds on top of python's standard library
|
|
29
|
+
[argparse](https://docs.python.org/3/library/argparse.html).
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
env_arg_parser = ArgumentParser()
|
|
33
|
+
env_arg_parser.add_argument(
|
|
34
|
+
"--port",
|
|
35
|
+
dest="port",
|
|
36
|
+
env_var="MY_APP_DB_PORT",
|
|
37
|
+
default="3306",
|
|
38
|
+
)
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
With this ArgumentParser instance, the port argument can be set in three ways:
|
|
42
|
+
|
|
43
|
+
1. On the command line:
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
python my_app.py --port 3307
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
2. As an environment variable (this can also be done by using a `.env` file):
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
export MY_APP_DB_PORT=3307
|
|
53
|
+
python my_app.py
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
3. Using the default value (here `3306`):
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
python my_app.py
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Contributing
|
|
63
|
+
|
|
64
|
+
Contributions are welcome!
|
|
65
|
+
For feature requests, bug reports or questions, please open an issue.
|
|
66
|
+
For code contributions, please open a pull request.
|
|
67
|
+
|
|
68
|
+
Please make sure to install `pre-commit` before making changes to the code (this enables
|
|
69
|
+
checks for code formatting, linting, etc. before committing changes):
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
pip install pre-commit
|
|
73
|
+
cd path/to/argparsenv
|
|
74
|
+
pre-commit install
|
|
75
|
+
```
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"""This module contains utilities for command line interfaces, icnluding argument parsing and configuration."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
import argparse
|
|
5
|
+
from os import (
|
|
6
|
+
path as os_path,
|
|
7
|
+
getenv as os_getenv,
|
|
8
|
+
getcwd as os_getcwd,
|
|
9
|
+
)
|
|
10
|
+
from dotenv import load_dotenv
|
|
11
|
+
from overrides import override
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class ArgumentParser(argparse.ArgumentParser):
|
|
15
|
+
"""Enhances the standard argparse.ArgumentParser by adding the ability to read\
|
|
16
|
+
default values from environment variables."""
|
|
17
|
+
|
|
18
|
+
def __init__(self, *args, **kwargs):
|
|
19
|
+
"""Initializes the parser and loads the environment variables from the .env file.
|
|
20
|
+
*args: inherited from argparse.ArgumentParser
|
|
21
|
+
**kwargs: inherited from argparse.ArgumentParser
|
|
22
|
+
|
|
23
|
+
Initializes the parser with the given arguments and loads the environment variables.
|
|
24
|
+
>>> parser = ArgumentParser(description="This is a parser")
|
|
25
|
+
"""
|
|
26
|
+
super().__init__(*args, **kwargs)
|
|
27
|
+
load_dotenv(os_path.join(os_getcwd(), ".env"))
|
|
28
|
+
|
|
29
|
+
def get_value_from_environment(
|
|
30
|
+
self, env_key: None | str, fallback_value: None | str
|
|
31
|
+
) -> None | str:
|
|
32
|
+
"""Looks up the value of the given environment variable and returns it if it is\
|
|
33
|
+
set or the fallback value otherwise. If the environment variable is set\
|
|
34
|
+
both via the environment and the .env file, the value from\
|
|
35
|
+
the environment is used.
|
|
36
|
+
|
|
37
|
+
Args:
|
|
38
|
+
env_key: The name of the environment variable to look up.
|
|
39
|
+
fallback_value: The value to return if the environment variable is not set.
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
A string containing the value of the environment variable or the fallback\
|
|
43
|
+
value if the environment variable is not set. May be None.
|
|
44
|
+
"""
|
|
45
|
+
if env_key:
|
|
46
|
+
env_key = env_key.strip()
|
|
47
|
+
if env_key := env_key.strip():
|
|
48
|
+
if (env_value := os_getenv(env_key.strip())) is not None:
|
|
49
|
+
return env_value
|
|
50
|
+
return fallback_value
|
|
51
|
+
|
|
52
|
+
@override
|
|
53
|
+
def add_argument(self, *args, **kwargs):
|
|
54
|
+
"""Adds an argument to the parser and (in additon to the inherited method)\
|
|
55
|
+
overrides the default value with the value of the environment variable\
|
|
56
|
+
if it is set.
|
|
57
|
+
|
|
58
|
+
Args:
|
|
59
|
+
*args: Inherited from argparse.ArgumentParser.add_argument.
|
|
60
|
+
**kwargs: Inherited from argparse.ArgumentParser.add_argument.\
|
|
61
|
+
Additionally, the keyword argument `env_var` is added.\
|
|
62
|
+
If `env_var` is set, the default value is tried to be read from\
|
|
63
|
+
the environment variable with the given name.\
|
|
64
|
+
|
|
65
|
+
Returns:
|
|
66
|
+
The original return value of argparse.ArgumentParser.add_argument.
|
|
67
|
+
|
|
68
|
+
Example:
|
|
69
|
+
>>> p = ArgumentParser().add_argument("--port", dest="port", env_var="MY_APP_DB_PORT", default="3306", )
|
|
70
|
+
"""
|
|
71
|
+
|
|
72
|
+
if "env_var" in kwargs:
|
|
73
|
+
env_var = kwargs["env_var"]
|
|
74
|
+
del kwargs["env_var"]
|
|
75
|
+
kwargs["default"] = self.get_value_from_environment(
|
|
76
|
+
env_var,
|
|
77
|
+
kwargs["default"] if "default" in kwargs else None,
|
|
78
|
+
)
|
|
79
|
+
return super().add_argument(*args, **kwargs)
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "argparsenv"
|
|
3
|
+
version = "1.0.0"
|
|
4
|
+
description = "Extends python's argparser with environment variables"
|
|
5
|
+
authors = ["snaeil <schnegel@posteo.de>"]
|
|
6
|
+
license = "MIT"
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
|
|
9
|
+
[tool.poetry.dependencies]
|
|
10
|
+
python = "^3.11"
|
|
11
|
+
python-dotenv = "^1.0.1"
|
|
12
|
+
overrides = "^7.7.0"
|
|
13
|
+
|
|
14
|
+
[tool.poetry.group.dev.dependencies]
|
|
15
|
+
black = "^24.4.2"
|
|
16
|
+
pytest-cov = "^5.0.0"
|
|
17
|
+
mypy = "^1.10.0"
|
|
18
|
+
pdoc = "^14.4.0"
|
|
19
|
+
bandit = "^1.7.8"
|
|
20
|
+
pylint = "^3.1.0"
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
[build-system]
|
|
24
|
+
requires = ["poetry-core"]
|
|
25
|
+
build-backend = "poetry.core.masonry.api"
|