Skip to content

Commit

Permalink
refactor(args): access Arg NamedTuple's values by key instead of by i…
Browse files Browse the repository at this point in the history
…ndex
  • Loading branch information
actionless committed Sep 12, 2024
1 parent fcb5b44 commit a70b3fe
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 48 deletions.
54 changes: 27 additions & 27 deletions pikaur/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,9 @@ 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, help_only in get_all_pikaur_options()
if (long_opt is not None) and (not help_only)
arg.long.replace("-", "_")
for arg in get_all_pikaur_options()
if (arg.long is not None) and (not arg.help_only)
]


Expand Down Expand Up @@ -726,12 +726,12 @@ def get_parser_for_action(

parser = PikaurArgumentParser(prog=app, add_help=False)
parser.add_argument("positional", nargs="*")
for letter, opt, default, _help, help_only in (
for arg in (
PACMAN_ACTIONS + PIKAUR_ACTIONS
):
if not help_only:
if not arg.help_only:
parser.add_letter_andor_opt(
action="store_true", letter=letter, opt=opt, default=default,
action="store_true", letter=arg.short, opt=arg.long, default=arg.default,
)
parsed_action = parser.parse_pikaur_args(args)
pikaur_action: str | None = None
Expand All @@ -750,20 +750,20 @@ 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, help_only in opt_list:
if not help_only:
for arg in opt_list:
if not arg.help_only:
if arg_type:
parser.add_letter_andor_opt(
action=action_type, letter=letter, opt=opt, default=default,
action=action_type, letter=arg.short, opt=arg.long, default=arg.default,
arg_type=arg_type,
)
else:
parser.add_letter_andor_opt(
action=action_type, letter=letter, opt=opt, default=default,
action=action_type, letter=arg.short, opt=arg.long, default=arg.default,
)
if is_pikaur:
help_msgs.append(
HelpMessage(letter, opt, help_msg),
HelpMessage(arg.short, arg.long, arg.doc),
)

if pikaur_action is None:
Expand Down Expand Up @@ -828,51 +828,51 @@ 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, help_only in get_all_pikaur_options(
for arg in get_all_pikaur_options(
action=LIST_ALL_ACTIONS,
):
if opt and not help_only:
ignore_args.append(opt.replace("-", "_"))
if arg.long and not arg.help_only:
ignore_args.append(arg.long.replace("-", "_"))
count_args = []
for letter, opt, _default, _help, help_only in get_pacman_count_opts(action=LIST_ALL_ACTIONS):
if help_only:
for arg in get_pacman_count_opts(action=LIST_ALL_ACTIONS):
if arg.help_only:
continue
if letter:
count_args.append(letter)
if opt:
count_args.append(opt.replace("-", "_"))
if arg.short:
count_args.append(arg.short)
if arg.long:
count_args.append(arg.long.replace("-", "_"))
reconstructed_args = {
f"--{key}" if len(key) > 1 else f"-{key}": value
for key, value in vars(parsed_args).items()
if value
if key not in ignore_args + count_args + [
"raw", "unknown_args", "positional", "read_stdin", # computed members
] + [
long_arg
for _short_arg, long_arg, default, _help, help_only in get_pacman_str_opts(
arg.long
for arg in get_pacman_str_opts(
action=LIST_ALL_ACTIONS,
) + PACMAN_APPEND_OPTS
if long_arg and not help_only
if arg.long and not arg.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, help_only in (
for arg in (
get_pacman_count_opts(action=LIST_ALL_ACTIONS)
):
if (not long) or help_only:
if (not arg.long) or arg.help_only:
continue
opt = long.replace("-", "_")
opt = arg.long.replace("-", "_")
if (
value
and (
opt == args_key
) and (
opt not in ignore_args
) and (
letter not in ignore_args
arg.short not in ignore_args
)
):
result += ["--" + opt] * value
Expand Down
14 changes: 7 additions & 7 deletions pikaur/help_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@
def _format_options_help(options: list[HelpMessage]) -> str:
return "\n".join([
"{:>{first_column_margin}} {:<{first_column_width}} {}".format(
(short_opt and ("-" + short_opt + ",")) or "",
(long_opt and ("--" + long_opt)) or "",
descr if (
(len(short_opt or "") + 1 + len(long_opt or "") + 2) < FIRST_COLUMN_WIDTH
) else f"\n{(FIRST_COLUMN_MARGIN + FIRST_COLUMN_WIDTH + 2) * ' '}{descr}",
(help_msg.short and ("-" + help_msg.short + ",")) or "",
(help_msg.long and ("--" + help_msg.long)) or "",
help_msg.doc if (
(len(help_msg.short or "") + 1 + len(help_msg.long or "") + 2) < FIRST_COLUMN_WIDTH
) else f"\n{(FIRST_COLUMN_MARGIN + FIRST_COLUMN_WIDTH + 2) * ' '}{help_msg.doc}",
first_column_margin=FIRST_COLUMN_MARGIN,
first_column_width=FIRST_COLUMN_WIDTH,
)
for short_opt, long_opt, descr in options
if descr
for help_msg in options
if help_msg.doc
])


Expand Down
28 changes: 14 additions & 14 deletions pikaur/pacman.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ def get_pacman_command( # pylint: disable=too-many-branches
else:
pacman_cmd += ["--color=never"]

for short, long, _default, _help, help_only in get_pacman_str_opts():
if help_only:
for data in get_pacman_str_opts():
if data.help_only:
continue
arg = long or short
arg = data.long or data.short
if not arg:
continue
if arg == "color": # we force it anyway
Expand All @@ -77,26 +77,26 @@ def get_pacman_command( # pylint: disable=too-many-branches
continue
value = getattr(args, arg)
if value:
if long:
pacman_cmd += ["--" + long, value]
elif short:
pacman_cmd += ["-" + short, value]
if data.long:
pacman_cmd += ["--" + data.long, value]
elif data.short:
pacman_cmd += ["-" + data.short, value]

for short, long, _default, _help, help_only in PACMAN_APPEND_OPTS:
if help_only:
for data in PACMAN_APPEND_OPTS:
if data.help_only:
continue
arg = long or short
arg = data.long or data.short
if not arg:
continue
if arg == "ignore": # we reprocess it anyway
continue
if arg in ignore_args:
continue
for value in getattr(args, arg) or []:
if long:
pacman_cmd += ["--" + long, value]
elif short:
pacman_cmd += ["-" + short, value]
if data.long:
pacman_cmd += ["--" + data.long, value]
elif data.short:
pacman_cmd += ["-" + data.short, value]

return pacman_cmd

Expand Down

0 comments on commit a70b3fe

Please sign in to comment.