diff --git a/pmb/helpers/frontend.py b/pmb/helpers/frontend.py index 924741cb56f88821a71d677ef1c13b73be61bfce..d786494408595f9e62286c4a87031254dd4d15eb 100644 --- a/pmb/helpers/frontend.py +++ b/pmb/helpers/frontend.py @@ -363,3 +363,31 @@ def bootimg_analyze(args): for line in pmb.aportgen.device.generate_deviceinfo_fastboot_content(args, bootimg).split("\n"): tmp_output += "\n" + line.lstrip() logging.info(tmp_output) + + +def register_bash_completion(args): + try: + import argcomplete + except ImportError: + raise RuntimeError("Could not find argcomplete! You can install it " + "with 'pip3 install argcomplete'") + dest = os.path.abspath(os.path.expanduser(args.dest)) + command = os.path.basename(sys.argv[0]) + dest_file = os.path.join(dest, command) + if args.uninstall: + logging.info("Removing file {}".format(dest_file)) + os.remove(dest_file) + try: + os.removedirs(dest) + except OSError: + pass + else: + if "shellcode" not in dir(argcomplete): + raise RuntimeError("Argcomplete must be >=1.9.4. You can update " + "with 'pip3 install --upgrade argcomplete'") + logging.info("Writing to {}".format(dest_file)) + os.makedirs(dest, mode=0o755, exist_ok=True) + with open(dest_file, "w") as f: + f.write(argcomplete.shellcode(command)) + logging.info("Success! Please close and re-open your terminal to " + "enable bash completion") diff --git a/pmb/parse/arguments.py b/pmb/parse/arguments.py index 98e0aaf6676c137238203c87ba2272c4c0dfe19e..aad34d52a5cf178cb9e2a64e2f7a6d22062e1564 100644 --- a/pmb/parse/arguments.py +++ b/pmb/parse/arguments.py @@ -478,6 +478,16 @@ def arguments(): help="force even if the file seems to be" " invalid") + # Action: register_bash_completion + completion = sub.add_parser("register_bash_completion", + help="install bash completion script") + completion_default_dir = "~/.local/share/bash-completion/completions/" + completion.add_argument("--dest", default=completion_default_dir, + help="destination directory, default: {}".format( + completion_default_dir)) + completion.add_argument("--uninstall", action="store_true", + help="remove bash completion script") + if argcomplete: argcomplete.autocomplete(parser, always_complete_options="long") diff --git a/setup.py b/setup.py index 0dba1446d4625c4f63627638d83344eb8918bbb6..cbddc318e9c7513d08726d7d473c563ce8dd58f1 100644 --- a/setup.py +++ b/setup.py @@ -59,7 +59,7 @@ setup( tests_require=['pytest'], cmdclass={'test': PyTest}, extras_require={ - 'argcomplete': ["argcomplete"], + 'argcomplete': ["argcomplete>=1.9.4"], }, entry_points={ 'console_scripts': [