Skip to main content

completion_utils

A lightweight set of functions intended to make writing shell completion functions in python easier. Install with pip install completion-utils and import what you need from completion_utils. Generally you only need to import bash_completion_decorator, and decorate a function with it.

Usage

Supposing you have some simple completion like this already...

#!/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()

You can then decorate completion_hook with bash_completion_decorator to have shell completion automatically supply the necessary arguments and use bash_complete to test out the results of potential completions.

#!/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()

Release files for completion-utils 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for completion-utils 0.0.2
File Size Uploaded
completion_utils-0.0.2.tar.gz 7.6 kB Details

Release files / completion_utils-0.0.2.tar.gz

Download URL completion_utils-0.0.2.tar.gz
Size 7.6 kB
Tags Source
SHA-256 checksum
How to use checksums
5eb2e0c77a3f04e22f6a9e4f861214ec6df81615350af24a2b73fbb2e39c6bc3
BLAKE2b-256 checksum
How to use checksums
6c6fa64e8a1657620dbcbb2f22f0c3bf4b027e8c54e2795be02ed407695319a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

Release history Release notifications | RSS feed

This release

0.0.2 This release

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page