Skip to main content

Hacky argparser - parses arguments and changes the defaults of functions

Project description

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()


####################################################################################################

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hackyargparser-0.12.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hackyargparser-0.12-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file hackyargparser-0.12.tar.gz.

File metadata

  • Download URL: hackyargparser-0.12.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for hackyargparser-0.12.tar.gz
Algorithm Hash digest
SHA256 cbda05f9198f900277ac25c871cc395ea092f28c34501cc12c93f97c1b686642
MD5 846e3cbe307676fafbffd30d35277f15
BLAKE2b-256 8e85d1134dd736305d7dd2d39593cf7bf2f559a67b2bb8f4b155ac9e264211f9

See more details on using hashes here.

File details

Details for the file hackyargparser-0.12-py3-none-any.whl.

File metadata

  • Download URL: hackyargparser-0.12-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for hackyargparser-0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 cf8a3eceb12b9066cf60d4ec064450071fab2dd778d819194a2feef5a1cfd4c2
MD5 fc5529083b820c385ef5d72dcc2bc6cc
BLAKE2b-256 764ffb1bfe962e896d241c7d316b6e99b63f15bf1032ee659f3378e9b3cc1ee4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page