cmdshell中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()

最新更新