# runs the setup script with no arguments at all. More useful help
# is generated with various --help options: global help, list commands,
# and per-command help.
-usage = """\
-usage: %s [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
- or: %s --help [cmd1 cmd2 ...]
- or: %s --help-commands
- or: %s cmd --help
-""" % ((os.path.basename(sys.argv[0]),) * 4)
+USAGE = """\
+usage: %(script)s [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
+ or: %(script)s --help [cmd1 cmd2 ...]
+ or: %(script)s --help-commands
+ or: %(script)s cmd --help
+"""
# If DISTUTILS_DEBUG is anything other than the empty string, we run in
# debug mode.
DEBUG = os.environ.get('DISTUTILS_DEBUG')
+def gen_usage (script_name):
+ script = os.path.basename(script_name)
+ return USAGE % vars()
+
def setup (**attrs):
"""The gateway to the Distutils: do everything your setup script needs
to do, in a highly flexible and user-driven way. Briefly: create a
Distribution instance; find and parse config files; parse the command
- line; run each of those commands using the options supplied to
- 'setup()' (as keyword arguments), in config files, and on the command
- line.
+ line; run each Distutils command found there, customized by the options
+ supplied to 'setup()' (as keyword arguments), in config files, and on
+ the command line.
The Distribution instance might be an instance of a class supplied via
the 'distclass' keyword argument to 'setup'; if no such class is
else:
klass = Distribution
+ if not attrs.has_key('script_name'):
+ attrs['script_name'] = sys.argv[0]
+ if not attrs.has_key('script_args'):
+ attrs['script_args'] = sys.argv[1:]
+
# Create the Distribution instance, using the remaining arguments
# (ie. everything except distclass) to initialize it
try:
# Parse the command line; any command-line errors are the end user's
# fault, so turn them into SystemExit to suppress tracebacks.
try:
- ok = dist.parse_command_line (sys.argv[1:])
+ ok = dist.parse_command_line()
except DistutilsArgError, msg:
- sys.stderr.write (usage + "\n")
- raise SystemExit, "error: %s" % msg
+ script = os.path.basename(dist.script_name)
+ raise SystemExit, \
+ gen_usage(dist.script_name) + "\nerror: %s" % msg
if DEBUG:
print "options (after parsing command line):"
# for the setup script to override command classes
self.cmdclass = {}
+ # 'script_name' and 'script_args' are usually set to sys.argv[0]
+ # and sys.argv[1:], but they can be overridden when the caller is
+ # not necessarily a setup script run from the command-line.
+ self.script_name = None
+ self.script_args = None
+
# 'command_options' is where we store command options between
# parsing them (from config files, the command-line, etc.) and when
# they are actually needed -- ie. when the command in question is
# -- Command-line parsing methods ----------------------------------
- def parse_command_line (self, args):
- """Parse the setup script's command line. 'args' must be a list
- of command-line arguments, most likely 'sys.argv[1:]' (see the
- 'setup()' function). This list is first processed for "global
- options" -- options that set attributes of the Distribution
- instance. Then, it is alternately scanned for Distutils
- commands and options for that command. Each new command
- terminates the options for the previous command. The allowed
- options for a command are determined by the 'user_options'
- attribute of the command class -- thus, we have to be able to
- load command classes in order to parse the command line. Any
- error in that 'options' attribute raises DistutilsGetoptError;
- any error on the command-line raises DistutilsArgError. If no
- Distutils commands were found on the command line, raises
- DistutilsArgError. Return true if command-line were
- successfully parsed and we should carry on with executing
- commands; false if no errors but we shouldn't execute commands
- (currently, this only happens if user asks for help).
+ def parse_command_line (self):
+ """Parse the setup script's command line, taken from the
+ 'script_args' instance attribute (which defaults to 'sys.argv[1:]'
+ -- see 'setup()' in core.py). This list is first processed for
+ "global options" -- options that set attributes of the Distribution
+ instance. Then, it is alternately scanned for Distutils commands
+ and options for that command. Each new command terminates the
+ options for the previous command. The allowed options for a
+ command are determined by the 'user_options' attribute of the
+ command class -- thus, we have to be able to load command classes
+ in order to parse the command line. Any error in that 'options'
+ attribute raises DistutilsGetoptError; any error on the
+ command-line raises DistutilsArgError. If no Distutils commands
+ were found on the command line, raises DistutilsArgError. Return
+ true if command-line were successfully parsed and we should carry
+ on with executing commands; false if no errors but we shouldn't
+ execute commands (currently, this only happens if user asks for
+ help).
"""
# We have to parse the command line a bit at a time -- global
# options, then the first command, then its options, and so on --
parser = FancyGetopt (self.global_options + self.display_options)
parser.set_negative_aliases (self.negative_opt)
parser.set_aliases ({'license': 'licence'})
- args = parser.getopt (object=self)
+ args = parser.getopt (args=self.script_args, object=self)
option_order = parser.get_option_order()
# for display options we return immediately
in 'commands'.
"""
# late import because of mutual dependence between these modules
- from distutils.core import usage
+ from distutils.core import gen_usage
from distutils.cmd import Command
if global_options:
parser.print_help ("Options for '%s' command:" % klass.__name__)
print
- print usage
+ print gen_usage(self.script_name)
return
# _show_help ()
line, display the requested info and return true; else return
false.
"""
- from distutils.core import usage
+ from distutils.core import gen_usage
# User just wants a list of commands -- we'll print it out and stop
# processing now (ie. if they ran "setup --help-commands foo bar",
if self.help_commands:
self.print_commands ()
print
- print usage
+ print gen_usage(self.script_name)
return 1
# If user supplied any of the "display metadata" options, then