diff --git a/andeboxlib/cli.py b/andeboxlib/cli.py index 4ea2ef5..8b23485 100755 --- a/andeboxlib/cli.py +++ b/andeboxlib/cli.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +# PYTHON_ARGCOMPLETE_OK # (c) 2021-2022, Alexei Znamensky # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) @@ -18,6 +19,8 @@ from .exceptions import AndeboxException from .util import set_dir +import argcomplete + # # Adapted from: @@ -67,7 +70,9 @@ def _make_parser() -> argparse.ArgumentParser: help="path to the virtual environment where andebox and ansible are installed", type=Path, ) - subparser = parser.add_subparsers(dest="action", required=True) + subparser = parser.add_subparsers( + dest="action", required=True, metavar=" ".join([a.name for a in actions]) + ) for action in actions: action.make_parser(subparser) @@ -76,22 +81,25 @@ def _make_parser() -> argparse.ArgumentParser: class AndeBox: - def __init__(self) -> None: + def __init__(self, args: argparse.Namespace) -> None: self.actions = {ac.name: ac() for ac in actions} - self.parser = _make_parser() + self.args = args def run(self): - args = self.parser.parse_args() - context = create_context(args) + context = create_context(self.args) with set_dir(context.base_dir): - action = self.actions[args.action] + action = self.actions[self.args.action] action.run(context) def run(): + parser = _make_parser() + argcomplete.autocomplete(parser) + args = parser.parse_args() + try: signal.signal(signal.SIGPIPE, signal.SIG_DFL) - box = AndeBox() + box = AndeBox(args) box.run() return 0 except KeyboardInterrupt: diff --git a/pyproject.toml b/pyproject.toml index a86b51f..d5f6a65 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -50,6 +50,7 @@ tox = "^4.5.2" virtualenv-pyenv = "*" bump2version = "^1.0.1" ruamel-yaml = "^0.18.6" +argcomplete = "^3.5.3" [tool.poetry.group.dev.dependencies] pytest = ">=7.3.1,<9.0.0"