aboutsummaryrefslogtreecommitdiffstats
path: root/build_scripts/options.py
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2018-10-26 15:52:11 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2018-10-29 18:21:27 +0000
commitd650bf05013f10a3e8de73f061e9a725609e5e65 (patch)
tree55f2deb5e047728e04aef07ad96430a83976446a /build_scripts/options.py
parentf4a6d74852b0ba6711195a9c3fc9356e677a3409 (diff)
setup.py: Warn about options occurring multiple times on the command line
Remove all occurrences of the option in question and output a warning should it appear multiple times. Task-number: PYSIDE-809 Change-Id: Ic78b753e28032081cd99a17da93c0deab9e85210 Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io> Reviewed-by: Christian Tismer <tismer@stackless.com>
Diffstat (limited to 'build_scripts/options.py')
-rw-r--r--build_scripts/options.py55
1 files changed, 33 insertions, 22 deletions
diff --git a/build_scripts/options.py b/build_scripts/options.py
index daf3bb00e..3e3a63ca9 100644
--- a/build_scripts/options.py
+++ b/build_scripts/options.py
@@ -40,8 +40,13 @@
from __future__ import print_function
import sys
import os
+import warnings
+def _warn_multiple_option(option):
+ w = 'Option "{}" occurs multiple times on the command line.'.format(option)
+ warnings.warn(w)
+
class Options(object):
def __init__(self):
@@ -51,13 +56,13 @@ class Options(object):
def has_option(self, name):
""" Returns True if argument '--name' was passed on the command
line. """
- try:
- sys.argv.remove("--{}".format(name))
- self.dict[name] = True
- return True
- except ValueError:
- pass
- return False
+ option = '--' + name
+ count = sys.argv.count(option)
+ for i in range(count):
+ sys.argv.remove(option)
+ if count > 1:
+ _warn_multiple_option(option)
+ return count > 0
def option_value(self, name, remove=True):
"""
@@ -73,30 +78,36 @@ class Options(object):
:return: Either the option value or None.
"""
- for index, option in enumerate(sys.argv):
- if option == '--' + name:
- if index + 1 >= len(sys.argv):
- raise RuntimeError("The option {} requires a value".format(option))
- value = sys.argv[index + 1]
+ option = '--' + name
+ single_option_prefix = option + '='
+ value = None
+ for index in reversed(range(len(sys.argv))):
+ arg = sys.argv[index]
+ if arg == option:
+ if value:
+ _warn_multiple_option(option)
+ else:
+ if index + 1 >= len(sys.argv):
+ raise RuntimeError("The option {} requires a value".format(option))
+ value = sys.argv[index + 1]
if remove:
sys.argv[index:index + 2] = []
- self.dict[name] = value
- return value
-
- if option.startswith('--' + name + '='):
- value = option[len(name) + 3:]
+ elif arg.startswith(single_option_prefix):
+ if value:
+ _warn_multiple_option(option)
+ else:
+ value = arg[len(single_option_prefix):]
if remove:
sys.argv[index:index + 1] = []
- self.dict[name] = value
- return value
+ if value is None:
+ value = os.getenv(name.upper().replace('-', '_'))
- env_val = os.getenv(name.upper().replace('-', '_'))
- self.dict[name] = env_val
- return env_val
+ self.dict[name] = value
+ return value
options = Options()