add help command

This commit is contained in:
Evgenii Alekseev 2022-03-31 02:18:39 +03:00
parent fdcbcc4541
commit 8e6473d2a0
5 changed files with 132 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -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 <http://www.gnu.org/licenses/>.
#
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"])

View File

@ -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"])

View File

@ -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 == [""]