minimal arg parser that can pass on the cli arguments to parse_args class method arguments as per the declarative type definition. (works fine on windows & linux)
Sample code as shown below can read arguments in specified type as per function signature. (refer use_minimal.py under EXAMPLES/)
frompy4cli.minimalimportarg_parser# Multiple arguments exampleclassmulti_args(arg_parser):
# example parse_args template function with multiple arguments of different typesdefparse_args(self,
inp_int: int=6,
inp_float: float=6.0,
inp_str: str="Six",
inp_list: list= [6, 6.0, "Six"],
inp_dict: dict= {'int': 6, 'float': 6.0, 'str': "Six"},
inp_bool: bool=False) ->dict:
""" Six arguments of different data type can be passed any value of the respective data type can be passed for specific argument. for defaults refer above the function returns a dict containing all the arguments and its values. cmds : 1. python <__file__> 10 10.0 "Seven" "[10, 10.0, 'Seven']" "{'int':10, 'float':10.0, 'str':'Seven'}" True 2. python <__file__> -inp_int=10 -inp_float=10.0 -inp_str="Seven" -inp_list="[10, 10.0, 'Seven']" -inp_dict="{'int':10, 'float':10.0, 'str':'Seven'}" -inp_bool=True """return {
'inp_int': inp_int,
'inp_float': inp_float,
'inp_str': inp_str,
'inp_list': inp_list,
'inp_dict': inp_dict,
'inp_bool': inp_bool
}
if__name__=='__main__':
importsysimportjsonprint(sys.argv)
obj=multi_args()
print("")
ifobj.returned:
out_dict=obj.returned.copy()
print(json.dumps(out_dict, indent=2, sort_keys=True), type(obj.returned))
else:
print(obj.returned, type(obj.returned))
To get help on how to use the script, execute python use_minimal.py -h or python use_minimal.py --help which will generate the doc content based on the comments in script as shown below.
(examples) D:\GitRepos\py4cli\EXAMPLES>python use_minimal.py --help
['use_minimal.py', '--help']
| > def parse_args
|
| Description :
|
| example parse_args template function with multiple arguments of different types
|
| Arguments :
|
| -inp_int: int = 6
| -inp_float: float = 6.0
| -inp_str: str = 'Six'
| -inp_list: list = [6, 6.0, 'Six']
| -inp_dict: dict = {'int': 6, 'float': 6.0, 'str': 'Six'}
| -inp_bool: bool = False
|
| Usage :
|
| Six arguments of different data type can be passed
| any value of the respective data type can be passed for specific argument. for defaults refer above
| the function returns a dict containing all the arguments and its values.
|
| cmds :
| 1. python D:\GitRepos\py4cli\EXAMPLES\use_minimal.py 10 10.0 "Seven" "[10, 10.0, 'Seven']" "{'int':10, 'float':10.0, 'str':'Seven'}" True
| 2. python D:\GitRepos\py4cli\EXAMPLES\use_minimal.py -inp_int=10 -inp_float=10.0 -inp_str="Seven" -inp_list="[10, 10.0, 'Seven']" -inp_dict="{'int':10, 'float':10.0, 'str':'Seven'}" -inp_bool=True
|
| -> dict (Returnable)
None <class 'NoneType'>
The commands specified can be used to alter the values as command line arguments to the script.
Vertically scalable version of minimal arg parser, aimed at use case like, hyper parameter tuning.
Sample code as shown below can read arguments in specified type as per function signature. (refer use_moderate.py under EXAMPLES/)
frompy4cli.moderateimportarg_parser# Multiple arguments exampleclassvscaled_args(arg_parser):
# example multi_args template function with multiple arguments of different typesdefmulti_args1(self,
inp_int: int=6,
inp_float: float=6.0,
inp_str: str="Six",
inp_list: list= [6, 6.0, "Six"],
inp_dict: dict= {'int': 6, 'float': 6.0, 'str': "Six"},
inp_bool: bool=False) ->dict:
""" Six arguments of different data type can be passed any value of the respective data type can be passed for specific argument. for defaults refer above the function returns a dict containing all the arguments and its values. cmds : 1. python <__file__> ~<__func__> 10 10.0 "Seven" "[10, 10.0, 'Seven']" "{'int':10, 'float':10.0, 'str':'Seven'}" True 2. python <__file__> ~<__func__> -inp_int=10 -inp_float=10.0 -inp_str="Seven" -inp_list="[10, 10.0, 'Seven']" -inp_dict="{'int':10, 'float':10.0, 'str':'Seven'}" -inp_bool=True """return {
'inp_int': inp_int,
'inp_float': inp_float,
'inp_str': inp_str,
'inp_list': inp_list,
'inp_dict': inp_dict,
'inp_bool': inp_bool
}
# example multi_args template function with multiple arguments of different typesdefmulti_args2(self,
inp_int: int=6,
inp_float: float=6.0,
inp_str: str="Six",
inp_list: list= [6, 6.0, "Six"],
inp_dict: dict= {'int': 6, 'float': 6.0, 'str': "Six"},
inp_bool: bool=False) ->dict:
""" Six arguments of different data type can be passed any value of the respective data type can be passed for specific argument. for defaults refer above the function returns a dict containing all the arguments and its values. cmds : 1. python <__file__> ~<__func__> 10 10.0 "Seven" "[10, 10.0, 'Seven']" "{'int':10, 'float':10.0, 'str':'Seven'}" True 2. python <__file__> ~<__func__> -inp_int=10 -inp_float=10.0 -inp_str="Seven" -inp_list="[10, 10.0, 'Seven']" -inp_dict="{'int':10, 'float':10.0, 'str':'Seven'}" -inp_bool=True """return {
'inp_int': inp_int,
'inp_float': inp_float,
'inp_str': inp_str,
'inp_list': inp_list,
'inp_dict': inp_dict,
'inp_bool': inp_bool
}
if__name__=='__main__':
importsysimportjsonprint(sys.argv)
obj=vscaled_args()
print("")
ifobj.returned:
out_dict=obj.returned.copy()
print(json.dumps(out_dict, indent=2, sort_keys=True), type(obj.returned))
else:
print(obj.returned, type(obj.returned))
To get help on how to use the script, execute python use_moderate.py -h or python use_moderate.py --help which will generate the doc content based on the comments in script as shown below.
(examples) D:\GitRepos\py4cli\EXAMPLES>python use_moderate.py
['use_moderate.py']
Methods available for use, is listed below
[
"~multi_args1",
"~multi_args2"
]
None <class 'NoneType'>
(examples) D:\GitRepos\py4cli\EXAMPLES>python use_moderate.py --help
['use_moderate.py', '--help']
| > def multi_args1
|
| Description :
|
| example multi_args template function with multiple arguments of different types
|
| Arguments :
|
| -inp_int: int = 6
| -inp_float: float = 6.0
| -inp_str: str = 'Six'
| -inp_list: list = [6, 6.0, 'Six']
| -inp_dict: dict = {'int': 6, 'float': 6.0, 'str': 'Six'}
| -inp_bool: bool = False
|
| Usage :
|
| Six arguments of different data type can be passed
| any value of the respective data type can be passed for specific argument. for defaults refer above
| the function returns a dict containing all the arguments and its values.
|
| cmds :
| 1. python D:\GitRepos\py4cli\EXAMPLES\use_moderate.py ~multi_args1 10 10.0 "Seven" "[10, 10.0, 'Seven']" "{'int':10, 'float':10.0, 'str':'Seven'}" True
| 2. python D:\GitRepos\py4cli\EXAMPLES\use_moderate.py ~multi_args1 -inp_int=10 -inp_float=10.0 -inp_str="Seven" -inp_list="[10, 10.0, 'Seven']" -inp_dict="{'int':10, 'float':10.0, 'str':'Seven'}" -inp_bool=True
|
| -> dict (Returnable)
| > def multi_args2
|
| Description :
|
| example multi_args template function with multiple arguments of different types
|
| Arguments :
|
| -inp_int: int = 6
| -inp_float: float = 6.0
| -inp_str: str = 'Six'
| -inp_list: list = [6, 6.0, 'Six']
| -inp_dict: dict = {'int': 6, 'float': 6.0, 'str': 'Six'}
| -inp_bool: bool = False
|
| Usage :
|
| Six arguments of different data type can be passed
| any value of the respective data type can be passed for specific argument. for defaults refer above
| the function returns a dict containing all the arguments and its values.
|
| cmds :
| 1. python D:\GitRepos\py4cli\EXAMPLES\use_moderate.py ~multi_args2 10 10.0 "Seven" "[10, 10.0, 'Seven']" "{'int':10, 'float':10.0, 'str':'Seven'}" True
| 2. python D:\GitRepos\py4cli\EXAMPLES\use_moderate.py ~multi_args2 -inp_int=10 -inp_float=10.0 -inp_str="Seven" -inp_list="[10, 10.0, 'Seven']" -inp_dict="{'int':10, 'float':10.0, 'str':'Seven'}" -inp_bool=True
|
| -> dict (Returnable)
None <class 'NoneType'>
The commands specified can be used to alter the values as command line arguments to the script. The methods can be selected with appropriate arguments as per definition, and the order of execution of the methods is also controllable. For illustration purpose only two methods [multi_args1, multi_args2] is defined which can be scaled to number of functions, as much as python supports.
The Tidelift Subscription provides access to a continuously curated stream of human-researched and maintainer-verified data on open source packages and their licenses, releases, vulnerabilities, and development practices.