From c59a2703c59200194a9c045cfdf7a6c235d381e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Andreatta?= Date: Mon, 27 Apr 2026 21:22:36 +0200 Subject: [PATCH] Fix greedy capture of positional arguments when names are identical When a positional argument matches the name of a previous positional argument (like the database name) and follows an unknown flag, argparse incorrectly captures it. odev's rescue mechanism failed in this case because it used list.index(), which returned the first occurrence. This commit updates _rescue_positional_from_unknown_flag to check all occurrences of the value in the original arguments list. Assisted-by: gemini-3-flash --- odev/common/commands/base.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/odev/common/commands/base.py b/odev/common/commands/base.py index f15407cf..43ccd961 100644 --- a/odev/common/commands/base.py +++ b/odev/common/commands/base.py @@ -323,14 +323,13 @@ def _rescue_positional_from_unknown_flag( if not raw_val: continue - try: - val_idx = argv_list.index(raw_val) + indices = [i for i, x in enumerate(argv_list) if x == raw_val] + for val_idx in indices: if val_idx > 0 and argv_list[val_idx - 1] in unknown: flag_idx = unknown.index(argv_list[val_idx - 1]) unknown.insert(flag_idx + 1, raw_val) setattr(arguments, arg_name, None) - except ValueError: - pass + break @classmethod def parse_arguments(cls, argv: Sequence[str]) -> Namespace: