cmd2 icon indicating copy to clipboard operation
cmd2 copied to clipboard

onecmd returns False if command returns None, different from cmd module behavior

Open sn1kketysnake opened this issue 1 year ago • 0 comments

There is a difference in return value when comparing cmd2 and cmd because of the following line: https://github.com/python-cmd2/cmd2/blob/be1593929f046d44e0d27fcd902fdb9998ba6bb3/cmd2/cmd2.py#L3037

In cmd2 if the return of a do_ command is None then the return value will be changed to False, different from the behavior of cmd. I desire the cmd behavior of still returning exactly what the command returned. If indeed a None was returned or if no explicit return happened then it should return None.

The following tests illustrate the behavior. Notice how when running cmd, do_test_no_ret returns None where as the same command when running cmd2 returns False.

Was this intended behavior? I can change this behavior, but I have to override onecmd and copy and paste the entire def and remove the None --> False conversion.

cmd

Test Script:

import cmd
class cmds(cmd.Cmd):
 def onecmd(self, line, **kwargs):
  ret = super().onecmd(line, **kwargs)
  print(f'ret = {ret}')
  return ret
 def do_test_no_ret(self, p):
  print('test_no_ret')
 def do_test_ret(self, p):
  print('test_ret')
  return -1

cmds().cmdloop()

Output after sourcing the above and running the commands:

(Cmd) test_no_ret
test_no_ret
ret = None
(Cmd) test_ret
test_ret
ret = -1

cmd2

Test Script:

import cmd2
class cmds(cmd2.Cmd):
 def onecmd(self, line, **kwargs):
  ret = super().onecmd(line, **kwargs)
  print(f'ret = {ret}')
  return ret
 def do_test_no_ret(self, p):
  print('test_no_ret')
 def do_test_ret(self, p):
  print('test_ret')
  return -1

cmds().cmdloop()

Output after sourcing the above and running the commands:

(Cmd) test_no_ret
test_no_ret
ret = False
(Cmd) test_ret
test_ret
ret = -1
0

sn1kketysnake avatar Jan 24 '24 01:01 sn1kketysnake