Skip to content

Commit

Permalink
Add short option fix for SCons#3798, fix bug in test/SCONSFLAGS.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Dillan Mills committed Sep 19, 2020
1 parent f1b3299 commit 2d8a1ea
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
61 changes: 59 additions & 2 deletions SCons/Script/SConsOptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,63 @@ def error(self, msg):
sys.stderr.write("SCons Error: %s\n" % msg)
sys.exit(2)

def _process_short_opts(self, rargs, values):
"""
SCons-specific processing of short options.
This is copied directly from the normal
optparse._process_short_opts() method, except that, if configured
to do so, we catch the exception thrown when an unknown option
is encountered and just stick it back on the "leftover" arguments
for later (re-)processing.
"""
arg = rargs.pop(0)
stop = False
i = 1
for ch in arg[1:]:
opt = "-" + ch
option = self._short_opt.get(opt)
i += 1 # we have consumed a character

try:
if not option:
raise optparse.BadOptionError(opt)
except optparse.BadOptionError:
if self.preserve_unknown_options:
# SCons-specific: if requested, add unknown options to
# the "leftover arguments" list for later processing.
self.largs.append(arg)
return
raise

if option.takes_value():
# Any characters left in arg? Pretend they're the
# next arg, and stop consuming characters of arg.
if i < len(arg):
rargs.insert(0, arg[i:])
stop = True

nargs = option.nargs
if len(rargs) < nargs:
if nargs == 1:
self.error(_("%s option requires an argument") % opt)
else:
self.error(_("%s option requires %d arguments")
% (opt, nargs))
elif nargs == 1:
value = rargs.pop(0)
else:
value = tuple(rargs[0:nargs])
del rargs[0:nargs]

else: # option doesn't take a value
value = None

option.process(opt, value, values, self)

if stop:
break

def _process_long_opt(self, rargs, values):
"""
SCons-specific processing of long options.
Expand Down Expand Up @@ -413,7 +470,7 @@ def add_local_option(self, *args, **kw):
self.local_option_group = group

result = group.add_option(*args, **kw)

if result:
# The option was added successfully. We now have to add the
# default value to our object that holds the default values
Expand Down Expand Up @@ -508,7 +565,7 @@ def Parser(version):
"""

formatter = SConsIndentedHelpFormatter(max_help_position=30)

op = SConsOptionParser(option_class=SConsOption,
add_help_option=False,
formatter=formatter,
Expand Down
5 changes: 4 additions & 1 deletion test/AddOption/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

test.write('SConstruct', """\
env = Environment()
AddOption('--force',
AddOption('-F', '--force',
action="store_true",
help='force installation (overwrite any existing files)')
AddOption('--prefix',
Expand Down Expand Up @@ -65,6 +65,9 @@
status=1,
stdout="None\nNone\n")

test.run('-Q -q . -F',
stdout="True\nNone\n")

test.pass_test()

# Local Variables:
Expand Down
9 changes: 7 additions & 2 deletions test/SCONSFLAGS.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,21 @@
test.must_not_contain_any_line(test.stdout(), ['Help text.'])
test.must_contain_all_lines(test.stdout(), ['-H, --help-options'])

os.environ['SCONSFLAGS'] = '-Z'

expect = r"""usage: scons [OPTION] [TARGET] ...
SCons Error: no such option: -Z
"""

test.run(arguments = "-H", status = 2,
test.run(arguments = "-Z", status = 2,
stderr = expect, match=TestSCons.match_exact)

os.environ['SCONSFLAGS'] = '-Z'

test.run(status = 2,
stderr = expect,
match=TestSCons.match_exact)

test.pass_test()

# Local Variables:
Expand Down

0 comments on commit 2d8a1ea

Please sign in to comment.