Skip to content

Commit

Permalink
feat(args: Arg): add optional help_only field for arguments which nee…
Browse files Browse the repository at this point in the history
…d to be documented but not automatically parsed
  • Loading branch information
actionless committed Sep 12, 2024
1 parent 22d8951 commit 80503c1
Showing 1 changed file with 37 additions and 30 deletions.
67 changes: 37 additions & 30 deletions pikaur/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Arg(NamedTuple):
long: str | None
default: PossibleArgValuesTypes
doc: str | None
help_only: bool = False


ArgSchema = list[Arg]
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -469,23 +471,23 @@ 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() +
get_pacman_str_opts() +
PACMAN_APPEND_OPTS +
get_pacman_count_opts()
)
if long_opt is not None
if (long_opt is not None) and (not help_only)
]


Expand Down Expand Up @@ -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:
Expand All @@ -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),
Expand Down Expand Up @@ -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:
Expand All @@ -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 (
Expand Down

0 comments on commit 80503c1

Please sign in to comment.