From 80503c11c89d7d7428e86d1ef3e7e91d110fa52e Mon Sep 17 00:00:00 2001 From: actionless Date: Fri, 13 Sep 2024 00:09:24 +0200 Subject: [PATCH] feat(args: Arg): add optional help_only field for arguments which need to be documented but not automatically parsed --- pikaur/args.py | 67 ++++++++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/pikaur/args.py b/pikaur/args.py index 335b5b0b9..59f3ff9a7 100644 --- a/pikaur/args.py +++ b/pikaur/args.py @@ -22,6 +22,7 @@ class Arg(NamedTuple): long: str | None default: PossibleArgValuesTypes doc: str | None + help_only: bool = False ArgSchema = list[Arg] @@ -228,10 +229,11 @@ def get_pikaur_bool_opts(action: str | None = None) -> ArgSchema: None, translate("visualize package dependency tree"), ), - # Arg( - # "q", "quiet", None, - # translate("less verbose output"), - # ), + Arg( + "q", "quiet", None, + translate("less verbose output"), + help_only=True, + ), ] result += [ Arg( @@ -469,15 +471,15 @@ def get_all_pikaur_options(action: str | None = None) -> ArgSchema: def get_pikaur_long_opts() -> list[str]: return [ long_opt.replace("-", "_") - for _short_opt, long_opt, _default, _help in get_all_pikaur_options() - if long_opt is not None + for _short_opt, long_opt, _default, _help, help_only in get_all_pikaur_options() + if (long_opt is not None) and (not help_only) ] def get_pacman_long_opts() -> list[str]: # pragma: no cover return [ long_opt.replace("-", "_") - for _short_opt, long_opt, _default, _help + for _short_opt, long_opt, _default, _help, help_only in ( PACMAN_ACTIONS + get_pacman_bool_opts() + @@ -485,7 +487,7 @@ def get_pacman_long_opts() -> list[str]: # pragma: no cover PACMAN_APPEND_OPTS + get_pacman_count_opts() ) - if long_opt is not None + if (long_opt is not None) and (not help_only) ] @@ -724,12 +726,13 @@ def get_parser_for_action( parser = PikaurArgumentParser(prog=app, add_help=False) parser.add_argument("positional", nargs="*") - for letter, opt, default, _help in ( + for letter, opt, default, _help, help_only in ( PACMAN_ACTIONS + PIKAUR_ACTIONS ): - parser.add_letter_andor_opt( - action="store_true", letter=letter, opt=opt, default=default, - ) + if not help_only: + parser.add_letter_andor_opt( + action="store_true", letter=letter, opt=opt, default=default, + ) parsed_action = parser.parse_pikaur_args(args) pikaur_action: str | None = None for action_name in ALL_ACTIONS: @@ -747,15 +750,17 @@ def get_parser_for_action( (None, get_pikaur_str_opts(action=pikaur_action), True, None), (None, get_pikaur_int_opts(action=pikaur_action), True, int), ): - for letter, opt, default, help_msg in opt_list: - if arg_type: - parser.add_letter_andor_opt( - action=action_type, letter=letter, opt=opt, default=default, arg_type=arg_type, - ) - else: - parser.add_letter_andor_opt( - action=action_type, letter=letter, opt=opt, default=default, - ) + for letter, opt, default, help_msg, help_only in opt_list: + if not help_only: + if arg_type: + parser.add_letter_andor_opt( + action=action_type, letter=letter, opt=opt, default=default, + arg_type=arg_type, + ) + else: + parser.add_letter_andor_opt( + action=action_type, letter=letter, opt=opt, default=default, + ) if is_pikaur: help_msgs.append( HelpMessage(letter, opt, help_msg), @@ -823,13 +828,15 @@ def get_help() -> list[HelpMessage]: def reconstruct_args(parsed_args: PikaurArgs, ignore_args: list[str] | None = None) -> list[str]: if not ignore_args: ignore_args = [] - for _letter, opt, _default, _help in get_all_pikaur_options(action=LIST_ALL_ACTIONS): - # if letter: - # ignore_args.append(letter) - if opt: + for _letter, opt, _default, _help, help_only in get_all_pikaur_options( + action=LIST_ALL_ACTIONS, + ): + if opt and not help_only: ignore_args.append(opt.replace("-", "_")) count_args = [] - for letter, opt, _default, _help in get_pacman_count_opts(action=LIST_ALL_ACTIONS): + for letter, opt, _default, _help, help_only in get_pacman_count_opts(action=LIST_ALL_ACTIONS): + if help_only: + continue if letter: count_args.append(letter) if opt: @@ -842,20 +849,20 @@ def reconstruct_args(parsed_args: PikaurArgs, ignore_args: list[str] | None = No "raw", "unknown_args", "positional", "read_stdin", # computed members ] + [ long_arg - for _short_arg, long_arg, default, _help in get_pacman_str_opts( + for _short_arg, long_arg, default, _help, help_only in get_pacman_str_opts( action=LIST_ALL_ACTIONS, ) + PACMAN_APPEND_OPTS - if long_arg + if long_arg and not help_only ] } result = list(set( list(reconstructed_args.keys()) + parsed_args.unknown_args, )) for args_key, value in vars(parsed_args).items(): - for letter, long, _default, _help in ( + for letter, long, _default, _help, help_only in ( get_pacman_count_opts(action=LIST_ALL_ACTIONS) ): - if not long: + if (not long) or help_only: continue opt = long.replace("-", "_") if (