shell - Multiple modes for line-oriented command interpreters with Python Cmd class -


i trying build cli (line-oriented command interpreters) using python cmd class support 2 or more modes, each mode have different set of commands, commands switch between them.

currently implemented 2 modes using 2 separate classes each mode, set next class execute in state variable:

class opmode(cmd):     def do_show(self, line):         :     def do_configure(self, line): # switch configmode         ctx.state = 'config'         return true  class configmode(cmd):     def do_set(self, line):         :      def do_exit(self, line):  # go opmode         ctx.state = 'op'         return true  # in main ... while 1:     if ctx.state == 'op':         opcli.cmdloop()     elif ctx.state == 'conf':         confcli.cmdloop()     else:         break 

is there way achieve same thing single cmd class?

using single cmd instance not improve code. keep track of state inside cmd class , each command action adjust respone accordingly.

however, want clearer transition between different states. can follows:

def do_configure(self, line):     config = configmode(...)     config.cmdloop() 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -