def get_opts(ctx, args, incomplete):
""" auto complete for option "opt"
:param ctx: The current click context.
:param args: The list of arguments passed in.
:param incomplete: The partial word that is being completed, as a
string. May be an empty string '' if no characters have
been entered yet.
:return: list of possible choices
"""
opts = {
'foo1': ('bar11', 'bar21', 'bar31'),
'foo2': ('bar12', 'bar22', 'bar32'),
'fox3': ('bar13', 'bar23', 'bar33'),
}
if args[-1] == ' opt':
possible_choices = opts.keys()
elif args[-1] in opts:
possible_choices = opts[args[-1]]
else:
possible_choices = ()
return [arg for arg in possible_choices if arg.startswith(incomplete)]
要自动完成两个字符串(其中第二个字符串依赖于第一个字符串)的单击选项,不需要两个完成函数。您只需要一种方法来确定两个字符串中的哪一个当前正在完成。对于名为
opt
的选项,我们可以完成(str, str)
类型,如:代码:
使用自动完成
您可以通过
autocompletion
函数单击,如下所示:这是怎么回事?
autocompletion
函数被传递一个args
列表。当完成一个选项时,我们可以在args
中找到我们的选项名。在本例中,我们可以在args中查找opt
,以获得完成第一个或第二个字符串的位置的锚点。然后返回与已输入字符匹配的字符串。你知道吗测试代码:
测试结果:
相关问题 更多 >
编程相关推荐