optfunc2 0.2.2__tar.gz → 0.2.4__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.
- {optfunc2-0.2.2 → optfunc2-0.2.4}/PKG-INFO +6 -23
- {optfunc2-0.2.2 → optfunc2-0.2.4}/README.md +3 -19
- {optfunc2-0.2.2 → optfunc2-0.2.4}/pyproject.toml +3 -4
- {optfunc2-0.2.2 → optfunc2-0.2.4}/src/optfunc2/parser.py +18 -7
- {optfunc2-0.2.2 → optfunc2-0.2.4}/LICENSE.txt +0 -0
- {optfunc2-0.2.2 → optfunc2-0.2.4}/src/optfunc2/__init__.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.3
|
|
2
2
|
Name: optfunc2
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.4
|
|
4
4
|
Summary: Generate command line options and help/tips from function automatically.
|
|
5
5
|
License: PyPA
|
|
6
6
|
Author: bajeer
|
|
@@ -16,9 +16,8 @@ Classifier: Programming Language :: Python :: 3.12
|
|
|
16
16
|
Classifier: Programming Language :: Python :: 3.13
|
|
17
17
|
Requires-Dist: docstring-parser (>=0.16,<0.17)
|
|
18
18
|
Requires-Dist: prettytable (==3.11.0)
|
|
19
|
-
Project-URL: Bug
|
|
20
|
-
Project-URL: Homepage, https://
|
|
21
|
-
Project-URL: Source, https://gitee.com/z-bajer/autocall
|
|
19
|
+
Project-URL: Bug Tracker, https://github.com/yjloong/optfunc2/issues
|
|
20
|
+
Project-URL: Homepage, https://github.com/yjloong/optfunc2
|
|
22
21
|
Description-Content-Type: text/markdown
|
|
23
22
|
|
|
24
23
|
# Call function directly in cmd line
|
|
@@ -33,24 +32,6 @@ Description-Content-Type: text/markdown
|
|
|
33
32
|
2. Function with @optfunc_default has @optfunc implicitly.
|
|
34
33
|
3. Not support two type of variadic arguments.
|
|
35
34
|
|
|
36
|
-
### ChangeLog
|
|
37
|
-
### 0.2.2 (2025-2-16)
|
|
38
|
-
1. Support single argument in bool type.
|
|
39
|
-
2. Don't need user to pass globals() in cmdline_start().
|
|
40
|
-
3. Support pytest to run test.
|
|
41
|
-
4. Support omitting called function name who is default.
|
|
42
|
-
5. pyproject.toml format change for poetry version 2.1.0.
|
|
43
|
-
|
|
44
|
-
#### 0.2.1 (2025-2-14)
|
|
45
|
-
1. Fix installing dependencies automatically.
|
|
46
|
-
2. Add function 'called_directly' used to check if the function is called as entry point.
|
|
47
|
-
This function can be used in function development.
|
|
48
|
-
|
|
49
|
-
#### 0.1.2 (2023-05-06)
|
|
50
|
-
1. Add support for default called functions.
|
|
51
|
-
2. Fix README.md.
|
|
52
|
-
3. Add ChangeLog in README.md.
|
|
53
|
-
|
|
54
35
|
### Code example1 -- calculator
|
|
55
36
|
``` python
|
|
56
37
|
from optfunc2 import cmdline, cmdline_default, cmdline_start
|
|
@@ -66,7 +47,7 @@ def add(a: float, b: float):
|
|
|
66
47
|
print(f"{a} + {b} = {a + b}")
|
|
67
48
|
|
|
68
49
|
@cmdline
|
|
69
|
-
def multiply(x: int, y: int = 5):
|
|
50
|
+
def multiply(x: int|float, y: int = 5):
|
|
70
51
|
"""multiply two numbers. The second number is optional.
|
|
71
52
|
|
|
72
53
|
Args:
|
|
@@ -138,6 +119,8 @@ Arguments:
|
|
|
138
119
|
2.3 + 3.0 = 5.3
|
|
139
120
|
~/optfunc2$ python src/example_calc.py multiply -x 3
|
|
140
121
|
3 × 5 = 15
|
|
122
|
+
~/optfunc2$ python src/example_calc.py multiply -x 2.3
|
|
123
|
+
2.3 × 5 = 11.5
|
|
141
124
|
~/optfunc2$ python src/example_calc.py stats --numbers '[1, 2, 3, 4, 5]'
|
|
142
125
|
sum: 15
|
|
143
126
|
average: 3.00
|
|
@@ -10,24 +10,6 @@
|
|
|
10
10
|
2. Function with @optfunc_default has @optfunc implicitly.
|
|
11
11
|
3. Not support two type of variadic arguments.
|
|
12
12
|
|
|
13
|
-
### ChangeLog
|
|
14
|
-
### 0.2.2 (2025-2-16)
|
|
15
|
-
1. Support single argument in bool type.
|
|
16
|
-
2. Don't need user to pass globals() in cmdline_start().
|
|
17
|
-
3. Support pytest to run test.
|
|
18
|
-
4. Support omitting called function name who is default.
|
|
19
|
-
5. pyproject.toml format change for poetry version 2.1.0.
|
|
20
|
-
|
|
21
|
-
#### 0.2.1 (2025-2-14)
|
|
22
|
-
1. Fix installing dependencies automatically.
|
|
23
|
-
2. Add function 'called_directly' used to check if the function is called as entry point.
|
|
24
|
-
This function can be used in function development.
|
|
25
|
-
|
|
26
|
-
#### 0.1.2 (2023-05-06)
|
|
27
|
-
1. Add support for default called functions.
|
|
28
|
-
2. Fix README.md.
|
|
29
|
-
3. Add ChangeLog in README.md.
|
|
30
|
-
|
|
31
13
|
### Code example1 -- calculator
|
|
32
14
|
``` python
|
|
33
15
|
from optfunc2 import cmdline, cmdline_default, cmdline_start
|
|
@@ -43,7 +25,7 @@ def add(a: float, b: float):
|
|
|
43
25
|
print(f"{a} + {b} = {a + b}")
|
|
44
26
|
|
|
45
27
|
@cmdline
|
|
46
|
-
def multiply(x: int, y: int = 5):
|
|
28
|
+
def multiply(x: int|float, y: int = 5):
|
|
47
29
|
"""multiply two numbers. The second number is optional.
|
|
48
30
|
|
|
49
31
|
Args:
|
|
@@ -115,6 +97,8 @@ Arguments:
|
|
|
115
97
|
2.3 + 3.0 = 5.3
|
|
116
98
|
~/optfunc2$ python src/example_calc.py multiply -x 3
|
|
117
99
|
3 × 5 = 15
|
|
100
|
+
~/optfunc2$ python src/example_calc.py multiply -x 2.3
|
|
101
|
+
2.3 × 5 = 11.5
|
|
118
102
|
~/optfunc2$ python src/example_calc.py stats --numbers '[1, 2, 3, 4, 5]'
|
|
119
103
|
sum: 15
|
|
120
104
|
average: 3.00
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "optfunc2"
|
|
3
|
-
version = "0.2.
|
|
3
|
+
version = "0.2.4"
|
|
4
4
|
description = "Generate command line options and help/tips from function automatically."
|
|
5
5
|
authors = [
|
|
6
6
|
{name = "bajeer",email = "z-bajeer@yeah.net"}
|
|
@@ -19,9 +19,8 @@ requires = ["poetry-core>=2.0.0,<3.0.0"]
|
|
|
19
19
|
build-backend = "poetry.core.masonry.api"
|
|
20
20
|
|
|
21
21
|
[project.urls]
|
|
22
|
-
"Homepage" = "https://
|
|
23
|
-
"Bug
|
|
24
|
-
"Source" = "https://gitee.com/z-bajer/autocall"
|
|
22
|
+
"Homepage" = "https://github.com/yjloong/optfunc2"
|
|
23
|
+
"Bug Tracker" = "https://github.com/yjloong/optfunc2/issues"
|
|
25
24
|
|
|
26
25
|
[tool.poetry.group.dev.dependencies]
|
|
27
26
|
pytest = "^8.3.4"
|
|
@@ -3,10 +3,12 @@ import prettytable
|
|
|
3
3
|
import docstring_parser
|
|
4
4
|
import sys
|
|
5
5
|
import ast
|
|
6
|
+
import types
|
|
7
|
+
from typing import Callable, Any, List
|
|
6
8
|
|
|
7
9
|
# functions taged by @cmdline
|
|
8
|
-
registered_funcs = []
|
|
9
|
-
registered_func_default = None
|
|
10
|
+
registered_funcs: List[Callable[..., Any]] = []
|
|
11
|
+
registered_func_default: Callable[..., Any] = None
|
|
10
12
|
|
|
11
13
|
# Main eval function in this module must be called only once.
|
|
12
14
|
_called_func = None
|
|
@@ -93,7 +95,7 @@ def decode_opts(arg_pairs, func: callable):
|
|
|
93
95
|
raise ValueError(f'--{name} need more arguments.')
|
|
94
96
|
|
|
95
97
|
try:
|
|
96
|
-
if anno
|
|
98
|
+
if anno not in [dict, list, inspect.Signature.empty] and type(anno) != types.UnionType:
|
|
97
99
|
value = anno(val)
|
|
98
100
|
else:
|
|
99
101
|
try:
|
|
@@ -104,7 +106,13 @@ def decode_opts(arg_pairs, func: callable):
|
|
|
104
106
|
anno_new = type(value)
|
|
105
107
|
|
|
106
108
|
if anno != inspect.Signature.empty:
|
|
107
|
-
if
|
|
109
|
+
if type(anno) == types.UnionType:
|
|
110
|
+
type_list = [i.strip() for i in str(anno).split('|')]
|
|
111
|
+
if anno_new.__name__ not in type_list:
|
|
112
|
+
raise Exception(f'Type of {opt} should be one of {repr(type_list)}.')
|
|
113
|
+
|
|
114
|
+
elif anno_new != anno:
|
|
115
|
+
print(f'{anno_new = } {anno = }')
|
|
108
116
|
raise ValueError(f'Value type of {opt} should be {anno.__name__}.')
|
|
109
117
|
|
|
110
118
|
anno = anno_new
|
|
@@ -131,7 +139,7 @@ def hibit_variadic(func: callable):
|
|
|
131
139
|
color_end()
|
|
132
140
|
exit(1)
|
|
133
141
|
|
|
134
|
-
def cmdline_default(func):
|
|
142
|
+
def cmdline_default(func: Callable[..., Any]) -> Callable[..., Any]:
|
|
135
143
|
global registered_func_default
|
|
136
144
|
global registered_funcs
|
|
137
145
|
|
|
@@ -152,7 +160,7 @@ def cmdline_default(func):
|
|
|
152
160
|
# globals()[funcname] = func
|
|
153
161
|
return func
|
|
154
162
|
|
|
155
|
-
def cmdline(func):
|
|
163
|
+
def cmdline(func: Callable[..., Any]) -> Callable[..., Any]:
|
|
156
164
|
global registered_funcs
|
|
157
165
|
if func not in registered_funcs:
|
|
158
166
|
registered_funcs.append(func)
|
|
@@ -161,7 +169,7 @@ def cmdline(func):
|
|
|
161
169
|
return func
|
|
162
170
|
|
|
163
171
|
|
|
164
|
-
def cmd_help(func:
|
|
172
|
+
def cmd_help(func: Callable[..., Any], has_abbrev: bool = True):
|
|
165
173
|
args_list = decode_func_args(func)
|
|
166
174
|
|
|
167
175
|
func_desc = docstring_parser.parse(func.__doc__).description
|
|
@@ -361,6 +369,9 @@ def cmdline_start(globals = None, locals = None, *, argv = sys.argv, header_doc:
|
|
|
361
369
|
help(header_doc)
|
|
362
370
|
return
|
|
363
371
|
|
|
372
|
+
if not registered_func_default and argv[1][0] == '-':
|
|
373
|
+
return
|
|
374
|
+
|
|
364
375
|
if argv[1][0] == '-':
|
|
365
376
|
# use the default function
|
|
366
377
|
argv.insert(1, registered_func_default.__name__)
|
|
File without changes
|
|
File without changes
|