Reputation: 16940
I am trying to write an interactive shell in python for administering different type of hardware for configuring and issuing some set of commands.
How it will work:
So, once I got into the interactive shell prompt, I need to type a hardware name say HardwareA, after that my shell prompt will change to HardwareA. Once i got a specific Hardware prompt, after that what ever cmd(s) or option(s) I type will parse and called a particular function or methods from HardwareA module.
To make this working, I was trying to get some idea using argparse
or optparse
along with cmd
module.
But so far I am not able to get a clear picture or any good docs to start with.
So, if anybody had some kind of solution or good link, please let me know and throw me some light.
Here is my snippet:
import cmd, shlex
import argparse
class ChooseHardware(cmd.Cmd):
"""Simple command processor example."""
hardware = [ 'netapp', 'isilon', 'ibm' ]
def do_netapp(self, argv):
parser = argparse.ArgumentParser(description='Process netapp argument.')
parser.add_argument('--qtree', dest='qtree',
help='qtree name')
args = parser.parse_args(argv.split())
print args
def do_isilon(self, argv):
pass
def do_ibm(self, argv):
pass
def do_EOF(self, line):
return True
def do_exit(self, s):
return True
def do_help(self, h):
print 'Unknown: hardware type'
def help_exit(self):
print "Exit the interpreter."
print "You can also use the Ctrl-D shortcut."
if __name__ == '__main__':
obj = ChooseHardware()
obj.prompt = 'cmd_prompt:'
obj.cmdloop()
Output:
yopy:/test$ python choose_hw.py
cmd_prompt:
cmd_prompt:netapp
Namespace(qtree=None)
cmd_prompt:netapp --qtree /opt/var
Namespace(qtree='/opt/var')
cmd_prompt:
Upvotes: 2
Views: 1108