Skip to main content

A set of functions intended to make writing shell completion functions lot easier by letting you write them via python.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

completion_utils-0.0.2.tar.gz (7.6 kB view details)

Uploaded Source

File details

Details for the file completion_utils-0.0.2.tar.gz.

File metadata

  • Download URL: completion_utils-0.0.2.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for completion_utils-0.0.2.tar.gz
Algorithm Hash digest
SHA256 5eb2e0c77a3f04e22f6a9e4f861214ec6df81615350af24a2b73fbb2e39c6bc3
MD5 095abe13e6abef217604e264f479f46e
BLAKE2b-256 6c6fa64e8a1657620dbcbb2f22f0c3bf4b027e8c54e2795be02ed407695319a6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page