Skip to content

Commit 69fea21

Browse files
committed
Fixed bug in argreparse
1 parent aa02b75 commit 69fea21

4 files changed

Lines changed: 20 additions & 17 deletions

File tree

src/tinyscript/VERSION.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.30.4
1+
1.30.6

src/tinyscript/argreparse.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,7 @@ def get_tool_globals():
7373

7474

7575
class ProxyArgumentParser(object):
76-
"""
77-
Proxy class for collecting added arguments before initialization.
78-
"""
76+
""" Proxy class for collecting added arguments before initialization. """
7977
def __getattr__(self, name):
8078
""" Each time a method is called, return __collect to make it capture the input arguments and keyword-arguments
8179
if it exists in the original parser class. """
@@ -231,7 +229,7 @@ def add_argument(self, *args, **kwargs):
231229
action.orig = orig
232230
action.prefix = prefix
233231
action.suffix = suffix
234-
return args[-1]
232+
return action
235233
except ArgumentError:
236234
# drop the argument if conflict and cancel set to True
237235
if cancel:
@@ -295,7 +293,11 @@ class _NewArgumentGroup(_ArgumentGroup, _NewActionsContainer):
295293
class _NewMutuallyExclusiveGroup(_MutuallyExclusiveGroup, _NewArgumentGroup):
296294
""" Alternative argparse._MutuallyExclusiveGroup for modifying arguments mutually exclusive groups handling in the
297295
modified ActionsContainer. """
298-
pass
296+
def add_argument(self, *args, **kwargs):
297+
if len(args) == 1 and len(kwargs) == 0 and isinstance(args[0], Action):
298+
self._group_actions.append(args[0])
299+
return args[0]
300+
return super(_NewMutuallyExclusiveGroup, self).add_argument(*args, **kwargs)
299301

300302

301303
class ArgumentParser(BaseArgumentParser, _NewActionsContainer):
@@ -859,6 +861,7 @@ def add_argument(self, action):
859861
if len(categories) > 1 or len(categories) > 0 and list(categories.keys())[0] != "default":
860862
action.categories = categories
861863
self._add_item(self._format_action, [action])
864+
return action
862865

863866

864867
class Namespace(BaseNamespace):

src/tinyscript/helpers/fexec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def execute(cmd, **kwargs):
5656
os.killpg(os.getpgid(p.pid), signal.SIGTERM)
5757
out, err = p.communicate()
5858
if rr:
59-
raise
59+
raise TimeoutExpired(__set_cmd(cmd, shell=True), to)
6060
return (out, err, p.returncode) if rc else (out, err)
6161

6262

src/tinyscript/parser.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def __proxy_to_real_parser(value):
121121
if isinstance(value, ProxyArgumentParser):
122122
return __parsers[value]
123123
elif isinstance(value, (list, tuple)):
124-
return [__proxy_to_real_parser(_) for _ in value]
124+
return [__proxy_to_real_parser(v) for v in value]
125125
return value
126126
# now iterate over the registered calls
127127
from .argreparse import parser_calls
@@ -145,13 +145,13 @@ def __proxy_to_real_parser(value):
145145
note=gt("this overrides other arguments"))
146146
c.add_argument("-w", "--write-config", metavar="INI", help=gt("write args to a config file"))
147147
if noarg and noargs_action == "config":
148-
tokens[1:] = [opt, "config.ini"]
148+
tokens[1:] = [opt.option_strings[-1], "config.ini"]
149149
# demonstration feature, for executing an example amongst these defined in __examples__, useful for observing what
150150
# the tool does
151151
if add['demo']:
152152
opt = i.add_argument("--demo", action="demo", prefix="play", help=gt("demonstrate a random example"))
153153
if noarg and noargs_action == "demo":
154-
tokens[1:] = [opt]
154+
tokens[1:] = [opt.option_strings[-1]]
155155
# help feature, for displaying classical or extended help about the tool
156156
if add['help']:
157157
if glob.get('__details__'):
@@ -176,25 +176,25 @@ def __proxy_to_real_parser(value):
176176
j.add_argument("--port", default=12345, type=port_number, prefix="remote",
177177
help=gt("remote interacting port"))
178178
if noarg and noargs_action == "interact":
179-
tokens[1:] = [opt]
179+
tokens[1:] = [opt.option_strings[-1]]
180180
set_interact_items(glob)
181181
# notification feature, for displaying notifications during the execution
182182
if add['notify']:
183183
opt = i.add_argument("-n", "--notify", action="store_true", suffix="mode", help=gt("notify mode"))
184184
if noarg and noargs_action == "notify":
185-
tokens[1:] = [opt]
185+
tokens[1:] = [opt.option_strings[-1]]
186186
set_notify_items(glob)
187187
# progress mode feature, for displaying a progress bar during the execution
188188
if add['progress']:
189189
opt = i.add_argument("-p", "--progress", action="store_true", suffix="mode", help=gt("progress mode"))
190190
if noarg and noargs_action == "progress":
191-
tokens[1:] = [opt]
191+
tokens[1:] = [opt.option_strings[-1]]
192192
set_progress_items(glob)
193193
# stepping mode feature, for stepping within the tool during its execution, especially useful for debugging
194194
if add['step']:
195195
opt = i.add_argument("--step", action="store_true", last=True, suffix="mode", help=gt("stepping mode"))
196196
if noarg and noargs_action == "step":
197-
tokens[1:] = [opt]
197+
tokens[1:] = [opt.option_strings[-1]]
198198
set_step_items(glob)
199199
# timing mode feature, for measuring time along the execution of the tool
200200
if add['time']:
@@ -204,15 +204,15 @@ def __proxy_to_real_parser(value):
204204
b.add_argument("--timings", action='store_true', last=True, suffix="mode",
205205
help=gt("display time stats during execution"))
206206
if noarg and noargs_action == "time":
207-
tokens[1:] = [opt]
207+
tokens[1:] = [opt.option_strings[-1]]
208208
# version feature, for displaying the version from __version__
209209
if add['version']:
210210
version = glob['__version__'] if '__version__' in glob else None
211211
if version is not None:
212212
opt = i.add_argument("--version", action='version', prefix="show", version=version,
213213
help=gt("show program's version number and exit"))
214214
if noarg and noargs_action == "version":
215-
tokens[1:] = [opt]
215+
tokens[1:] = [opt.option_strings[-1]]
216216
# verbosity feature, for displaying debugging messages, with the possibility to handle multi-level verbosity
217217
if multi_level_debug:
218218
i.add_argument("-v", dest="verbose", default=0, action="count", suffix="mode", cancel=True, last=True,
@@ -223,7 +223,7 @@ def __proxy_to_real_parser(value):
223223
if add['wizard']:
224224
opt = i.add_argument("-w", "--wizard", action="wizard", prefix="start", help=gt("start a wizard"))
225225
if noarg and noargs_action == "wizard":
226-
tokens[1:] = [opt]
226+
tokens[1:] = [opt.option_strings[-1]]
227227
# reporting feature, for making a reporting with the results of the tool at the end of its execution
228228
if report_func is not None:
229229
if not isfunction(report_func):

0 commit comments

Comments
 (0)