@@ -199,28 +199,29 @@ def _run(self):
199199 self ._command_name ))
200200
201201 # reconstruct the cmd2 command from the python call
202- cmd_str = [ '' ]
202+ command = self . _command_name + ' '
203203
204204 def process_argument (action , value ):
205+ nonlocal command
205206 if isinstance (action , argparse ._CountAction ):
206207 if isinstance (value , int ):
207208 for _ in range (value ):
208- cmd_str [ 0 ] += '{} ' .format (action .option_strings [0 ])
209+ command += '{} ' .format (action .option_strings [0 ])
209210 return
210211 else :
211212 raise TypeError ('Expected int for ' + action .dest )
212213 if isinstance (action , argparse ._StoreConstAction ) or isinstance (action , argparse ._AppendConstAction ):
213214 if value :
214215 # 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 ])
216217 return
217218 else :
218219 # value is not True so we default to false, which means don't include the flag
219220 return
220221
221222 # was the argument a flag?
222223 if action .option_strings :
223- cmd_str [ 0 ] += '{} ' .format (action .option_strings [0 ])
224+ command += '{} ' .format (action .option_strings [0 ])
224225
225226 is_remainder_arg = action .dest == self ._remainder_arg
226227
@@ -231,33 +232,34 @@ def process_argument(action, value):
231232 raise ValueError ('{} appears to be a flag and should be supplied as a keyword argument '
232233 'to the function.' .format (item ))
233234 item = quote_string_if_needed (item )
234- cmd_str [ 0 ] += '{} ' .format (item )
235+ command += '{} ' .format (item )
235236
236237 # If this is a flag parameter that can accept a variable number of arguments and we have not
237238 # reached the max number, add a list completion suffix to tell argparse to move to the next
238239 # parameter
239240 if action .option_strings and isinstance (action , _RangeAction ) and action .nargs_max is not None and \
240241 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 ])
242243
243244 else :
244245 value = str (value ).strip ()
245246 if not is_remainder_arg and is_potential_flag (value , self ._parser ):
246247 raise ValueError ('{} appears to be a flag and should be supplied as a keyword argument '
247248 'to the function.' .format (value ))
248249 value = quote_string_if_needed (value )
249- cmd_str [ 0 ] += '{} ' .format (value )
250+ command += '{} ' .format (value )
250251
251252 # If this is a flag parameter that can accept a variable number of arguments and we have not
252253 # reached the max number, add a list completion suffix to tell argparse to move to the next
253254 # parameter
254255 if action .option_strings and isinstance (action , _RangeAction ) and action .nargs_max is not None and \
255256 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 ])
257258
258259 def process_action (action ):
260+ nonlocal command
259261 if isinstance (action , argparse ._SubParsersAction ):
260- cmd_str [ 0 ] += '{} ' .format (self ._args [action .dest ])
262+ command += '{} ' .format (self ._args [action .dest ])
261263 traverse_parser (action .choices [self ._args [action .dest ]])
262264 elif isinstance (action , argparse ._AppendAction ):
263265 if isinstance (self ._args [action .dest ], list ) or isinstance (self ._args [action .dest ], tuple ):
@@ -284,8 +286,9 @@ def traverse_parser(parser):
284286 process_action (action )
285287
286288 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 )
289292
290293
291294class PyscriptBridge (object ):
@@ -310,9 +313,7 @@ def __getattr__(self, item: str):
310313 else :
311314 # Command doesn't use argparse, we will accept parameters in the form of a command string
312315 def wrap_func (args = '' ):
313- command = item
314- if args :
315- command += ' ' + args
316+ command = (item + ' ' + args ).strip ()
316317 return _exec_cmd (self ._cmd2_app ,
317318 functools .partial (self ._cmd2_app .onecmd_plus_hooks , command + '\n ' ),
318319 self .cmd_echo )
0 commit comments