Repository: SCons/scons

Issue:
`--site-dir` has precedence over `--no-site-dir`, order should be swapped
@bdbaddog requested this ticket

Currently, if you run `scons --site-dir=xyz --no-site-dir`, SCons will ignore the `--no-site-dir` flag due to the order of the code below:
```
if options.site_dir:
    _load_site_scons_dir(d.get_internal_path(), options.site_dir)
elif not options.no_site_dir:
    _load_all_site_scons_dirs(d.get_internal_path())
```
However, I think it makes sense to reverse these and let `--no-site-dir` take precedence. That way, you can specify an arbitrary `--site-dir` using `SCONSFLAGS` or a shell alias, but still override that if needed. Currently you can override one site-dir with another by appending a second flag, but there is no way to disable the site-dir once it is on the command line. 

The one reverse argument is if you had `SCONSFLAGS='--no-site-dir'` and appended a site-dir locally, it would not get read in. I would expect that to be an extremely uncommon situation, since it would be easier to just rename the local site-dir to something other than `site_scons` and avoid needing the `--no-site-dir` flag at all. 


Discussion:
It should be based on order in the command line, the current oddity is it's treated as two different variables during options processing, more logically the same variable would be updated, that way you easily get "last one wins".

The attached diff illustrates what I'm thinking of. 

[3904.txt](https://github.com/SCons/scons/files/6175993/3904.txt)


That was similar to what I was thinking, but I wasn’t certain if multiple options could store to the same variable or not. If they can then yes that is the easiest way to go for sure. 
Yes, optparse allows multiple options to have the same variable to store to.  It's not cumulative unless you use `action="append"`, which is sometimes useful, though doesn't seem to be required here.

Resolving patch:
diff --git a/SCons/Script/Main.py b/SCons/Script/Main.py
--- a/SCons/Script/Main.py
+++ b/SCons/Script/Main.py
@@ -775,7 +775,7 @@ def _load_site_scons_dir(topdir, site_dir_name=None):
         raise
 
 
-def _load_all_site_scons_dirs(topdir, verbose=None):
+def _load_all_site_scons_dirs(topdir, verbose=False):
     """Load all of the predefined site_scons dir.
     Order is significant; we load them in order from most generic
     (machine-wide) to most specific (topdir).
@@ -968,10 +968,12 @@ def _main(parser):
     if options.no_progress or options.silent:
         progress_display.set_mode(0)
 
-    if options.site_dir:
-        _load_site_scons_dir(d.get_internal_path(), options.site_dir)
-    elif not options.no_site_dir:
+    # if site_dir unchanged from default None, neither --site-dir
+    # nor --no-site-dir was seen, use SCons default
+    if options.site_dir is None:
         _load_all_site_scons_dirs(d.get_internal_path())
+    elif options.site_dir:  # if a dir was set, use it
+        _load_site_scons_dir(d.get_internal_path(), options.site_dir)
 
     if options.include_dir:
         sys.path = options.include_dir + sys.path
diff --git a/SCons/Script/SConsOptions.py b/SCons/Script/SConsOptions.py
--- a/SCons/Script/SConsOptions.py
+++ b/SCons/Script/SConsOptions.py
@@ -801,8 +801,8 @@ def opt_implicit_deps(option, opt, value, parser):
                   help="Don't build; just print commands.")
 
     op.add_option('--no-site-dir',
-                  dest='no_site_dir', default=False,
-                  action="store_true",
+                  dest='site_dir',
+                  action="store_false",
                   help="Don't search or use the usual site_scons dir.")
 
     op.add_option('--profile',


Test patch:
diff --git a/test/site_scons/basic.py b/test/site_scons/basic.py
--- a/test/site_scons/basic.py
+++ b/test/site_scons/basic.py
@@ -1,6 +1,8 @@
 #!/usr/bin/env python
 #
-# __COPYRIGHT__
+# 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
@@ -20,9 +22,6 @@
 # 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.
-#
-
-__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
 
 import TestSCons
 
diff --git a/test/site_scons/no-site-dir.py b/test/site_scons/no-site-dir.py
--- a/test/site_scons/no-site-dir.py
+++ b/test/site_scons/no-site-dir.py
@@ -1,6 +1,8 @@
 #!/usr/bin/env python
 #
-# __COPYRIGHT__
+# 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
@@ -20,9 +22,6 @@
 # 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.
-#
-
-__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
 
 """
 Verify use of the --no-site-dir option:
diff --git a/test/site_scons/nonexistent.py b/test/site_scons/nonexistent.py
--- a/test/site_scons/nonexistent.py
+++ b/test/site_scons/nonexistent.py
@@ -1,6 +1,8 @@
 #!/usr/bin/env python
 #
-# __COPYRIGHT__
+# 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
@@ -20,9 +22,6 @@
 # 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.
-#
-
-__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
 
 """
 Verify that specifying --site-dir= with a nonexistent directory
diff --git a/test/site_scons/override.py b/test/site_scons/override.py
--- a/test/site_scons/override.py
+++ b/test/site_scons/override.py
@@ -1,6 +1,8 @@
 #!/usr/bin/env python
 #
-# __COPYRIGHT__
+# 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
@@ -20,9 +22,6 @@
 # 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.
-#
-
-__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
 
 
 """
diff --git a/test/site_scons/site-dir.py b/test/site_scons/site-dir.py
--- a/test/site_scons/site-dir.py
+++ b/test/site_scons/site-dir.py
@@ -1,6 +1,8 @@
 #!/usr/bin/env python
 #
-# __COPYRIGHT__
+# 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
@@ -20,52 +22,78 @@
 # 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.
-#
-
-__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
 
 """
-Verify that --site-dir=otherdir loads the site_init.py script
-from the other dir;
+Verify that --site-dir=otherdir loads the site_init.py script from otherdir;
 the usual site_scons/site_init.py should NOT be loaded.
+Check that a later --no-site-dir turns off site-dir processing
+even if a --site-dir option was seen earlier.
 """
 
+import os
+
 import TestSCons
 
 test = TestSCons.TestSCons()
 
 test.subdir('site_scons', ['site_scons', 'site_tools'])
 
-test.write(['site_scons', 'site_init.py'], """
+test.write(
+    ['site_scons', 'site_init.py'],
+    """
 from SCons.Script import *
 print("Hi there, I am in site_scons/site_init.py!")
-""")
+""",
+)
 
-test.write(['site_scons', 'site_tools', 'mytool.py'], """
+test.write(
+    ['site_scons', 'site_tools', 'mytool.py'],
+    """
 import SCons.Tool
 def generate(env):
     env['MYTOOL']='mytool'
 def exists(env):
     return 1
-""")
-
-
+""",
+)
 
 test.subdir('alt_site', ['alt_site', 'site_tools'])
 
-test.write(['alt_site', 'site_init.py'], """
+test.write(
+    ['alt_site', 'site_init.py'],
+    """
 from SCons.Script import *
 print("Hi there, I am in alt_site/site_init.py!")
-""")
+""",
+)
 
-test.write('SConstruct', """
+test.write(
+    'SConstruct',
+    """
 e=Environment()
-""")
+""",
+)
+
+test.run(
+    arguments='-Q --site-dir=alt_site .',
+    stdout="""Hi there, I am in alt_site/site_init.py!
+scons: `.' is up to date.\n""",
+)
+
+
+# --site-dir followed by --no-site-dir turns processing off:
+test.run(
+    arguments="-Q --site-dir=alt_site --no-site-dir .",
+    stdout="""scons: `.' is up to date.\n""",
+)
 
-test.run(arguments = '-Q --site-dir=alt_site .',
-         stdout = """Hi there, I am in alt_site/site_init.py!
-scons: `.' is up to date.\n""")
 
+# same test, but using SCONSFLAGS
+os.environ["SCONSFLAGS"] = "-Q --site-dir=alt_site"
+test.run(
+    arguments="--no-site-dir .",
+    stdout="""scons: `.' is up to date.\n""",
+)
 
 
 test.pass_test()
diff --git a/test/site_scons/site_init.py b/test/site_scons/site_init.py
--- a/test/site_scons/site_init.py
+++ b/test/site_scons/site_init.py
@@ -1,6 +1,8 @@
 #!/usr/bin/env python
 #
-# __COPYRIGHT__
+# 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
@@ -20,9 +22,6 @@
 # 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.
-#
-
-__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
 
 """
 Verify various aspects of the handling of site_init.py.
@@ -41,7 +40,7 @@
 
 def _test_metadata():
     """Test site_init's module metadata.
-    
+
     The following special variables should be predefined: __doc__,
     __file__ and __name__. No special variables should be transferred from
     SCons.Script.
diff --git a/test/site_scons/sys-path.py b/test/site_scons/sys-path.py
--- a/test/site_scons/sys-path.py
+++ b/test/site_scons/sys-path.py
@@ -1,6 +1,8 @@
 #!/usr/bin/env python
 #
-# __COPYRIGHT__
+# 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
@@ -20,9 +22,6 @@
 # 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.
-#
-
-__revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
 
 """
 Verify that the site_scons dir is added to sys.path as an
diff --git a/test/site_scons/sysdirs.py b/test/site_scons/sysdirs.py
--- a/test/site_scons/sysdirs.py
+++ b/test/site_scons/sysdirs.py
@@ -1,6 +1,8 @@
 #!/usr/bin/env python
 #
-# __COPYRIGHT__
+# 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