Hacky argparser - parses arguments and changes the defaults of functions
pip install hackyargparser
A decorator function that modifies the default arguments of a given function based on the values passed through command line arguments. The function first checks if the input function is callable or None. It then retrieves the global dictionary of the calling frame and defines a decorator function. The decorator function modifies the default arguments of the input function based on the values passed through command line arguments. It does this by retrieving the variable names and annotations of the input function and creating a dictionary of all the annotations. It then iterates through the default arguments of the input function and tries to convert them to the appropriate data type based on the annotations. If the conversion fails, it tries all possible data types until a successful conversion is made. The modified default arguments are then set and the input function is called with the modified arguments.
Create a py file
# "a2.py" in this example
from hackyargparser import add_sysargv, config
config.helptext = (
"Error! --f2_a, --f2_b, --f1_a are mandatory!" # The help text to be printed
)
config.kill_when_not_there(
("--f2_a", "--f2_b", "--f1_a")
) # If those arguments are not passed, config.helptext will be printed, and sys.exit will be called
config.stop_after_kill = True # Stop after printing config.helptext -> input()
@add_sysargv
def function1(
f1_a: int | float | None = None,
f1_b: int | float = 1,
mvar: int = 10,
):
print(f1_b * f1_a * mvar)
return f1_a, f1_b, mvar
@add_sysargv
def function2(
f2_a: int | float | None = None,
f2_b: int | float | None = None,
mvar: int = 10,
):
print(f2_a * f2_b * mvar)
return f2_a, f2_b, mvar
@add_sysargv
def function3(
f3_a: list | tuple = (),
f3_b: int | float = 1,
):
for l in f3_a:
print(l + f3_b)
return set(f3_a)
def function4(
f3_a: list | tuple = (),
f3_b: int | float = 1,
):
print("i am not decorated")
print(f"{f3_a=}, {f3_b=}")
return set(f3_a)
print(function2())
print(function1())
print(function3())
print(function4())
Execute the py file with arguments from the command line
####################################################################################################
# use -- and the local variable name as arguments followed by the value you want to pass
# Type hints are necessary as well as default values
# .\python.exe .\a2.py --f2_a 12 --f2_b 300 --f1_a 60
# 36000
# (12, 300, 10)
# 600
# (60, 1, 10)
# ...
####################################################################################################
# .\python.exe .\a2.py --f2_a 12 --f2_b 300
# Error! --f2_a, --f2_b, --f1_a are mandatory!
####################################################################################################
# .\python.exe .\a2.py --f2_a 12 --f2_b 300 --f1_a 60 --f3_a [1,2,3,4,5,4,4] --f3_b 22
# 36000
# (12, 300, 10)
# 600
# (60, 1, 10)
# 23
# 24
# 25
# 26
# 27
# 26
# 26
# {1, 2, 3, 4, 5}
# i am not decorated
# f3_a=(), f3_b=1
# set()
####################################################################################################
# If you get any errors, try using quotes --f3_a "(1,2,3,4,5)"
# .\python.exe .\a2.py --f2_a 12 --f2_b 300 --f1_a 60 --f3_a "(1,2,3,4,5)" --f3_b 22
# 36000
# (12, 300, 10)
# 600
# (60, 1, 10)
# 23
# 24
# 25
# 26
# 27
# {1, 2, 3, 4, 5}
# i am not decorated
# f3_a=(), f3_b=1
# set()
####################################################################################################
Metadata
Release files for hackyargparser 0.12
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| hackyargparser-0.12.tar.gz | 4.7 kB | Without provenance | Details for hackyargparser-0.12.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| hackyargparser-0.12-py3-none-any.whl | Python 3 | none | any | Details for hackyargparser-0.12-py3-none-any.whl |
Total release size: 11.3 kB
Release files / hackyargparser-0.12.tar.gz
| Download URL | hackyargparser-0.12.tar.gz |
|---|---|
| Size | 4.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cbda05f9198f900277ac25c871cc395ea092f28c34501cc12c93f97c1b686642
|
|
BLAKE2b-256 checksum How to use checksums |
8e85d1134dd736305d7dd2d39593cf7bf2f559a67b2bb8f4b155ac9e264211f9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.10
|
Release files / hackyargparser-0.12-py3-none-any.whl
| Download URL | hackyargparser-0.12-py3-none-any.whl |
|---|---|
| Size | 6.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cf8a3eceb12b9066cf60d4ec064450071fab2dd778d819194a2feef5a1cfd4c2
|
|
BLAKE2b-256 checksum How to use checksums |
764ffb1bfe962e896d241c7d316b6e99b63f15bf1032ee659f3378e9b3cc1ee4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.10
|
Security for hackyargparser 0.12
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.