From 0d7d394d98ec72dc34496bce0d7824806a79f35d Mon Sep 17 00:00:00 2001 From: William Deegan Date: Sat, 13 Aug 2022 16:20:49 -0700 Subject: Add ValidateOption() API which validates that all command line options are either SCons specified or specifie by AddOption calls. It will error out if there are any unknown options. Resolves Issue #4187 --- CHANGES.txt | 5 +++ RELEASE.txt | 4 +- SCons/Script/Main.py | 11 +++++ SCons/Script/Main.xml | 20 +++++++++ SCons/Script/SConscript.py | 1 + SCons/Script/__init__.py | 1 + SCons/__init__.py | 8 ++-- doc/generated/examples/caching_ex-random_1.xml | 6 +-- doc/generated/examples/troubleshoot_explain1_3.xml | 2 +- doc/generated/functions.gen | 18 ++++++++ doc/generated/functions.mod | 4 ++ test/ValidateOptions.py | 51 ++++++++++++++++++++++ test/fixture/SConstruct-check-valid-options | 9 ++++ 13 files changed, 131 insertions(+), 9 deletions(-) create mode 100644 test/ValidateOptions.py create mode 100644 test/fixture/SConstruct-check-valid-options diff --git a/CHANGES.txt b/CHANGES.txt index ee49590..31407fb 100755 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -9,6 +9,11 @@ NOTE: 4.3.0 now requires Python 3.6.0 and above. Python 3.5.x is no longer suppo RELEASE VERSION/DATE TO BE FILLED IN LATER + From William Deegan: + - Added ValidateOptions() which will check that all command line options are in either + those specified by SCons itself, or by AddOption() in SConstruct/SConscript. It should + not be called until all AddOption() calls are completed. Resolves Issue #4187 + From Dan Mezhiborsky: - Add newline to end of compilation db (compile_commands.json). diff --git a/RELEASE.txt b/RELEASE.txt index 711b6f2..3eff8ad 100755 --- a/RELEASE.txt +++ b/RELEASE.txt @@ -16,7 +16,9 @@ Here is a summary of the changes since 4.4.0: NEW FUNCTIONALITY ----------------- -- List new features (presumably why a checkpoint is being released) +- Added ValidateOptions() which will check that all command line options are in either + those specified by SCons itself, or by AddOption() in SConstruct/SConscript. It should + not be called until all AddOption() calls are completed. Resolves Issue #4187 DEPRECATED FUNCTIONALITY ------------------------ diff --git a/SCons/Script/Main.py b/SCons/Script/Main.py index ab2dc0e..850ca71 100644 --- a/SCons/Script/Main.py +++ b/SCons/Script/Main.py @@ -492,6 +492,16 @@ def GetOption(name): def SetOption(name, value): return OptionsParser.values.set_option(name, value) +def ValidateOptions(): + """ + If you call this after you set all your command line options with AddOption(), + it will verify that all command line options are valid. + So if you added an option --xyz and you call SCons with --xyy you can cause + SCons to issue an error message and exit by calling this function. + """ + OptionsParser.preserve_unknown_options = False + OptionsParser.parse_args(OptionsParser.largs, OptionsParser.values) + def PrintHelp(file=None): OptionsParser.print_help(file=file) @@ -1364,6 +1374,7 @@ def _exec_main(parser, values): else: _main(parser) + def main(): global OptionsParser global exit_status diff --git a/SCons/Script/Main.xml b/SCons/Script/Main.xml index f4f4705..746c1dc 100644 --- a/SCons/Script/Main.xml +++ b/SCons/Script/Main.xml @@ -740,6 +740,7 @@ Multiple targets can be passed in to a single call to + (name, value) @@ -943,6 +944,25 @@ Example: SetOption('max_drift', 0) + + + + + + Check that all the options provided by the user on the command line are either defined by SCons itself + or + defined by &f-link-AddOption;. + + + If there are any command line options not defined. + Calling this function will cause SCons to issue an error message and then exit with and error exit + status. + + + This function should only be called after the last &f-link-AddOption; call in your &SConscript; logic. + + + diff --git a/SCons/Script/SConscript.py b/SCons/Script/SConscript.py index 6188151..548ef44 100644 --- a/SCons/Script/SConscript.py +++ b/SCons/Script/SConscript.py @@ -528,6 +528,7 @@ class SConsEnvironment(SCons.Environment.Base): name = self.subst(name) return SCons.Script.Main.GetOption(name) + def Help(self, text, append=False): text = self.subst(text, raw=1) SCons.Script.HelpFunction(text, append=append) diff --git a/SCons/Script/__init__.py b/SCons/Script/__init__.py index 40be95e..aed31a5 100644 --- a/SCons/Script/__init__.py +++ b/SCons/Script/__init__.py @@ -107,6 +107,7 @@ AddOption = Main.AddOption PrintHelp = Main.PrintHelp GetOption = Main.GetOption SetOption = Main.SetOption +ValidateOptions = Main.ValidateOptions Progress = Main.Progress GetBuildFailures = Main.GetBuildFailures diff --git a/SCons/__init__.py b/SCons/__init__.py index 189bec3..dc8a9c0 100644 --- a/SCons/__init__.py +++ b/SCons/__init__.py @@ -1,9 +1,9 @@ -__version__="4.4.0" +__version__="4.4.1" __copyright__="Copyright (c) 2001 - 2022 The SCons Foundation" __developer__="bdbaddog" -__date__="Sat, 30 Jul 2022 14:11:34 -0700" +__date__="Sat, 13 Aug 2022 15:38:28 -0700" __buildsys__="M1Dog2021" -__revision__="fc8d0ec215ee6cba8bc158ad40c099be0b598297" -__build__="fc8d0ec215ee6cba8bc158ad40c099be0b598297" +__revision__="c7deb78684a518c59ed05c40bef266e9d27a9957" +__build__="c7deb78684a518c59ed05c40bef266e9d27a9957" # make sure compatibility is always in place import SCons.compat # noqa \ No newline at end of file diff --git a/doc/generated/examples/caching_ex-random_1.xml b/doc/generated/examples/caching_ex-random_1.xml index b8bd1bf..b54cc37 100644 --- a/doc/generated/examples/caching_ex-random_1.xml +++ b/doc/generated/examples/caching_ex-random_1.xml @@ -1,8 +1,8 @@ % scons -Q -cc -o f4.o -c f4.c -cc -o f2.o -c f2.c -cc -o f3.o -c f3.c cc -o f1.o -c f1.c +cc -o f3.o -c f3.c +cc -o f2.o -c f2.c cc -o f5.o -c f5.c +cc -o f4.o -c f4.c cc -o prog f1.o f2.o f3.o f4.o f5.o diff --git a/doc/generated/examples/troubleshoot_explain1_3.xml b/doc/generated/examples/troubleshoot_explain1_3.xml index e658d89..7a02fd5 100644 --- a/doc/generated/examples/troubleshoot_explain1_3.xml +++ b/doc/generated/examples/troubleshoot_explain1_3.xml @@ -2,5 +2,5 @@ cp file.in file.oout scons: warning: Cannot find target file.out after building -File "/Users/bdbaddog/devel/scons/git/as_scons/scripts/scons.py", line 97, in <module> +File "/Users/bdbaddog/devel/scons/git/scons-bugfixes-4/scripts/scons.py", line 97, in <module> diff --git a/doc/generated/functions.gen b/doc/generated/functions.gen index c135173..3c3413d 100644 --- a/doc/generated/functions.gen +++ b/doc/generated/functions.gen @@ -4384,6 +4384,24 @@ the tool object, previously it did not return + + ValidateOptions() + env.ValidateOptions() + + Check that all the options provided by the user on the command line are either defined by SCons itself + or + defined by &f-link-AddOption;. + + + If there are any command line options not defined. + Calling this function will cause SCons to issue an error message and then exit with and error exit + status. + + + This function should only be called after the last &f-link-AddOption; call in your &SConscript; logic. + + + Value(value, [built_value], [name]) env.Value(value, [built_value], [name]) diff --git a/doc/generated/functions.mod b/doc/generated/functions.mod index 9701bf3..34424a5 100644 --- a/doc/generated/functions.mod +++ b/doc/generated/functions.mod @@ -84,6 +84,7 @@ THIS IS AN AUTOMATICALLY-GENERATED FILE. DO NOT EDIT. subst"> Tag"> Tool"> +ValidateOptions"> Value"> VariantDir"> WhereIs"> @@ -164,6 +165,7 @@ THIS IS AN AUTOMATICALLY-GENERATED FILE. DO NOT EDIT. env.subst"> env.Tag"> env.Tool"> +env.ValidateOptions"> env.Value"> env.VariantDir"> env.WhereIs"> @@ -250,6 +252,7 @@ THIS IS AN AUTOMATICALLY-GENERATED FILE. DO NOT EDIT. subst"> Tag"> Tool"> +ValidateOptions"> Value"> VariantDir"> WhereIs"> @@ -330,6 +333,7 @@ THIS IS AN AUTOMATICALLY-GENERATED FILE. DO NOT EDIT. env.subst"> env.Tag"> env.Tool"> +env.ValidateOptions"> env.Value"> env.VariantDir"> env.WhereIs"> diff --git a/test/ValidateOptions.py b/test/ValidateOptions.py new file mode 100644 index 0000000..09609ef --- /dev/null +++ b/test/ValidateOptions.py @@ -0,0 +1,51 @@ +# MIT License +# +# Copyright The SCons Foundation +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY +# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE +# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +""" +Test ValidateOptions() command. +""" + +import TestSCons + +test = TestSCons.TestSCons() +test.file_fixture('fixture/SConstruct-check-valid-options', 'SConstruct') + +# Should see "This is in SConstruct" +test.run() +test.must_contain_single_instance_of(test.stdout(), ["This is in SConstruct"]) + +test.run(arguments="--testing=abc") +test.must_contain_single_instance_of(test.stdout(), ["This is in SConstruct"]) + +# Should not see "This is in SConstruct" +test.run(arguments="--garbage=xyz", status=2, stderr=".*SCons Error: no such option: --garbage.*", match=TestSCons.match_re_dotall) +test.fail_test(("This is in SConstruct" in test.stdout()), message='"This is in SConstruct" should not be output. This means ValidateOptions() did not error out before this was printed') + + + + +# Local Variables: +# tab-width:4 +# indent-tabs-mode:nil +# End: +# vim: set expandtab tabstop=4 shiftwidth=4: diff --git a/test/fixture/SConstruct-check-valid-options b/test/fixture/SConstruct-check-valid-options new file mode 100644 index 0000000..0e2930d --- /dev/null +++ b/test/fixture/SConstruct-check-valid-options @@ -0,0 +1,9 @@ + +AddOption( + '--testing', + help='Test arg', +) + +ValidateOptions() + +print("This is in SConstruct") \ No newline at end of file -- cgit v0.12