python的新手,我正在使用cmd处理交互式shell,它接受定义的函数和参数,并以if / then格式返回值。以下示例。但我的问题是,每次用户键入一个函数并向用户返回一些内容时,如果他们只是按Enter键,那么他们就会收到最后提供的输出。
例如下面的代码......但基本上如果用户没有为此输入参数或函数,我只想要什么,只显示""或空的空间。我认为else pass论证会为我做,但显然不是。我试过if和elif,结果相同。
使用" test arp"输出示例,只需按Enter键返回上一个按下的参数:
user@hostname>test arp
? (192.168.50.1) at 0:26:88:38:c6:48 on en0 ifscope [ethernet]
? (192.168.50.255) at ff:ff:ff:ff:ff:ff on en0 ifscope [ethernet]
> **<enter key>**
? (192.168.50.1) at 0:26:88:38:c6:48 on en0 ifscope [ethernet]
? (192.168.50.255) at ff:ff:ff:ff:ff:ff on en0 ifscope [ethernet]
> **<enter key>**
示例代码: #!/ usr / bin / env python
from cmd import Cmd
from time import ctime
import csv, os, subprocess, getpass, socket, pwd, re, select, shutil, subprocess, sys
syntaxError = "Sorry that syntax isn't quite right..."
class cliPrompt(Cmd):
def do_test(self, args):
if len(args) == 0:
print syntaxError
if args == '?':
print 'want to read the help?'
if args == 'arp':
subprocess.call('arp -an', shell=True)
if args == 'cpu':
subprocess.call('grep "model name" /proc/cpuinfo', shell=True)
if args == 'firewall':
subprocess.call('iptables -L -v -n --line-numbers', shell=True)
if args == 'interfaces':
subprocess.call('ifconfig', shell=True)
else:
pass
if __name__ == '__main__':
prompt = cliPrompt()
prompt.prompt = '>'
prompt.cmdloop()