@@ -45,8 +45,14 @@ def __bool__(self) -> bool:
45
45
return not self .stderr
46
46
47
47
48
- def _exec_cmd (cmd2_app , func : Callable , echo : bool ) -> CommandResult :
49
- """Helper to encapsulate executing a command and capturing the results"""
48
+ def _exec_cmd (cmd2_app , command : str , echo : bool ) -> CommandResult :
49
+ """
50
+ Helper to encapsulate executing a command and capturing the results
51
+ :param cmd2_app: cmd2 app that will run the command
52
+ :param command: command line being run
53
+ :param echo: if True, output will be echoed to stdout/stderr while the command runs
54
+ :return: result of the command
55
+ """
50
56
copy_stdout = StdSim (sys .stdout , echo )
51
57
copy_stderr = StdSim (sys .stderr , echo )
52
58
@@ -58,7 +64,8 @@ def _exec_cmd(cmd2_app, func: Callable, echo: bool) -> CommandResult:
58
64
cmd2_app .stdout = copy_cmd_stdout
59
65
with redirect_stdout (copy_stdout ):
60
66
with redirect_stderr (copy_stderr ):
61
- func ()
67
+ # Include a newline in case it's a multiline command
68
+ cmd2_app .onecmd_plus_hooks (command + '\n ' )
62
69
finally :
63
70
cmd2_app .stdout = copy_cmd_stdout .inner_stream
64
71
@@ -199,29 +206,29 @@ def _run(self):
199
206
self ._command_name ))
200
207
201
208
# reconstruct the cmd2 command from the python call
202
- command = self ._command_name + ' '
209
+ command = self ._command_name
203
210
204
211
def process_argument (action , value ):
205
212
nonlocal command
206
213
if isinstance (action , argparse ._CountAction ):
207
214
if isinstance (value , int ):
208
215
for _ in range (value ):
209
- command += '{} ' .format (action .option_strings [0 ])
216
+ command += ' {} ' .format (action .option_strings [0 ])
210
217
return
211
218
else :
212
219
raise TypeError ('Expected int for ' + action .dest )
213
220
if isinstance (action , argparse ._StoreConstAction ) or isinstance (action , argparse ._AppendConstAction ):
214
221
if value :
215
222
# Nothing else to append to the command string, just the flag is enough.
216
- command += '{} ' .format (action .option_strings [0 ])
223
+ command += ' {} ' .format (action .option_strings [0 ])
217
224
return
218
225
else :
219
226
# value is not True so we default to false, which means don't include the flag
220
227
return
221
228
222
229
# was the argument a flag?
223
230
if action .option_strings :
224
- command += '{} ' .format (action .option_strings [0 ])
231
+ command += ' {} ' .format (action .option_strings [0 ])
225
232
226
233
is_remainder_arg = action .dest == self ._remainder_arg
227
234
@@ -232,34 +239,34 @@ def process_argument(action, value):
232
239
raise ValueError ('{} appears to be a flag and should be supplied as a keyword argument '
233
240
'to the function.' .format (item ))
234
241
item = quote_string_if_needed (item )
235
- command += '{} ' .format (item )
242
+ command += ' {} ' .format (item )
236
243
237
244
# If this is a flag parameter that can accept a variable number of arguments and we have not
238
245
# reached the max number, add a list completion suffix to tell argparse to move to the next
239
246
# parameter
240
247
if action .option_strings and isinstance (action , _RangeAction ) and action .nargs_max is not None and \
241
248
action .nargs_max > len (value ):
242
- command += '{0}{0} ' .format (self ._parser .prefix_chars [0 ])
249
+ command += ' {0}{0}' .format (self ._parser .prefix_chars [0 ])
243
250
244
251
else :
245
252
value = str (value ).strip ()
246
253
if not is_remainder_arg and is_potential_flag (value , self ._parser ):
247
254
raise ValueError ('{} appears to be a flag and should be supplied as a keyword argument '
248
255
'to the function.' .format (value ))
249
256
value = quote_string_if_needed (value )
250
- command += '{} ' .format (value )
257
+ command += ' {} ' .format (value )
251
258
252
259
# If this is a flag parameter that can accept a variable number of arguments and we have not
253
260
# reached the max number, add a list completion suffix to tell argparse to move to the next
254
261
# parameter
255
262
if action .option_strings and isinstance (action , _RangeAction ) and action .nargs_max is not None and \
256
263
action .nargs_max > 1 :
257
- command += '{0}{0} ' .format (self ._parser .prefix_chars [0 ])
264
+ command += ' {0}{0}' .format (self ._parser .prefix_chars [0 ])
258
265
259
266
def process_action (action ):
260
267
nonlocal command
261
268
if isinstance (action , argparse ._SubParsersAction ):
262
- command += '{} ' .format (self ._args [action .dest ])
269
+ command += ' {} ' .format (self ._args [action .dest ])
263
270
traverse_parser (action .choices [self ._args [action .dest ]])
264
271
elif isinstance (action , argparse ._AppendAction ):
265
272
if isinstance (self ._args [action .dest ], list ) or isinstance (self ._args [action .dest ], tuple ):
@@ -286,9 +293,7 @@ def traverse_parser(parser):
286
293
process_action (action )
287
294
288
295
traverse_parser (self ._parser )
289
- return _exec_cmd (self ._cmd2_app ,
290
- functools .partial (self ._cmd2_app .onecmd_plus_hooks , command .strip () + '\n ' ),
291
- self ._echo )
296
+ return _exec_cmd (self ._cmd2_app , command , self ._echo )
292
297
293
298
294
299
class PyscriptBridge (object ):
@@ -313,10 +318,10 @@ def __getattr__(self, item: str):
313
318
else :
314
319
# Command doesn't use argparse, we will accept parameters in the form of a command string
315
320
def wrap_func (args = '' ):
316
- command = ( item + ' ' + args ). strip ()
317
- return _exec_cmd ( self . _cmd2_app ,
318
- functools . partial ( self . _cmd2_app . onecmd_plus_hooks , command + ' \n ' ),
319
- self .cmd_echo )
321
+ command = item
322
+ if args :
323
+ command += ' ' + args
324
+ return _exec_cmd ( self . _cmd2_app , command , self .cmd_echo )
320
325
321
326
return wrap_func
322
327
else :
@@ -329,17 +334,15 @@ def __dir__(self):
329
334
attributes .insert (0 , 'cmd_echo' )
330
335
return attributes
331
336
332
- def __call__ (self , args : str , echo : Optional [bool ]= None ) -> CommandResult :
337
+ def __call__ (self , command : str , echo : Optional [bool ]= None ) -> CommandResult :
333
338
"""
334
339
Provide functionality to call application commands by calling PyscriptBridge
335
340
ex: app('help')
336
- :param args: The string being passed to the command
337
- :param echo: If True, output will be echoed while the command runs
338
- This temporarily overrides the value of self.cmd_echo
341
+ :param command: command line being run
342
+ :param echo: if True, output will be echoed to stdout/stderr while the command runs
343
+ this temporarily overrides the value of self.cmd_echo
339
344
"""
340
345
if echo is None :
341
346
echo = self .cmd_echo
342
347
343
- return _exec_cmd (self ._cmd2_app ,
344
- functools .partial (self ._cmd2_app .onecmd_plus_hooks , args + '\n ' ),
345
- echo )
348
+ return _exec_cmd (self ._cmd2_app , command , echo )
0 commit comments