如何使用 cmd2 自动完成参数解析器中的一个参数。
from cmd2 import Cmd, Cmd2ArgumentParser
import cmd2
numbers = ['0', '1', '2', '3', '4']
alphabet = ['a', 'b', 'c', 'd']
class Complete(Cmd):
parser = Cmd2ArgumentParser()
parser.add_argument("type", choices=['numbers', 'alphabet'])
parser.add_argument("value")
@cmd2.with_argparser(parser)
def do_list(self, args):
self.poutput(args.value)
if __name__ == "__main__":
app = Complete()
app.cmdloop()
使用此代码,我可以自动完成“类型”参数(在 add_argument 中进行选择)。我想根据“类型”参数自动完成“值”参数。如果值是“数字”,我用数字列表完成它。如果值是“字母”,我用字母列表完成它。
有什么方法可以正确实现这种行为?还是我应该实现自己的 complete_list 方法?
谢谢,