From c1acf2f557a0e99ef8f97d694f346e8c10ba6b97 Mon Sep 17 00:00:00 2001 From: Mats Wichmann Date: Sun, 23 Aug 2020 08:12:37 -0600 Subject: Reject abbreviations of AddOption options. If a cmdline option is left over, and looks like an abbreviation of an AddOption'd option, raise an error. Signed-off-by: Mats Wichmann --- CHANGES.txt | 2 ++ SCons/Script/Main.xml | 4 ++-- SCons/Script/SConsOptions.py | 11 +++++++---- test/AddOption/longopts.py | 10 ++++++++++ 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index fe45dc9..7719cf3 100755 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -30,6 +30,8 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER From Mats Wichmann: - Complete tests for Dictionary, env.keys() and env.values() for OverrideEnvironment. Enable env.setdefault() method, add tests. + - Raise an error if an option (not otherwise consumed) is used which + looks like an abbreviation of one one added by AddOption. (#3653) From Joachim Kuebart: - Suppress missing SConscript deprecation warning if `must_exist=False` diff --git a/SCons/Script/Main.xml b/SCons/Script/Main.xml index 0a8f9dc..f65ffca 100644 --- a/SCons/Script/Main.xml +++ b/SCons/Script/Main.xml @@ -36,13 +36,13 @@ are the same as those supported by the add_option method in the standard Python library module optparse, with a few additional capabilities noted below. See the documentation for -optparse +optparse for a thorough discussion of its option-processing capabities. In addition to the arguments and values supported by the -optparse +optparse add_option method, &f-AddOption; allows setting the diff --git a/SCons/Script/SConsOptions.py b/SCons/Script/SConsOptions.py index 9fee74e..6e95163 100644 --- a/SCons/Script/SConsOptions.py +++ b/SCons/Script/SConsOptions.py @@ -273,8 +273,7 @@ class SConsOptionParser(optparse.OptionParser): """ arg = rargs.pop(0) - # Value explicitly attached to arg? Pretend it's the next - # argument. + # Value explicitly attached to arg? Pretend it's the next argument. if "=" in arg: (opt, next_arg) = arg.split("=", 1) rargs.insert(0, next_arg) @@ -284,7 +283,11 @@ class SConsOptionParser(optparse.OptionParser): had_explicit_value = False try: - opt = self._match_long_opt(opt) + if opt != self._match_long_opt(opt): + raise optparse.BadOptionError( + "cannot use abbreviated option %s, use %s instead" + % (opt, self._match_long_opt(opt)) + ) except optparse.BadOptionError: if self.preserve_unknown_options: # SCons-specific: if requested, add unknown options to @@ -398,7 +401,7 @@ class SConsOptionParser(optparse.OptionParser): """ Adds a local option to the parser. - This is initiated by a SetOption() call to add a user-defined + This is initiated by an AddOption() call to add a user-defined command-line option. We add the option to a separate option group for the local options, creating the group if necessary. """ diff --git a/test/AddOption/longopts.py b/test/AddOption/longopts.py index 47ae4f1..0fcaa5b 100644 --- a/test/AddOption/longopts.py +++ b/test/AddOption/longopts.py @@ -51,6 +51,16 @@ test.run('-Q -q . --myargument=helloworld', test.run('-Q -q . --myarg=helloworld', stdout="myargument: gully\nmyarg: helloworld\n") +# Issue #3653: add a check for an abbreviation which never gets AddOption'd. +test.run('-Q -q . --myargumen=helloworld', status=2, + stdout="myargument: gully\nmyarg: balla\n", + stderr="""\ +usage: scons [OPTION] [TARGET] ... + +SCons Error: no such option: cannot use abbreviated option --myargumen, use --myargument instead +""") + + test.pass_test() # Local Variables: -- cgit v0.12 From 30e9339739c411c1eb8fd514e1d0e995ddddb9b1 Mon Sep 17 00:00:00 2001 From: Mats Wichmann Date: Mon, 24 Aug 2020 21:20:36 -0600 Subject: [PR #3784] adjust opt-abbreviation msg after PR review Signed-off-by: Mats Wichmann --- SCons/Script/SConsOptions.py | 2 +- test/AddOption/longopts.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/SCons/Script/SConsOptions.py b/SCons/Script/SConsOptions.py index 6e95163..aa31635 100644 --- a/SCons/Script/SConsOptions.py +++ b/SCons/Script/SConsOptions.py @@ -285,7 +285,7 @@ class SConsOptionParser(optparse.OptionParser): try: if opt != self._match_long_opt(opt): raise optparse.BadOptionError( - "cannot use abbreviated option %s, use %s instead" + "'%s'. Did you mean '%s'?" % (opt, self._match_long_opt(opt)) ) except optparse.BadOptionError: diff --git a/test/AddOption/longopts.py b/test/AddOption/longopts.py index 0fcaa5b..48c3502 100644 --- a/test/AddOption/longopts.py +++ b/test/AddOption/longopts.py @@ -57,7 +57,7 @@ test.run('-Q -q . --myargumen=helloworld', status=2, stderr="""\ usage: scons [OPTION] [TARGET] ... -SCons Error: no such option: cannot use abbreviated option --myargumen, use --myargument instead +SCons Error: no such option: '--myargumen'. Did you mean '--myargument'? """) -- cgit v0.12