From d650bf05013f10a3e8de73f061e9a725609e5e65 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Fri, 26 Oct 2018 15:52:11 +0200 Subject: 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 Reviewed-by: Christian Tismer --- build_scripts/options.py | 55 +++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 22 deletions(-) (limited to 'build_scripts/options.py') 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() -- cgit v1.2.3