Ticket #11407: command_parser.diff

File command_parser.diff, 3.4 KB (added by Mark Barrett, 12 years ago)
  • django/core/management/__init__.py

     
    147147
    148148    return klass.execute(*args, **defaults)
    149149
    150 class LaxOptionParser(OptionParser):
     150class ManagementUtilityOptionParser(OptionParser):
    151151    """
    152152    An option parser that doesn't raise any errors on unknown options.
    153153
    154154    This is needed because the --settings and --pythonpath options affect
    155155    the commands (and thus the options) that are available to the user.
    156156    """
     157
     158    def __init__(self, main_help_text=None, **kwargs):
     159        """Initialise class with main help string, which is used to
     160        supplement the information printed by OptionParser.print_help()
     161        """
     162        OptionParser.__init__(self, **kwargs)
     163        self.main_help_text = main_help_text
     164
    157165    def error(self, msg):
    158166        pass
    159167
    160168    def print_help(self):
    161         """Output nothing.
    162 
    163         The lax options are included in the normal option parser, so under
    164         normal usage, we don't need to print the lax options.
     169        """Output help generated by OptionParser.print_help() along with
     170        supplementary message
    165171        """
    166         pass
     172        self.print_lax_help()
     173        sys.stdout.write(self.main_help_text + '\n')
    167174
    168175    def print_lax_help(self):
    169176        """Output the basic options available to every command.
     
    197204                    # either way, add it to the args list so we can keep
    198205                    # dealing with options
    199206                    del rargs[0]
    200                     raise Exception
    201             except:
     207                    raise Exception()
     208
     209            except Exception:
    202210                largs.append(arg)
    203211
    204212class ManagementUtility(object):
     
    327335        # Preprocess options to extract --settings and --pythonpath.
    328336        # These options could affect the commands that are available, so they
    329337        # must be processed early.
    330         parser = LaxOptionParser(usage="%prog subcommand [options] [args]",
     338        parser = ManagementUtilityOptionParser(usage="%prog subcommand [options] [args]",
    331339                                 version=get_version(),
    332                                  option_list=BaseCommand.option_list)
     340                                 option_list=BaseCommand.option_list,
     341                                 main_help_text=self.main_help_text())
     342
    333343        self.autocomplete()
    334         try:
    335             options, args = parser.parse_args(self.argv)
    336             handle_default_options(options)
    337         except:
    338             pass # Ignore any option errors at this point.
     344        options, args = parser.parse_args(self.argv)
     345        handle_default_options(options)
    339346
    340347        try:
    341348            subcommand = self.argv[1]
     
    351358                sys.exit(1)
    352359        # Special-cases: We want 'django-admin.py --version' and
    353360        # 'django-admin.py --help' to work, for backwards compatibility.
    354         elif self.argv[1:] == ['--version']:
    355             # LaxOptionParser already takes care of printing the version.
    356             pass
    357         elif self.argv[1:] in (['--help'], ['-h']):
    358             parser.print_lax_help()
    359             sys.stdout.write(self.main_help_text() + '\n')
     361        # If --version, --help or -h is specified, we want to exit immediately.
    360362        else:
    361363            self.fetch_command(subcommand).run_from_argv(self.argv)
    362364
Back to Top