cmdchopper 0.1.0__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.
- cmdchopper-0.1.0/LICENSE +21 -0
- cmdchopper-0.1.0/PKG-INFO +14 -0
- cmdchopper-0.1.0/README.md +0 -0
- cmdchopper-0.1.0/pyproject.toml +27 -0
- cmdchopper-0.1.0/setup.cfg +4 -0
- cmdchopper-0.1.0/src/cmdchopper/__init__.py +0 -0
- cmdchopper-0.1.0/src/cmdchopper/parser.py +257 -0
- cmdchopper-0.1.0/src/cmdchopper.egg-info/PKG-INFO +14 -0
- cmdchopper-0.1.0/src/cmdchopper.egg-info/SOURCES.txt +9 -0
- cmdchopper-0.1.0/src/cmdchopper.egg-info/dependency_links.txt +1 -0
- cmdchopper-0.1.0/src/cmdchopper.egg-info/top_level.txt +1 -0
cmdchopper-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 0x4D2
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cmdchopper
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A simple yet powerful command string parser
|
|
5
|
+
Author-email: 0x4D2 <1706119930@qq.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/0x4D25F2/cmdchopper
|
|
8
|
+
Project-URL: Issues, https://github.com/0x4D25F2/cmdchopper/issues
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Dynamic: license-file
|
|
File without changes
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "cmdchopper"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A simple yet powerful command string parser"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = {text = "MIT"}
|
|
11
|
+
authors = [
|
|
12
|
+
{name = "0x4D2", email = "1706119930@qq.com"}
|
|
13
|
+
]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"License :: OSI Approved :: MIT License",
|
|
17
|
+
]
|
|
18
|
+
requires-python = ">=3.8"
|
|
19
|
+
dependencies = []
|
|
20
|
+
|
|
21
|
+
[project.urls]
|
|
22
|
+
Homepage = "https://github.com/0x4D25F2/cmdchopper"
|
|
23
|
+
Issues = "https://github.com/0x4D25F2/cmdchopper/issues"
|
|
24
|
+
|
|
25
|
+
[tool.setuptools]
|
|
26
|
+
package-dir = {"" = "src"}
|
|
27
|
+
packages = ["cmdchopper"]
|
|
File without changes
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
# pip install cmdchopper
|
|
2
|
+
# Default type: str
|
|
3
|
+
# Arg[name(,type,checker,default)](.blocked())
|
|
4
|
+
# Arg["a3"]["*args"] #wildcard: list
|
|
5
|
+
# Arg["a4"]["#text"] #wildcard: str
|
|
6
|
+
# Checker(checker_function,failed_message)
|
|
7
|
+
|
|
8
|
+
SUCCESS = 1
|
|
9
|
+
INPUT_FAILED = -1
|
|
10
|
+
ARG_FAILED = 0
|
|
11
|
+
CHECKER_FAILED = -2
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class Checker:
|
|
15
|
+
def __init__(self, func, msg):
|
|
16
|
+
self.func = func
|
|
17
|
+
self.msg = msg
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
no_checker = Checker(lambda x: True, "[你不应该看到这条消息...]")
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def minn(n):
|
|
24
|
+
return Checker(lambda x: x >= n, "必须大于{}".format(n))
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def maxn(n):
|
|
28
|
+
return Checker(lambda x: x <= n, "必须小于{}".format(n))
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def among(a, b):
|
|
32
|
+
return Checker(lambda x: a <= x <= b, "必须在{}和{}直接".format(a, b))
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def enum(*l):
|
|
36
|
+
return Checker(lambda x: x in l, "必须是以下值之一: " + ", ".join(l))
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class Wildcard: pass
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class ArgClass:
|
|
43
|
+
def __init__(self, default: bool, wildcard: int, args=None):
|
|
44
|
+
self.args = args.copy() if args else []
|
|
45
|
+
self.default = default
|
|
46
|
+
self.wildcard = wildcard
|
|
47
|
+
self.block = False
|
|
48
|
+
|
|
49
|
+
def blocked(self):
|
|
50
|
+
self.block = True
|
|
51
|
+
return self
|
|
52
|
+
|
|
53
|
+
def _clone(self):
|
|
54
|
+
return ArgClass(self.default, self.wildcard, self.args)
|
|
55
|
+
|
|
56
|
+
def __getitem__(self, arg_spec):
|
|
57
|
+
if arg_spec is None:
|
|
58
|
+
return self
|
|
59
|
+
if not isinstance(arg_spec, tuple):
|
|
60
|
+
arg_spec = (arg_spec, str, None)
|
|
61
|
+
if len(arg_spec) > 4:
|
|
62
|
+
raise SyntaxError("too many arguments")
|
|
63
|
+
if not isinstance(arg_spec[0], str):
|
|
64
|
+
raise SyntaxError("arg_name must be a string")
|
|
65
|
+
if not arg_spec[0]:
|
|
66
|
+
raise SyntaxError("arg_name is empty")
|
|
67
|
+
if not isinstance(arg_spec[1], type):
|
|
68
|
+
raise SyntaxError("arg_type must be a type")
|
|
69
|
+
if len(arg_spec) == 4 and not isinstance(arg_spec[3], arg_spec[1]):
|
|
70
|
+
raise SyntaxError("default value is not paired with arg_type")
|
|
71
|
+
if len(arg_spec) <= 2 or arg_spec[2] is None:
|
|
72
|
+
if len(arg_spec) <= 3:
|
|
73
|
+
arg_spec = (arg_spec[0], arg_spec[1], no_checker)
|
|
74
|
+
else:
|
|
75
|
+
arg_spec = (arg_spec[0], arg_spec[1], no_checker, arg_spec[3])
|
|
76
|
+
if arg_spec[0][0] in ("*", "#"):
|
|
77
|
+
if self.wildcard == 0:
|
|
78
|
+
if arg_spec[0][0] == "*":
|
|
79
|
+
self.wildcard = 1
|
|
80
|
+
elif arg_spec[0][0] == "#":
|
|
81
|
+
self.wildcard = 2
|
|
82
|
+
if len(arg_spec) == 4:
|
|
83
|
+
raise SyntaxError("no default value for wildcard arg")
|
|
84
|
+
arg_spec = list(arg_spec)
|
|
85
|
+
if arg_spec[0] != "*" and arg_spec[0] != "#":
|
|
86
|
+
arg_spec[0] = arg_spec[0][1:]
|
|
87
|
+
if self.wildcard == 1:
|
|
88
|
+
arg_spec[1] = list
|
|
89
|
+
else:
|
|
90
|
+
arg_spec[1] = str
|
|
91
|
+
arg_spec = tuple(arg_spec)
|
|
92
|
+
else:
|
|
93
|
+
raise SyntaxError("too many wildcard arg")
|
|
94
|
+
elif self.wildcard:
|
|
95
|
+
raise SyntaxError("wildcard must be the last argument")
|
|
96
|
+
if len(arg_spec) == 4:
|
|
97
|
+
self.default = True
|
|
98
|
+
elif self.default and not self.wildcard:
|
|
99
|
+
raise SyntaxError("non-default argument follows default argument")
|
|
100
|
+
new = self._clone()
|
|
101
|
+
new.args.append(arg_spec)
|
|
102
|
+
return new
|
|
103
|
+
|
|
104
|
+
def __repr__(self):
|
|
105
|
+
output = "Args"
|
|
106
|
+
for i in self.args:
|
|
107
|
+
if len(i) == 4:
|
|
108
|
+
output += f"[{i[0]}: {i[1].__name__} = {i[3]}]"
|
|
109
|
+
else:
|
|
110
|
+
output += f"[{i[0]}: {i[1].__name__}]"
|
|
111
|
+
return output
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
class ArgInput:
|
|
115
|
+
def __getitem__(self, arg):
|
|
116
|
+
a = ArgClass(False, 0)
|
|
117
|
+
return a[arg]
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
class ArgResult:
|
|
121
|
+
def __init__(self, kwargs, names):
|
|
122
|
+
self.arg_name = [i[0] for i in names]
|
|
123
|
+
for i in kwargs:
|
|
124
|
+
setattr(self, i, kwargs[i])
|
|
125
|
+
|
|
126
|
+
def __getitem__(self, index):
|
|
127
|
+
if isinstance(index, int):
|
|
128
|
+
index = self.arg_name[index]
|
|
129
|
+
if isinstance(index, str):
|
|
130
|
+
return getattr(self, index)
|
|
131
|
+
raise TypeError()
|
|
132
|
+
|
|
133
|
+
def __repr__(self):
|
|
134
|
+
arg_txt = []
|
|
135
|
+
for i in self.arg_name:
|
|
136
|
+
arg_txt.append(i + "=" + repr(self[i]))
|
|
137
|
+
return "ArgResult({})".format(", ".join(arg_txt))
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
Arg = ArgInput()
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def arg_split(text, prefixes=None, quote_str=True, max_arg=None):
|
|
144
|
+
if isinstance(prefixes, str):
|
|
145
|
+
prefixes = [prefixes]
|
|
146
|
+
arg_list = [""]
|
|
147
|
+
mode_str = False
|
|
148
|
+
mode_escape = False
|
|
149
|
+
if prefixes:
|
|
150
|
+
pc = False
|
|
151
|
+
text += " "
|
|
152
|
+
for i in prefixes: # Remove prefix
|
|
153
|
+
if text.startswith(i + " "):
|
|
154
|
+
if text.rstrip() == i.rstrip():
|
|
155
|
+
return True, []
|
|
156
|
+
text = text[len(i):].lstrip()
|
|
157
|
+
pc = True
|
|
158
|
+
break
|
|
159
|
+
if not pc:
|
|
160
|
+
return False, "未找到匹配的前缀"
|
|
161
|
+
text = " " + text.rstrip() + " "
|
|
162
|
+
for i in range(1, len(text) - 1):
|
|
163
|
+
if max_arg and len(arg_list) == max_arg:
|
|
164
|
+
arg_list[-1] += text[i:].strip()
|
|
165
|
+
break
|
|
166
|
+
if mode_escape:
|
|
167
|
+
arg_list[-1] += text[i]
|
|
168
|
+
mode_escape = False
|
|
169
|
+
continue
|
|
170
|
+
elif text[i] == "\\":
|
|
171
|
+
mode_escape = True
|
|
172
|
+
continue
|
|
173
|
+
if mode_str:
|
|
174
|
+
if text[i] == "\"":
|
|
175
|
+
if text[i + 1] != " ":
|
|
176
|
+
return False, "字符串参数后缺失空格"
|
|
177
|
+
mode_str = False
|
|
178
|
+
else:
|
|
179
|
+
arg_list[-1] += text[i]
|
|
180
|
+
else:
|
|
181
|
+
if quote_str and text[i] == "\"":
|
|
182
|
+
if text[i - 1] != " ":
|
|
183
|
+
return False, "引号前必须为空格"
|
|
184
|
+
mode_str = True
|
|
185
|
+
elif text[i] == " ":
|
|
186
|
+
if text[i - 1] != " ":
|
|
187
|
+
arg_list.append("")
|
|
188
|
+
else:
|
|
189
|
+
arg_list[-1] += text[i]
|
|
190
|
+
if mode_str:
|
|
191
|
+
return False, "字符串引号未闭合"
|
|
192
|
+
return True, arg_list
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
class CommandParser:
|
|
196
|
+
def __init__(self, args, prefixes=None, quote_str=True):
|
|
197
|
+
self.args = args
|
|
198
|
+
self.prefixes = prefixes
|
|
199
|
+
self.quote_str = quote_str
|
|
200
|
+
|
|
201
|
+
def _parse_single(self, output, args):
|
|
202
|
+
arg_list = {}
|
|
203
|
+
wc = args.wildcard == 1
|
|
204
|
+
args = args.args
|
|
205
|
+
arg_len = len(args)
|
|
206
|
+
if wc:
|
|
207
|
+
arg_len -= 1
|
|
208
|
+
i = -1
|
|
209
|
+
elif len(args) < len(output):
|
|
210
|
+
return ARG_FAILED, "提供了过多的参数"
|
|
211
|
+
for i in range(arg_len):
|
|
212
|
+
if i >= len(output):
|
|
213
|
+
if len(args[i]) == 4:
|
|
214
|
+
arg_list[args[i][0]] = args[i][3]
|
|
215
|
+
else:
|
|
216
|
+
return ARG_FAILED, "缺失参数:" + args[i][0]
|
|
217
|
+
else:
|
|
218
|
+
try:
|
|
219
|
+
output[i] = args[i][1](output[i])
|
|
220
|
+
except ValueError:
|
|
221
|
+
return ARG_FAILED, "{} 必须为 {} 类型,而非 {}".format(
|
|
222
|
+
args[i][0], args[i][1].__name__, type(output[i]).__name__)
|
|
223
|
+
if not args[i][2].func(output[i]):
|
|
224
|
+
return CHECKER_FAILED, args[i][0] + " " + args[i][2].msg
|
|
225
|
+
arg_list[args[i][0]] = output[i]
|
|
226
|
+
if wc:
|
|
227
|
+
arg_list[args[-1][0]] = output[i + 1:]
|
|
228
|
+
res = ArgResult(arg_list, args)
|
|
229
|
+
return SUCCESS, res
|
|
230
|
+
|
|
231
|
+
def parse(self, text):
|
|
232
|
+
single_arg = isinstance(self.args, ArgClass)
|
|
233
|
+
success, output = arg_split(text, self.prefixes, self.quote_str)
|
|
234
|
+
if not success:
|
|
235
|
+
if single_arg:
|
|
236
|
+
return INPUT_FAILED, output
|
|
237
|
+
else:
|
|
238
|
+
return INPUT_FAILED, output, -1
|
|
239
|
+
if single_arg:
|
|
240
|
+
if self.args.wildcard == 2:
|
|
241
|
+
_, output = arg_split(text, self.prefixes, self.quote_str,
|
|
242
|
+
len(self.args.args))
|
|
243
|
+
success, result = self._parse_single(output, self.args)
|
|
244
|
+
return success, result
|
|
245
|
+
else:
|
|
246
|
+
result = None
|
|
247
|
+
for i in range(len(self.args)):
|
|
248
|
+
if self.args[i].wildcard == 2:
|
|
249
|
+
_, output = arg_split(text, self.prefixes, self.quote_str,
|
|
250
|
+
len(self.args[i].args))
|
|
251
|
+
success, result = self._parse_single(output, self.args[i])
|
|
252
|
+
if success == SUCCESS:
|
|
253
|
+
return success, result, i
|
|
254
|
+
elif success == CHECKER_FAILED and self.args[i].block:
|
|
255
|
+
return success, result, -1
|
|
256
|
+
return success, result, -1
|
|
257
|
+
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: cmdchopper
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A simple yet powerful command string parser
|
|
5
|
+
Author-email: 0x4D2 <1706119930@qq.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/0x4D25F2/cmdchopper
|
|
8
|
+
Project-URL: Issues, https://github.com/0x4D25F2/cmdchopper/issues
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Dynamic: license-file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
cmdchopper
|