@@ -92,14 +92,15 @@ def _f(value):
9292 return _f
9393
9494
95- def _find_positions (args , flags ):
95+ def _find_positions (args : Sequence [str ],
96+ flags : Sequence [str ]) -> list [Optional [int ]]:
9697 """Returns the index in flags of each of args.
9798
9899 args is expected to be a subsequence of flags, such as the unknown args that
99100 argparse returns from parse_known_args. The position of an arg that cannot be
100101 found is None.
101102 """
102- positions = []
103+ positions : list [ Optional [ int ]] = []
103104 start = 0
104105 for arg in args :
105106 try :
@@ -112,6 +113,36 @@ def _find_positions(args, flags):
112113 return positions
113114
114115
116+ def _has_separate_value (
117+ unknown_args : Sequence [str ],
118+ i : int ,
119+ all_flags : Sequence [str ],
120+ positions : Sequence [Optional [int ]]) -> bool :
121+ """Returns whether the unknown flag unknown_args[i] is followed by its value.
122+
123+ The value has to be the next unknown arg and also the arg that directly
124+ follows the flag in all_flags, so that a flag is not given a stray value from
125+ further along the command line. A flag written as --flag=value already
126+ carries its value, so nothing after it belongs to it.
127+
128+ Args:
129+ unknown_args: the args that argparse did not recognize, in their original
130+ order.
131+ i: the index of the flag in unknown_args.
132+ all_flags: the complete list of flags that unknown_args came from.
133+ positions: the index in all_flags of each of unknown_args, as returned by
134+ _find_positions.
135+ """
136+ if '=' in unknown_args [i ]:
137+ return False
138+ if i + 1 >= len (unknown_args ) or unknown_args [i + 1 ].startswith ('-' ):
139+ return False
140+ position = positions [i ]
141+ if position is None or position + 1 >= len (all_flags ):
142+ return False
143+ return not all_flags [position + 1 ].startswith ('-' )
144+
145+
115146class _BeamArgumentParser (argparse .ArgumentParser ):
116147 """An ArgumentParser that supports ValueProvider options.
117148
@@ -573,12 +604,7 @@ def add_new_arg(arg, **kwargs):
573604 if not unknown_args [i ].startswith ('-' ):
574605 i += 1
575606 continue
576- has_value = (
577- '=' not in unknown_args [i ] and i + 1 < len (unknown_args ) and
578- not unknown_args [i + 1 ].startswith ('-' ) and
579- positions [i ] is not None and positions [i ] + 1 < len (all_flags ) and
580- not all_flags [positions [i ] + 1 ].startswith ('-' ))
581- if not has_value :
607+ if not _has_separate_value (unknown_args , i , all_flags , positions ):
582608 split = unknown_args [i ].split ('=' , 1 )
583609 if len (split ) == 1 :
584610 add_new_arg (unknown_args [i ], action = 'store_true' )
0 commit comments