Capture do function results inside cmd2

67 Views Asked by At

Here is a simple script using the Python cmd2 library.

import cmd2

class App(cmd2.Cmd):

  result = 0

  # Long computation
  def do_add3(self, arg):
    r = 3 + int(arg)
    print(r)
    self.result = r

  # Scripting
  def do_test(self, arg):
      self.do_add3("4")
      self.do_add3("2")
      self.do_add3("1")
      print(self.result)

if __name__ == '__main__':
  app = App()
  app.cmdloop()

Gist link

The result of a do_add3 function is saved (returned) into self.result. Later that do_add3 was called internally with self.do_add3("4").

Is there a better way to return results from a do_ function, such that I can code like in the following?

self.do_add("4") + self.do_add("3") + self.do_add("2")

P.S. For example, I can't use return inside do_ function to return value. Is there a better way?

0

There are 0 best solutions below