From 66fdd48fddc50bd3c97c2efb310aa2ac677e7a6e Mon Sep 17 00:00:00 2001 From: Carles Cufi Date: Wed, 8 May 2019 18:05:38 +0200 Subject: [PATCH] west: Add completion command Add a completion command that dumps the contents of a shell completion file present in the zephyr repository. Signed-off-by: Carles Cufi --- doc/guides/west/extensions.rst | 1 + scripts/west-commands.yml | 5 ++ scripts/west_commands/completion.py | 50 +++++++++++++++++++ .../completion/west-completion.bash | 13 +++++ 4 files changed, 69 insertions(+) create mode 100644 scripts/west_commands/completion.py diff --git a/doc/guides/west/extensions.rst b/doc/guides/west/extensions.rst index dd35f563a3..00a29c3594 100644 --- a/doc/guides/west/extensions.rst +++ b/doc/guides/west/extensions.rst @@ -18,6 +18,7 @@ help for them shows up like this in ``west --help``: .. code-block:: none commands from project at "zephyr": + completion: display shell completion scripts boards: display information about supported boards build: compile a Zephyr application sign: sign a Zephyr binary for bootloader chain-loading diff --git a/scripts/west-commands.yml b/scripts/west-commands.yml index 807489b1ee..81ff6306a0 100644 --- a/scripts/west-commands.yml +++ b/scripts/west-commands.yml @@ -1,5 +1,10 @@ # Keep the help strings in sync with the values in the .py files! west-commands: + - file: scripts/west_commands/completion.py + commands: + - name: completion + class: Completion + help: display shell completion scripts - file: scripts/west_commands/boards.py commands: - name: boards diff --git a/scripts/west_commands/completion.py b/scripts/west_commands/completion.py new file mode 100644 index 0000000000..46dfa4f1ec --- /dev/null +++ b/scripts/west_commands/completion.py @@ -0,0 +1,50 @@ +# Copyright (c) 2019 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: Apache-2.0 + +import argparse +import os + +from west import log +from west.commands import WestCommand + +# Relative to the folder where this script lives +COMPLETION_REL_PATH = 'completion/west-completion' + +class Completion(WestCommand): + + def __init__(self): + super().__init__( + 'completion', + # Keep this in sync with the string in west-commands.yml. + 'display shell completion scripts', + 'Display shell completion scripts.', + accepts_unknown_args=False) + + def do_add_parser(self, parser_adder): + parser = parser_adder.add_parser( + self.name, + help=self.help, + formatter_class=argparse.RawDescriptionHelpFormatter, + description=self.description) + + # Remember to update completion/west-completion.bash if you add or + # remove flags + parser.add_argument('shell', nargs=1, choices=['bash'], + help='''Select the shell that which the completion + script is intended for. + Currently only bash is supported.''') + return parser + + def do_run(self, args, unknown_args): + cf = os.path.join(os.path.dirname(os.path.realpath(__file__)), + *COMPLETION_REL_PATH.split('/')) + + cf += '.' + args.shell[0] + + try: + with open(cf, 'r') as f: + print(f.read()) + except FileNotFoundError as e: + log.die('Unable to find completion file: {}'.format(e)) + diff --git a/scripts/west_commands/completion/west-completion.bash b/scripts/west_commands/completion/west-completion.bash index 78434ace02..b434396b4e 100644 --- a/scripts/west_commands/completion/west-completion.bash +++ b/scripts/west_commands/completion/west-completion.bash @@ -575,6 +575,18 @@ __comp_west_help() } # Zephyr extension commands +__comp_west_completion() +{ + case "$cur" in + *) + local counter=$( __west_pos_first_nonflag "$(__west_to_extglob "$global_args_opts")" ) + if [ "$cword" -eq "$counter" ]; then + __set_comp "bash" + fi + ;; + esac +} + __comp_west_boards() { local boards_args_opts=" @@ -750,6 +762,7 @@ __comp_west() ) local zephyr_ext_cmds=( + completion boards build sign