From 884ecdcf0bcd9fd03ee46d94ae1bc5e4e28efaab Mon Sep 17 00:00:00 2001 From: stevenknight Date: Tue, 11 Sep 2001 14:06:27 +0000 Subject: [PATCH] Initialize Options in a subroutine. git-svn-id: http://scons.tigris.org/svn/scons/trunk@42 fdb21ef1-2011-0410-befe-b5e4ea1792b1 --- src/scons.py | 308 +++++++++++++++++++++++++++------------------------ 1 file changed, 163 insertions(+), 145 deletions(-) diff --git a/src/scons.py b/src/scons.py index 2649637f..500910d7 100644 --- a/src/scons.py +++ b/src/scons.py @@ -106,23 +106,33 @@ def Help(text): sys.exit(0) -option_list = [] -# Generic routine for to-be-written options, used by multiple options below. - -def opt_not_yet(opt, arg): - sys.stderr.write("Warning: the %s option is not yet implemented\n" % opt) +# +# After options are initialized, the following variables are +# filled in: +# +option_list = [] # list of Option objects +short_opts = "" # string of short (single-character) options +long_opts = [] # array of long (--) options +opt_func = {} # mapping of option strings to functions + +def options_init(): + """Initialize command-line options processing. + + This is in a subroutine mainly so we can easily single-step over + it in the debugger. + """ -class Option: - """Class for command-line option information. + class Option: + """Class for command-line option information. - This exists to provide a central location for everything - describing a command-line option, so that we can change - options without having to update the code to handle the - option in one place, the -h help message in another place, - etc. There are no methods here, only attributes. + This exists to provide a central location for everything + describing a command-line option, so that we can change + options without having to update the code to handle the + option in one place, the -h help message in another place, + etc. There are no methods here, only attributes. - You can initialize an Option with the following: + You can initialize an Option with the following: func The function that will be called when this option is processed on the command line. @@ -169,270 +179,278 @@ class Option: recognize the option, but it won't show up in the -h output. - The following attribute is derived from the supplied attributes: + The following attribute is derived from the supplied attributes: optstring A string, with hyphens, describing the flags for this option, as constructed from the specified short, long and arg attributes. - All Option objects are stored in the global option_list list, - in the order in which they're created. This is the list - that's used to generate -h output, so the order in which the - objects are created is the order in which they're printed. + All Option objects are stored in the global option_list list, + in the order in which they're created. This is the list + that's used to generate -h output, so the order in which the + objects are created is the order in which they're printed. - The upshot is that specifying a command-line option and having - everything work correctly is a matter of defining a function to - process its command-line argument (set the right flag, update - the right value), and then creating an appropriate Option object - at the correct point in the code below. - """ + The upshot is that specifying a command-line option and having + everything work correctly is a matter of defining a function to + process its command-line argument (set the right flag, update + the right value), and then creating an appropriate Option object + at the correct point in the code below. + """ - def __init__(self, func = None, helpline = None, + def __init__(self, func = None, helpline = None, short = None, long = None, arg = None, help = None, future = None): - self.func = func - self.short = short - self.long = long - self.arg = arg - self.help = help - opts = [] - if self.short: - for c in self.short: + self.func = func + self.short = short + self.long = long + self.arg = arg + self.help = help + opts = [] + if self.short: + for c in self.short: + if arg: + c = c + " " + arg + opts = opts + ['-' + c] + if self.long: + l = self.long if arg: - c = c + " " + arg - opts = opts + ['-' + c] - if self.long: - l = self.long - if arg: - l = map(lambda x,a=arg: x + "=" + a, self.long) - opts = opts + map(lambda x: '--' + x, l) - self.optstring = string.join(opts, ', ') - if helpline: - self.helpline = helpline - elif help and not future: - if len(self.optstring) <= 26: - sep = " " * (28 - len(self.optstring)) + l = map(lambda x,a=arg: x + "=" + a, self.long) + opts = opts + map(lambda x: '--' + x, l) + self.optstring = string.join(opts, ', ') + if helpline: + self.helpline = helpline + elif help and not future: + if len(self.optstring) <= 26: + sep = " " * (28 - len(self.optstring)) + else: + sep = self.helpstring = "\n" + " " * 30 + self.helpline = " " + self.optstring + sep + self.help else: - sep = self.helpstring = "\n" + " " * 30 - self.helpline = " " + self.optstring + sep + self.help - else: - self.helpline = None - global option_list - option_list.append(self) + self.helpline = None + global option_list + option_list.append(self) + + # Generic routine for to-be-written options, used by multiple + # options below. -# In the following instantiations, the help string should be no -# longer than 49 characters. Use the following as a guide: -# help = "1234567890123456789012345678901234567890123456789" + def opt_not_yet(opt, arg): + sys.stderr.write("Warning: the %s option is not yet implemented\n" + % opt) -def opt_ignore(opt, arg): - sys.stderr.write("Warning: ignoring %s option\n" % opt) + # In the following instantiations, the help string should be no + # longer than 49 characters. Use the following as a guide: + # help = "1234567890123456789012345678901234567890123456789" -Option(func = opt_ignore, + def opt_ignore(opt, arg): + sys.stderr.write("Warning: ignoring %s option\n" % opt) + + Option(func = opt_ignore, short = 'bmSt', long = ['no-keep-going', 'stop', 'touch'], help = "Ignored for compatibility.") -Option(func = opt_not_yet, + Option(func = opt_not_yet, short = 'c', long = ['clean', 'remove'], help = "Remove specified targets and dependencies.") -Option(func = opt_not_yet, future = 1, + Option(func = opt_not_yet, future = 1, long = ['cache-disable', 'no-cache'], help = "Do not retrieve built targets from Cache.") -Option(func = opt_not_yet, future = 1, + Option(func = opt_not_yet, future = 1, long = ['cache-force', 'cache-populate'], help = "Copy already-built targets into the Cache.") -Option(func = opt_not_yet, future = 1, + Option(func = opt_not_yet, future = 1, long = ['cache-show'], help = "Print what would have built Cached targets.") -def opt_C(opt, arg): - try: - os.chdir(arg) - except: - sys.stderr.write("Could not change directory to 'arg'\n") + def opt_C(opt, arg): + try: + os.chdir(arg) + except: + sys.stderr.write("Could not change directory to 'arg'\n") -Option(func = opt_C, + Option(func = opt_C, short = 'C', long = ['directory'], arg = 'DIRECTORY', help = "Change to DIRECTORY before doing anything.") -Option(func = opt_not_yet, + Option(func = opt_not_yet, short = 'd', help = "Print file dependency information.") -Option(func = opt_not_yet, future = 1, + Option(func = opt_not_yet, future = 1, long = ['debug'], arg = 'FLAGS', help = "Print various types of debugging information.") -Option(func = opt_not_yet, future = 1, + Option(func = opt_not_yet, future = 1, short = 'e', long = ['environment-overrides'], help = "Environment variables override makefiles.") -def opt_f(opt, arg): - global Scripts - Scripts.append(arg) + def opt_f(opt, arg): + global Scripts + Scripts.append(arg) -Option(func = opt_f, + Option(func = opt_f, short = 'f', long = ['file', 'makefile', 'sconstruct'], arg = 'FILE', help = "Read FILE as the top-level SConstruct file.") -def opt_help(opt, arg): - global local_help - local_help = 1 + def opt_help(opt, arg): + global local_help + local_help = 1 -Option(func = opt_help, + Option(func = opt_help, short = 'h', long = ['help'], help = "Print defined help message, or this one.") -def opt_help_options(opt, arg): - PrintUsage() - sys.exit(0) + def opt_help_options(opt, arg): + PrintUsage() + sys.exit(0) -Option(func = opt_help_options, + Option(func = opt_help_options, short = 'H', long = ['help-options'], help = "Print this message and exit.") -Option(func = opt_not_yet, + Option(func = opt_not_yet, short = 'i', long = ['ignore-errors'], help = "Ignore errors from build actions.") -Option(func = opt_not_yet, + Option(func = opt_not_yet, short = 'I', long = ['include-dir'], arg = 'DIRECTORY', help = "Search DIRECTORY for imported Python modules.") -def opt_j(opt, arg): - global num_jobs - try: - num_jobs = int(arg) - except: - PrintUsage() - sys.exit(1) + def opt_j(opt, arg): + global num_jobs + try: + num_jobs = int(arg) + except: + PrintUsage() + sys.exit(1) - if num_jobs <= 0: - PrintUsage() - sys.exit(1) + if num_jobs <= 0: + PrintUsage() + sys.exit(1) -Option(func = opt_j, + Option(func = opt_j, short = 'j', long = ['jobs'], arg = 'N', help = "Allow N jobs at once.") -Option(func = opt_not_yet, + Option(func = opt_not_yet, short = 'k', long = ['keep-going'], help = "Keep going when a target can't be made.") -Option(func = opt_not_yet, future = 1, + Option(func = opt_not_yet, future = 1, short = 'l', long = ['load-average', 'max-load'], arg = 'N', help = "Don't start multiple jobs unless load is below N.") -Option(func = opt_not_yet, future = 1, + Option(func = opt_not_yet, future = 1, long = ['list-derived'], help = "Don't build; list files that would be built.") -Option(func = opt_not_yet, future = 1, + Option(func = opt_not_yet, future = 1, long = ['list-actions'], help = "Don't build; list files and build actions.") -Option(func = opt_not_yet, future = 1, + Option(func = opt_not_yet, future = 1, long = ['list-where'], help = "Don't build; list files and where defined.") -def opt_n(opt, arg): - scons.Builder.execute_actions = None + def opt_n(opt, arg): + scons.Builder.execute_actions = None -Option(func = opt_n, + Option(func = opt_n, short = 'n', long = ['no-exec', 'just-print', 'dry-run', 'recon'], help = "Don't build; just print commands.") -Option(func = opt_not_yet, future = 1, + Option(func = opt_not_yet, future = 1, short = 'o', long = ['old-file', 'assume-old'], arg = 'FILE', help = "Consider FILE to be old; don't rebuild it.") -Option(func = opt_not_yet, future = 1, + Option(func = opt_not_yet, future = 1, long = ['override'], arg = 'FILE', help = "Override variables as specified in FILE.") -Option(func = opt_not_yet, future = 1, + Option(func = opt_not_yet, future = 1, short = 'p', help = "Print internal environments/objects.") -Option(func = opt_not_yet, future = 1, + Option(func = opt_not_yet, future = 1, short = 'q', long = ['question'], help = "Don't build; exit status says if up to date.") -Option(func = opt_not_yet, future = 1, + Option(func = opt_not_yet, future = 1, short = 'rR', long = ['no-builtin-rules', 'no-builtin-variables'], help = "Clear default environments and variables.") -Option(func = opt_not_yet, future = 1, + Option(func = opt_not_yet, future = 1, long = ['random'], help = "Build dependencies in random order.") -def opt_s(opt, arg): - scons.Builder.print_actions = None + def opt_s(opt, arg): + scons.Builder.print_actions = None -Option(func = opt_s, + Option(func = opt_s, short = 's', long = ['silent', 'quiet'], help = "Don't print commands.") -Option(func = opt_not_yet, future = 1, + Option(func = opt_not_yet, future = 1, short = 'u', long = ['up', 'search-up'], help = "Search up directory tree for SConstruct.") -def option_v(opt, arg): - print "SCons version __VERSION__, by Steven Knight et al." - print "Copyright 2001 Steven Knight" - sys.exit(0) + def option_v(opt, arg): + print "SCons version __VERSION__, by Steven Knight et al." + print "Copyright 2001 Steven Knight" + sys.exit(0) -Option(func = option_v, + Option(func = option_v, short = 'v', long = ['version'], help = "Print the SCons version number and exit.") -Option(func = opt_not_yet, + Option(func = opt_not_yet, short = 'w', long = ['print-directory'], help = "Print the current directory.") -Option(func = opt_not_yet, + Option(func = opt_not_yet, long = ['no-print-directory'], help = "Turn off -w, even if it was turned on implicitly.") -Option(func = opt_not_yet, future = 1, + Option(func = opt_not_yet, future = 1, long = ['write-filenames'], arg = 'FILE', help = "Write all filenames examined into FILE.") -Option(func = opt_not_yet, future = 1, + Option(func = opt_not_yet, future = 1, short = 'W', long = ['what-if', 'new-file', 'assume-new'], arg = 'FILE', help = "Consider FILE to be changed.") -Option(func = opt_not_yet, future = 1, + Option(func = opt_not_yet, future = 1, long = ['warn-undefined-variables'], help = "Warn when an undefined variable is referenced.") -Option(func = opt_not_yet, future = 1, + Option(func = opt_not_yet, future = 1, short = 'Y', long = ['repository'], arg = 'REPOSITORY', help = "Search REPOSITORY for source and target files.") -short_opts = "" -long_opts = [] -opt_func = {} - -for o in option_list: - if o.short: - if o.func: - for c in o.short: - opt_func['-' + c] = o.func - short_opts = short_opts + o.short - if o.arg: - short_opts = short_opts + ":" - if o.long: - if o.func: - for l in o.long: - opt_func['--' + l] = o.func - if o.arg: - long_opts = long_opts + map(lambda a: a + "=", o.long) - else: - long_opts = long_opts + o.long + global short_opts + global long_opts + global opt_func + for o in option_list: + if o.short: + if o.func: + for c in o.short: + opt_func['-' + c] = o.func + short_opts = short_opts + o.short + if o.arg: + short_opts = short_opts + ":" + if o.long: + if o.func: + for l in o.long: + opt_func['--' + l] = o.func + if o.arg: + long_opts = long_opts + map(lambda a: a + "=", o.long) + else: + long_opts = long_opts + o.long + +options_init() -- 2.26.2