@@ -199,28 +199,29 @@ def _run(self):
199
199
self ._command_name ))
200
200
201
201
# reconstruct the cmd2 command from the python call
202
- cmd_str = [ '' ]
202
+ command = self . _command_name + ' '
203
203
204
204
def process_argument (action , value ):
205
+ nonlocal command
205
206
if isinstance (action , argparse ._CountAction ):
206
207
if isinstance (value , int ):
207
208
for _ in range (value ):
208
- cmd_str [ 0 ] += '{} ' .format (action .option_strings [0 ])
209
+ command += '{} ' .format (action .option_strings [0 ])
209
210
return
210
211
else :
211
212
raise TypeError ('Expected int for ' + action .dest )
212
213
if isinstance (action , argparse ._StoreConstAction ) or isinstance (action , argparse ._AppendConstAction ):
213
214
if value :
214
215
# Nothing else to append to the command string, just the flag is enough.
215
- cmd_str [ 0 ] += '{} ' .format (action .option_strings [0 ])
216
+ command += '{} ' .format (action .option_strings [0 ])
216
217
return
217
218
else :
218
219
# value is not True so we default to false, which means don't include the flag
219
220
return
220
221
221
222
# was the argument a flag?
222
223
if action .option_strings :
223
- cmd_str [ 0 ] += '{} ' .format (action .option_strings [0 ])
224
+ command += '{} ' .format (action .option_strings [0 ])
224
225
225
226
is_remainder_arg = action .dest == self ._remainder_arg
226
227
@@ -231,33 +232,34 @@ def process_argument(action, value):
231
232
raise ValueError ('{} appears to be a flag and should be supplied as a keyword argument '
232
233
'to the function.' .format (item ))
233
234
item = quote_string_if_needed (item )
234
- cmd_str [ 0 ] += '{} ' .format (item )
235
+ command += '{} ' .format (item )
235
236
236
237
# If this is a flag parameter that can accept a variable number of arguments and we have not
237
238
# reached the max number, add a list completion suffix to tell argparse to move to the next
238
239
# parameter
239
240
if action .option_strings and isinstance (action , _RangeAction ) and action .nargs_max is not None and \
240
241
action .nargs_max > len (value ):
241
- cmd_str [ 0 ] += '{0}{0} ' .format (self ._parser .prefix_chars [0 ])
242
+ command += '{0}{0} ' .format (self ._parser .prefix_chars [0 ])
242
243
243
244
else :
244
245
value = str (value ).strip ()
245
246
if not is_remainder_arg and is_potential_flag (value , self ._parser ):
246
247
raise ValueError ('{} appears to be a flag and should be supplied as a keyword argument '
247
248
'to the function.' .format (value ))
248
249
value = quote_string_if_needed (value )
249
- cmd_str [ 0 ] += '{} ' .format (value )
250
+ command += '{} ' .format (value )
250
251
251
252
# If this is a flag parameter that can accept a variable number of arguments and we have not
252
253
# reached the max number, add a list completion suffix to tell argparse to move to the next
253
254
# parameter
254
255
if action .option_strings and isinstance (action , _RangeAction ) and action .nargs_max is not None and \
255
256
action .nargs_max > 1 :
256
- cmd_str [ 0 ] += '{0}{0} ' .format (self ._parser .prefix_chars [0 ])
257
+ command += '{0}{0} ' .format (self ._parser .prefix_chars [0 ])
257
258
258
259
def process_action (action ):
260
+ nonlocal command
259
261
if isinstance (action , argparse ._SubParsersAction ):
260
- cmd_str [ 0 ] += '{} ' .format (self ._args [action .dest ])
262
+ command += '{} ' .format (self ._args [action .dest ])
261
263
traverse_parser (action .choices [self ._args [action .dest ]])
262
264
elif isinstance (action , argparse ._AppendAction ):
263
265
if isinstance (self ._args [action .dest ], list ) or isinstance (self ._args [action .dest ], tuple ):
@@ -284,8 +286,9 @@ def traverse_parser(parser):
284
286
process_action (action )
285
287
286
288
traverse_parser (self ._parser )
287
-
288
- return _exec_cmd (self ._cmd2_app , functools .partial (func , cmd_str [0 ]), self ._echo )
289
+ return _exec_cmd (self ._cmd2_app ,
290
+ functools .partial (self ._cmd2_app .onecmd_plus_hooks , command .strip () + '\n ' ),
291
+ self ._echo )
289
292
290
293
291
294
class PyscriptBridge (object ):
@@ -310,9 +313,7 @@ def __getattr__(self, item: str):
310
313
else :
311
314
# Command doesn't use argparse, we will accept parameters in the form of a command string
312
315
def wrap_func (args = '' ):
313
- command = item
314
- if args :
315
- command += ' ' + args
316
+ command = (item + ' ' + args ).strip ()
316
317
return _exec_cmd (self ._cmd2_app ,
317
318
functools .partial (self ._cmd2_app .onecmd_plus_hooks , command + '\n ' ),
318
319
self .cmd_echo )
0 commit comments