python 利用cmd库实现模拟终端指令输入
###########################################################
import cmd
import subprocess
import sys
from robot import *
############################################################
# def parse(arg):
# return tuple(map(int, arg.split()))
class Cli(cmd.Cmd):
def __init__(self):
cmd.Cmd.__init__(self)
self.prompt = '(cli)>>'
pass
def do_bye(self, arg):
print('bye')
pass
# 命令格式 run add_item 1 2
def do_run(self, arg):
arg_list = arg.split()
if arg_list[0] == 'item_add':
pass
pass
def do_shell(self, arg):
subshell = subprocess.Popen(args, shell=True, stdin=None, stdout=None)
subshell.communicate()
subshell.terminate()
pass
def do_quit(self, arg):
return True
pass
def cmd_loop(self, robot):
self.cmdloop()
pass
if __name__ == '__main__':
cli = Cli()
cli.cmd_loop()
pass
注意:输入指令的开头默认是run