# 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
```python
# "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
```python
####################################################################################################
# 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()
####################################################################################################
```
Raw data
{
"_id": null,
"home_page": "https://github.com/hansalemaos/hackyargparser",
"name": "hackyargparser",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "argparser",
"author": "Johannes Fischer",
"author_email": "aulasparticularesdealemaosp@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/8e/85/d1134dd736305d7dd2d39593cf7bf2f559a67b2bb8f4b155ac9e264211f9/hackyargparser-0.12.tar.gz",
"platform": null,
"description": "# Hacky argparser - parses arguments and changes the defaults of functions\r\n\r\n## pip install hackyargparser\r\n\r\nA decorator function that modifies the default arguments of a given function based on the values passed through command line arguments.\r\nThe function first checks if the input function is callable or None. It then retrieves the global dictionary of the\r\ncalling frame and defines a decorator function. The decorator function modifies the default arguments of the input\r\nfunction based on the values passed through command line arguments. It does this by retrieving the variable names\r\nand annotations of the input function and creating a dictionary of all the annotations. It then iterates through\r\nthe default arguments of the input function and tries to convert them to the appropriate data type based on the\r\nannotations. If the conversion fails, it tries all possible data types until a successful conversion is made. The\r\nmodified default arguments are then set and the input function is called with the modified arguments.\r\n\r\n\r\n## Create a py file \r\n\r\n```python\r\n\r\n# \"a2.py\" in this example\r\n\r\nfrom hackyargparser import add_sysargv, config\r\n\r\nconfig.helptext = (\r\n \"Error! --f2_a, --f2_b, --f1_a are mandatory!\" # The help text to be printed\r\n)\r\nconfig.kill_when_not_there(\r\n (\"--f2_a\", \"--f2_b\", \"--f1_a\")\r\n) # If those arguments are not passed, config.helptext will be printed, and sys.exit will be called\r\nconfig.stop_after_kill = True # Stop after printing config.helptext -> input()\r\n\r\n\r\n@add_sysargv\r\ndef function1(\r\n f1_a: int | float | None = None,\r\n f1_b: int | float = 1,\r\n mvar: int = 10,\r\n):\r\n print(f1_b * f1_a * mvar)\r\n return f1_a, f1_b, mvar\r\n\r\n\r\n@add_sysargv\r\ndef function2(\r\n f2_a: int | float | None = None,\r\n f2_b: int | float | None = None,\r\n mvar: int = 10,\r\n):\r\n print(f2_a * f2_b * mvar)\r\n\r\n return f2_a, f2_b, mvar\r\n\r\n\r\n@add_sysargv\r\ndef function3(\r\n f3_a: list | tuple = (),\r\n f3_b: int | float = 1,\r\n):\r\n for l in f3_a:\r\n print(l + f3_b)\r\n\r\n return set(f3_a)\r\n\r\n\r\ndef function4(\r\n f3_a: list | tuple = (),\r\n f3_b: int | float = 1,\r\n):\r\n print(\"i am not decorated\")\r\n print(f\"{f3_a=}, {f3_b=}\")\r\n\r\n return set(f3_a)\r\n\r\n\r\nprint(function2())\r\n\r\nprint(function1())\r\n\r\nprint(function3())\r\n\r\nprint(function4())\r\n\r\n\r\n```\r\n\r\n\r\n# Execute the py file with arguments from the command line\r\n\r\n```python\r\n####################################################################################################\r\n# use -- and the local variable name as arguments followed by the value you want to pass\r\n# Type hints are necessary as well as default values \r\n# .\\python.exe .\\a2.py --f2_a 12 --f2_b 300 --f1_a 60\r\n# 36000\r\n# (12, 300, 10)\r\n# 600\r\n# (60, 1, 10)\r\n# ...\r\n####################################################################################################\r\n\r\n# .\\python.exe .\\a2.py --f2_a 12 --f2_b 300\r\n# Error! --f2_a, --f2_b, --f1_a are mandatory!\r\n####################################################################################################\r\n# .\\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\r\n\r\n\r\n# 36000\r\n# (12, 300, 10)\r\n# 600\r\n# (60, 1, 10)\r\n# 23\r\n# 24\r\n# 25\r\n# 26\r\n# 27\r\n# 26\r\n# 26\r\n# {1, 2, 3, 4, 5}\r\n# i am not decorated\r\n# f3_a=(), f3_b=1\r\n# set()\r\n\r\n####################################################################################################\r\n# If you get any errors, try using quotes --f3_a \"(1,2,3,4,5)\"\r\n# .\\python.exe .\\a2.py --f2_a 12 --f2_b 300 --f1_a 60 --f3_a \"(1,2,3,4,5)\" --f3_b 22\r\n\r\n# 36000\r\n# (12, 300, 10)\r\n# 600\r\n# (60, 1, 10)\r\n# 23\r\n# 24\r\n# 25\r\n# 26\r\n# 27\r\n# {1, 2, 3, 4, 5}\r\n# i am not decorated\r\n# f3_a=(), f3_b=1\r\n# set()\r\n\r\n\r\n####################################################################################################\r\n```\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Hacky argparser - parses arguments and changes the defaults of functions",
"version": "0.12",
"split_keywords": [
"argparser"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "764ffb1bfe962e896d241c7d316b6e99b63f15bf1032ee659f3378e9b3cc1ee4",
"md5": "fc5529083b820c385ef5d72dcc2bc6cc",
"sha256": "cf8a3eceb12b9066cf60d4ec064450071fab2dd778d819194a2feef5a1cfd4c2"
},
"downloads": -1,
"filename": "hackyargparser-0.12-py3-none-any.whl",
"has_sig": false,
"md5_digest": "fc5529083b820c385ef5d72dcc2bc6cc",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 6537,
"upload_time": "2023-04-15T00:30:17",
"upload_time_iso_8601": "2023-04-15T00:30:17.187599Z",
"url": "https://files.pythonhosted.org/packages/76/4f/fb1bfe962e896d241c7d316b6e99b63f15bf1032ee659f3378e9b3cc1ee4/hackyargparser-0.12-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "8e85d1134dd736305d7dd2d39593cf7bf2f559a67b2bb8f4b155ac9e264211f9",
"md5": "846e3cbe307676fafbffd30d35277f15",
"sha256": "cbda05f9198f900277ac25c871cc395ea092f28c34501cc12c93f97c1b686642"
},
"downloads": -1,
"filename": "hackyargparser-0.12.tar.gz",
"has_sig": false,
"md5_digest": "846e3cbe307676fafbffd30d35277f15",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 4743,
"upload_time": "2023-04-15T00:30:18",
"upload_time_iso_8601": "2023-04-15T00:30:18.664313Z",
"url": "https://files.pythonhosted.org/packages/8e/85/d1134dd736305d7dd2d39593cf7bf2f559a67b2bb8f4b155ac9e264211f9/hackyargparser-0.12.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-04-15 00:30:18",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "hansalemaos",
"github_project": "hackyargparser",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "hackyargparser"
}