From 8e6473d2a0f553667c3c249d9e92f5ee532b64da Mon Sep 17 00:00:00 2001 From: Evgeniy Alekseev Date: Thu, 31 Mar 2022 02:18:39 +0300 Subject: [PATCH] add help command --- src/ahriman/application/ahriman.py | 20 +++++++- src/ahriman/application/handlers/__init__.py | 1 + src/ahriman/application/handlers/help.py | 50 +++++++++++++++++++ .../application/handlers/test_handler_help.py | 41 +++++++++++++++ tests/ahriman/application/test_ahriman.py | 23 ++++++++- 5 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 src/ahriman/application/handlers/help.py create mode 100644 tests/ahriman/application/handlers/test_handler_help.py diff --git a/src/ahriman/application/ahriman.py b/src/ahriman/application/ahriman.py index a128a6b1..0ea32a7f 100644 --- a/src/ahriman/application/ahriman.py +++ b/src/ahriman/application/ahriman.py @@ -72,7 +72,8 @@ def _parser() -> argparse.ArgumentParser: subparsers = parser.add_subparsers(title="command", help="command to run", dest="command", required=True) _set_aur_search_parser(subparsers) - _set_help_commands_unsafe(subparsers) + _set_help_parser(subparsers) + _set_help_commands_unsafe_parser(subparsers) _set_key_import_parser(subparsers) _set_package_add_parser(subparsers) _set_package_remove_parser(subparsers) @@ -119,7 +120,22 @@ def _set_aur_search_parser(root: SubParserAction) -> argparse.ArgumentParser: return parser -def _set_help_commands_unsafe(root: SubParserAction) -> argparse.ArgumentParser: +def _set_help_parser(root: SubParserAction) -> argparse.ArgumentParser: + """ + add parser for listing help subcommand + :param root: subparsers for the commands + :return: created argument parser + """ + parser = root.add_parser("help", help="show help message", + description="show help message for application or command and exit", + formatter_class=_formatter) + parser.add_argument("command", help="show help message for specific command", nargs="?") + parser.set_defaults(handler=handlers.Help, architecture=[""], lock=None, no_report=True, quiet=True, + unsafe=True, parser=_parser) + return parser + + +def _set_help_commands_unsafe_parser(root: SubParserAction) -> argparse.ArgumentParser: """ add parser for listing unsafe commands :param root: subparsers for the commands diff --git a/src/ahriman/application/handlers/__init__.py b/src/ahriman/application/handlers/__init__.py index 9b0bd6b5..2a478798 100644 --- a/src/ahriman/application/handlers/__init__.py +++ b/src/ahriman/application/handlers/__init__.py @@ -22,6 +22,7 @@ from ahriman.application.handlers.handler import Handler from ahriman.application.handlers.add import Add from ahriman.application.handlers.clean import Clean from ahriman.application.handlers.dump import Dump +from ahriman.application.handlers.help import Help from ahriman.application.handlers.key_import import KeyImport from ahriman.application.handlers.patch import Patch from ahriman.application.handlers.rebuild import Rebuild diff --git a/src/ahriman/application/handlers/help.py b/src/ahriman/application/handlers/help.py new file mode 100644 index 00000000..b1be3a2b --- /dev/null +++ b/src/ahriman/application/handlers/help.py @@ -0,0 +1,50 @@ +# +# Copyright (c) 2021-2022 ahriman team. +# +# This file is part of ahriman +# (see https://github.com/arcan1s/ahriman). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +import argparse + +from typing import Type + +from ahriman.application.handlers.handler import Handler +from ahriman.core.configuration import Configuration + + +class Help(Handler): + """ + help handler + """ + + ALLOW_AUTO_ARCHITECTURE_RUN = False # it should be called only as "no-architecture" + + @classmethod + def run(cls: Type[Handler], args: argparse.Namespace, architecture: str, + configuration: Configuration, no_report: bool, unsafe: bool) -> None: + """ + callback for command line + :param args: command line args + :param architecture: repository architecture + :param configuration: configuration instance + :param no_report: force disable reporting + :param unsafe: if set no user check will be performed before path creation + """ + parser: argparse.ArgumentParser = args.parser() + if args.command is None: + parser.parse_args(["--help"]) + else: + parser.parse_args([args.command, "--help"]) diff --git a/tests/ahriman/application/handlers/test_handler_help.py b/tests/ahriman/application/handlers/test_handler_help.py new file mode 100644 index 00000000..6a79db36 --- /dev/null +++ b/tests/ahriman/application/handlers/test_handler_help.py @@ -0,0 +1,41 @@ +import argparse + +from pytest_mock import MockerFixture + +from ahriman.application.ahriman import _parser +from ahriman.application.handlers import Help +from ahriman.core.configuration import Configuration + + +def _default_args(args: argparse.Namespace) -> argparse.Namespace: + """ + default arguments for these test cases + :param args: command line arguments fixture + :return: generated arguments for these test cases + """ + args.parser = _parser + args.command = None + return args + + +def test_run(args: argparse.Namespace, configuration: Configuration, mocker: MockerFixture) -> None: + """ + must run command + """ + args = _default_args(args) + parse_mock = mocker.patch("argparse.ArgumentParser.parse_args") + + Help.run(args, "x86_64", configuration, True, False) + parse_mock.assert_called_once_with(["--help"]) + + +def test_run_command(args: argparse.Namespace, configuration: Configuration, mocker: MockerFixture) -> None: + """ + must run command for specific subcommand + """ + args = _default_args(args) + args.command = "aur-search" + parse_mock = mocker.patch("argparse.ArgumentParser.parse_args") + + Help.run(args, "x86_64", configuration, True, False) + parse_mock.assert_called_once_with(["aur-search", "--help"]) diff --git a/tests/ahriman/application/test_ahriman.py b/tests/ahriman/application/test_ahriman.py index 34dacf65..54e156d2 100644 --- a/tests/ahriman/application/test_ahriman.py +++ b/tests/ahriman/application/test_ahriman.py @@ -65,6 +65,27 @@ def test_subparsers_aur_search_architecture(parser: argparse.ArgumentParser) -> assert args.architecture == [""] +def test_subparsers_help(parser: argparse.ArgumentParser) -> None: + """ + help command must imply architecture list, lock, no-report, quiet, unsafe and parser + """ + args = parser.parse_args(["help"]) + assert args.architecture == [""] + assert args.lock is None + assert args.no_report + assert args.quiet + assert args.unsafe + assert args.parser is not None and args.parser() + + +def test_subparsers_help_architecture(parser: argparse.ArgumentParser) -> None: + """ + help command must correctly parse architecture list + """ + args = parser.parse_args(["-a", "x86_64", "help"]) + assert args.architecture == [""] + + def test_subparsers_help_commands_unsafe(parser: argparse.ArgumentParser) -> None: """ help-commands-unsafe command must imply architecture list, lock, no-report, quiet, unsafe and parser @@ -80,7 +101,7 @@ def test_subparsers_help_commands_unsafe(parser: argparse.ArgumentParser) -> Non def test_subparsers_help_commands_unsafe_architecture(parser: argparse.ArgumentParser) -> None: """ - help-ommands-unsafe command must correctly parse architecture list + help-commands-unsafe command must correctly parse architecture list """ args = parser.parse_args(["-a", "x86_64", "help-commands-unsafe"]) assert args.architecture == [""]