python - 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 并在“界面”之后点击两次时,我看到了
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, text, line, begidx, 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()
推荐阅读
- ios - 当应用程序从后台删除时,iOS11 处理 Firebase 推送通知
- python-3.x - AttributeError:模块'cv2.cv2'在python 3.6.4和opencv 3.4中没有属性'createLBPHFaceRecognizer'
- plsql - 当问号是转换错误时,计算字符串中的问号
- java - cucumber using java - 从运行器类执行测试时出现空指针异常
- android - Beta用户提示下载同版本公版App
- r - 如何在闪避的 geom_col 上方对齐 geom_text 标签
- java - Telegram Bot,内联按钮
- sqlite - SQLite - 计数不同的三列分组忽略最后一个分组
- c# - 如何在asp.net core中获取客户端的计算机名
- css - 在角 5 的三行之后显示省略号