Skip to content

Commit e869623

Browse files
committed
Partial fix for local options
Collect nargs arguments and get them off the lefover-args list Signed-off-by: Mats Wichmann <[email protected]>
1 parent d4a6d0e commit e869623

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

SCons/Script/SConsOptions.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -359,43 +359,60 @@ def reparse_local_options(self):
359359
This would lead to further confusion, because we might want
360360
to add another option "--myarg" later on (see issue #2929).
361361
362+
The implementation proved to be problematic in the case of
363+
space-separation (--arg opt) style, where the arg gets
364+
separated from the opt. So now we try to figure out if there
365+
are nargs and pop them off largs.
362366
"""
363367
rargs = []
364-
largs_restore = []
368+
largs = []
365369
# Loop over all remaining arguments
366370
skip = False
367-
for l in self.largs:
371+
372+
for idx, larg in enumerate(self.largs):
368373
if skip:
369374
# Accept all remaining arguments as they are
370-
largs_restore.append(l)
375+
largs.append(larg)
371376
else:
372-
if len(l) > 2 and l[0:2] == "--":
377+
if len(larg) > 2 and larg[0:2] == "--":
373378
# Check long option
374-
lopt = (l,)
375-
if "=" in l:
379+
if "=" in larg:
376380
# Split into option and value
377-
lopt = l.split("=", 1)
381+
lopt = larg.split("=", 1)
382+
else:
383+
lopt = [larg]
378384

379385
if lopt[0] in self._long_opt:
380386
# Argument is already known
381-
rargs.append('='.join(lopt))
387+
if len(lopt) > 1:
388+
# "--opt=arg" style, join it back up
389+
rargs.append('='.join(lopt))
390+
else:
391+
rargs.append(larg)
392+
# maybe "--opt arg" style, pull matching args
393+
option = self._long_opt[larg]
394+
if option.takes_value():
395+
for _ in range(option.nargs):
396+
try:
397+
rargs.append(self.largs.pop(idx + 1))
398+
except IndexError:
399+
# missing args, will be handled later
400+
pass
382401
else:
383402
# Not known yet, so reject for now
384-
largs_restore.append('='.join(lopt))
403+
largs.append('='.join(lopt))
385404
else:
386-
if l == "--" or l == "-":
405+
if larg in ("--", "-"):
387406
# Stop normal processing and don't
388407
# process the rest of the command-line opts
389-
largs_restore.append(l)
390408
skip = True
391-
else:
392-
rargs.append(l)
409+
largs.append(larg)
393410

394411
# Parse the filtered list
395412
self.parse_args(rargs, self.values)
396413
# Restore the list of remaining arguments for the
397414
# next call of AddOption/add_local_option...
398-
self.largs = self.largs + largs_restore
415+
self.largs = largs
399416

400417
def add_local_option(self, *args, **kw):
401418
"""

test/AddOption/multi-arg.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,16 @@
8383
print(str(GetOption('extras')))
8484
""")
8585

86+
# no options
8687
test.run('-Q -q .', stdout="None\n()\n")
88+
# one single-arg option
8789
test.run('-Q -q . --prefix=/home/foo', stdout="/home/foo\n()\n")
90+
# one two-arg option
8891
test.run('-Q -q . --extras A B', status=1, stdout="None\n('A', 'B')\n")
89-
test.run('-Q -q . -- --prefix=/home/foo --extras A B',
90-
status=1, stdout="None\n()\n")
92+
# single-arg option followed by two-arg option
93+
test.run('-Q -q . --prefix=/home/foo --extras A B', status=1, stdout="/home/foo\n('A', 'B')\n")
94+
# two-arg option followed by single-arg option
95+
test.run('-Q -q . --extras A B --prefix=/home/foo', status=1, stdout="/home/foo\n('A', 'B')\n")
9196

9297
test.pass_test()
9398

0 commit comments

Comments
 (0)