一组函数,通过让您通过python编写它们,使编写shell完成函数变得更加容易。
completion-utils的Python项目详细描述
完成用途
一组轻量级函数,旨在使用python编写shell完成函数更容易。使用pip install completion-utils
安装并从completion_utils
导入所需内容。通常您只需要导入bash_completion_decorator
,并用它装饰函数。
用法
假设你已经完成了这样简单的工作…
#!/usr/bin/env python3
import sys
def foobar():
return ["a", "bunch", "of", "potential", "matches"]
def completion_hook(cmd, curr_word, prev_word):
potential_matches = foobar()
matches = [k for k in potential_matches if k.startswith(curr_word)]
return matches
def main():
results = completion_hook(*sys.argv[1:])
if len(results):
print("\n".join(results))
if __name__ == "__main__":
main()
然后,您可以用bash_completion_decorator
来修饰completion_hook
,使shell完成自动提供必要的参数,并使用bash完成来测试潜在完成的结果。
#!/usr/bin/env python3
import sys
import unittest
from completion_utils import bash_completion_decorator, bash_complete
class CompletionTestCase_up(unittest.TestCase):
def test_all_options_show(self):
expected = "\n".join(sorted(foobar()))
actual = bash_complete("heylook ", __file__)
def test_one_option(self):
expected = "m"
actual = bash_complete("heylook m", __file__)
def foobar():
return ["a", "bunch", "of", "potential", "matches"]
@bash_completion_decorator
def completion_hook(cmd, curr_word, prev_word):
potential_matches = foobar()
matches = [k for k in potential_matches if k.startswith(curr_word)]
return matches
if __name__ == "__main__":
completion_hook()