From c3a75e1a8cb6e8de95d6ed9c77923c80a2b8cfd4 Mon Sep 17 00:00:00 2001 From: anatoly techtonik Date: Fri, 7 Dec 2012 18:44:03 +0300 Subject: Add hints about valid choices to error messages from option parser --- src/CHANGES.txt | 1 + src/engine/SCons/Script/SConsOptions.py | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/CHANGES.txt b/src/CHANGES.txt index 5c8f821..6136f81 100644 --- a/src/CHANGES.txt +++ b/src/CHANGES.txt @@ -9,6 +9,7 @@ RELEASE 2.X.X - From Anatoly Techtonik: - Added ability to run scripts/scons.py directly from source checkout - Hide deprecated --debug={dtree,stree,tree} from --help output + - Error messages from option parser now include hints about valid choices From Juan Lang: - Fix WiX Tool to use .wixobj rather than .wxiobj for compiler output diff --git a/src/engine/SCons/Script/SConsOptions.py b/src/engine/SCons/Script/SConsOptions.py index 6f5293d..645ab11 100644 --- a/src/engine/SCons/Script/SConsOptions.py +++ b/src/engine/SCons/Script/SConsOptions.py @@ -268,8 +268,9 @@ class SConsOptionParser(optparse.OptionParser): preserve_unknown_options = False def error(self, msg): + # overriden OptionValueError exception handler self.print_usage(sys.stderr) - sys.stderr.write("SCons error: %s\n" % msg) + sys.stderr.write("SCons Error: %s\n" % msg) sys.exit(2) def _process_long_opt(self, rargs, values): @@ -568,11 +569,15 @@ def Parser(version): action="store_true", help="Print build actions for files from CacheDir.") + def opt_invalid(group, value, options): + errmsg = "`%s' is not a valid %s option type, try:\n" % (value, group) + return errmsg + " %s" % ", ".join(options) + config_options = ["auto", "force" ,"cache"] def opt_config(option, opt, value, parser, c_options=config_options): if not value in c_options: - raise OptionValueError("Warning: %s is not a valid config type" % value) + raise OptionValueError(opt_invalid('config', value, c_options)) setattr(parser.values, option.dest, value) opt_config_help = "Controls Configure subsystem: %s." \ % ", ".join(config_options) @@ -617,7 +622,7 @@ def Parser(version): t = (SCons.Warnings.DeprecatedDebugOptionsWarning, w) parser.values.delayed_warnings.append(t) else: - raise OptionValueError("Warning: %s is not a valid debug type" % value) + raise OptionValueError(opt_invalid('debug', value, debug_options)) opt_debug_help = "Print various types of debugging information: %s." \ % ", ".join(debug_options) op.add_option('--debug', @@ -631,7 +636,7 @@ def Parser(version): try: diskcheck_value = diskcheck_convert(value) except ValueError, e: - raise OptionValueError("Warning: `%s' is not a valid diskcheck type" % e) + raise OptionValueError("`%s' is not a valid diskcheck type" % e) setattr(parser.values, option.dest, diskcheck_value) op.add_option('--diskcheck', @@ -643,7 +648,8 @@ def Parser(version): def opt_duplicate(option, opt, value, parser): if not value in SCons.Node.FS.Valid_Duplicates: - raise OptionValueError("`%s' is not a valid duplication style." % value) + raise OptionValueError(opt_invalid('duplication', value, + SCons.Node.FS.Valid_Duplicates)) setattr(parser.values, option.dest, value) # Set the duplicate style right away so it can affect linking # of SConscript files. @@ -808,7 +814,7 @@ def Parser(version): elif o == 'status': tp.status = True else: - raise OptionValueError("Warning: %s is not a valid --tree option" % o) + raise OptionValueError(opt_invalid('--tree', o, tree_options)) parser.values.tree_printers.append(tp) opt_tree_help = "Print a dependency tree in various formats: %s." \ -- cgit v0.12