How to structure a python cmd application which has separate command modules
I'm writing开发者_StackOverflow社区 a python command-line tool using the cmd2 module which is an enhanced version of cmd. The tool is an administration application and is used to administer a number of sub-modules.
I'd like to structure the code so that each sub-module has its own class responsible for providing the commands that can be executed on that module. But I'm not sure how to structure this given how the command class works.
So what I'd like is something like:
import cmd
class ConsoleApp(cmd.Cmd):
# this is the main app and should get the commands from the sub-modules
app = ConsoleApp()
app.cmdloop()
And then the sub-modules would be separately defined.
class SubModuleA():
def do_sm_a_command_1(self, line):
print "sm a command 1"
class SubModuleB():
def do_sm_b_command_1(self, line):
print "sm b command 2"
How can I structure this so that the main app will pick up the commands from the sub-modules?
Thanks, Jon
You may have some luck structuring your SubModules as Plugins to the main ConsoleApp. You'd need a couple of new methods on ConsoleApp. Something like add_command_module(self, klass)
which would just append the klass (SubModuleA for example) to some list inside ConsoleApp.
Then in ConsoleApp, override the onecmd
method to look something like this
def onecmd(self, line):
if not line:
return self.emptyline()
if cmd is None:
return self.default(line)
self.lastcmd = line
if cmd == '':
return self.default(line)
else:
# iterate over all submodules that have been added including ourselves
# self.submodules would just be a list or set of all submodules as mentioned above
func = None
for submod in self.submodules:
# just return the first match we find
if hasattr(submod, 'do_%s' % cmd):
func = getattr(submod, 'do_%s' % cmd)
break # instead of breaking, you could also add up all the modules that have
# this method, and tell the user to be more specific
if func is not None:
return func(arg)
else:
return self.default(line)
You could also hack up the parseline
method to recognize submodule prefixes for commands etc...
精彩评论