Closed brijeshsirpatil closed 3 weeks ago
If returning a value worked at any point in the past then that was a bug in cmd2. The while not stop
behavior is consistent with the behavior of the python core library class cmd2 is based upon.
See: https://github.com/python/cpython/blob/main/Lib/cmd.py#L128
Then please ignore the comment about intended behavior. I used CMD2 in last job, wrote something 2 years ago where functions were returning data ( I forget the details). At a new job now, I am starting fresh, downloaded latest everything and ran into this issue. I am out of my depth here beyond observing the hack seems to work for the simple example.
Python's core cmd
library says "the return value is a flag indicating whether interpretation of commands by the interpreter should stop."
Because of this, cmd2
added a member called self.last_result
.
# Stores results from the last command run to enable usage of results in Python shells and pyscripts
self.last_result: Any = None
All you have to do is populate self.last_result
in your do_*
command function and check it after the command function returns. All of our built-in commands actually do this.
Thank you this is helpful. I had not come across self.last_result
, now that I know about it, I will use this feature to return values.
If your function returns a value, the command loop is being terminated. In the example below, the function is returning integer value 25,when you try this code you will see that executing the command results in cmd loop being terminated.
This used to work previously, probably in older release of Python. I recently got the latest version 3.12.4 and I find that it does not work any more
`
`
The issue seems to be in the _cmd_loop() function, the line "while not stop :" is no longer working as intended. The 'stop' variable at this point is integer value 25 which is being converted to true and the loop exits.
`
`
Python programming is not my day job, so don't know the proper way to fix this. My hack for now is to change the line of code to explicitly check for Boolean True value.
while not (stop == True):