如何解决cmd shell 中 complete_xxx 函数中不同列表的不同行为
我正在使用 cmd 模块创建一个简单的 shell。对于两个命令,我有两个不同的列表。一方面,完成按预期工作,但另一方面却没有。
[Good list] IntList="eth0","eth1","eth2"
[Bad List] IntList="heth-0-0","heth-0-1","heth-0-2","heth-0-3","heth-0-4"
我的日常工作如下:
def complete_interface(self,text,line,begidx,endidx):
return[ f for f in IntList if f.startswith(text)]
当我运行 shell 并在“interface”之后点击两次时,我看到
Delem(eth2): interface eth
eth0 eth1 eth2
Delem(eth2): interface eth
eth0 eth1 eth2
Delem(eth2): interface eth
但是当我使用另一个列表时,我得到
Delem(heth0-0): interface heth-0-heth-0-
No such interface: heth-0-heth-0-
Delem(heth0-0):
看看它是如何将起始字符串附加到第二个列表中的? 不知道如何调试这个...
解决方法
#!/usr/bin/env python3
import cmd
Good = ['eth0','eth1','eth2','eth3']
Bad = ['heth-0-0','heth-0-1','heth-0-2','heth-0-3' ]
Lists = ['Good','Bad']
List = Good
Int = 'eth0'
line='line'
class DelemCmd(cmd.Cmd):
"""Simple shell command processor for delem."""
global Int,List,Lists
prompt = "Test: "
def do_interface(self,arg):
global Int
"Set the interface to use."
Int = arg
def complete_interface(self,text,line,begidx,endidx):
"Complete interface names"
return [ f for f in List if f.startswith(text)]
def do_node(self,arg):
"Change the List we are using."
global List
if arg == "Good":
List = Good
elif arg == "Bad":
List = Bad
else:
print(f'Bad arg {arg}')
exit()
print(List)
def complete_node(self,endidx):
return [ f for f in Lists if f.startswith(text)]
def do_EOF(self,line):
"Ctrl-D to quit."
return True
def do_quit(self,line):
"Bye-bye!"
return True
def emptyline(self):
"""Called when an empty line is entered in response to the prompt."""
if self.lastcmd:
self.lastcmd = ""
return self.onecmd('\n')
DelemCmd().cmdloop()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。