Ticket #11407: 11407-2.diff

File 11407-2.diff, 4.7 KB (added by Claude Paroz, 6 years ago)
  • django/core/management/__init__.py

    diff --git a/django/core/management/__init__.py b/django/core/management/__init__.py
    index 7638937..0cdfbef 100644
    a b def call_command(name, *args, **options): 
    160160
    161161    return klass.execute(*args, **defaults)
    162162
     163
    163164class LaxOptionParser(OptionParser):
    164165    """
    165166    An option parser that doesn't raise any errors on unknown options.
    class LaxOptionParser(OptionParser): 
    167168    This is needed because the --settings and --pythonpath options affect
    168169    the commands (and thus the options) that are available to the user.
    169170    """
     171    def __init__(self, utility, **kwargs):
     172        self.utility = utility
     173        OptionParser.__init__(self, **kwargs)
     174
    170175    def error(self, msg):
    171176        pass
    172177
    173178    def print_help(self):
    174         """Output nothing.
    175 
    176         The lax options are included in the normal option parser, so under
    177         normal usage, we don't need to print the lax options.
    178179        """
    179         pass
    180 
    181     def print_lax_help(self):
    182         """Output the basic options available to every command.
    183 
    184         This just redirects to the default print_help() behavior.
     180        Either output the general help or trigger the help output of any
     181        subcommand.
    185182        """
    186         OptionParser.print_help(self)
     183        if len(self.largs) > 1 and self.largs[-1] != 'help':
     184            # Calling help for subcommand instead
     185            self.utility.fetch_command(self.largs[-1]).print_help(*self.largs)
     186        else:
     187            OptionParser.print_help(self)
     188            sys.stdout.write(self.utility.main_help_text() + '\n')
    187189
    188190    def _process_args(self, largs, rargs, values):
    189191        """
    class LaxOptionParser(OptionParser): 
    211213                    # dealing with options
    212214                    del rargs[0]
    213215                    raise Exception
    214             except:
     216            except Exception:
    215217                largs.append(arg)
    216218
     219
    217220class ManagementUtility(object):
    218221    """
    219222    Encapsulates the logic of the django-admin.py and manage.py utilities.
    class ManagementUtility(object): 
    355358        # Preprocess options to extract --settings and --pythonpath.
    356359        # These options could affect the commands that are available, so they
    357360        # must be processed early.
    358         parser = LaxOptionParser(usage="%prog subcommand [options] [args]",
     361        parser = LaxOptionParser(self,
     362                                 usage="%prog subcommand [options] [args]",
    359363                                 version=get_version(),
    360364                                 option_list=BaseCommand.option_list)
    361365        self.autocomplete()
    362         try:
    363             options, args = parser.parse_args(self.argv)
    364             handle_default_options(options)
    365         except:
    366             pass # Ignore any option errors at this point.
     366        options, args = parser.parse_args(self.argv)
     367        handle_default_options(options)
    367368
    368369        try:
    369370            subcommand = self.argv[1]
    class ManagementUtility(object): 
    372373
    373374        if subcommand == 'help':
    374375            if len(args) <= 2:
    375                 parser.print_lax_help()
    376                 sys.stdout.write(self.main_help_text() + '\n')
     376                parser.print_help()
    377377            elif args[2] == '--commands':
    378378                sys.stdout.write(self.main_help_text(commands_only=True) + '\n')
    379379            else:
    380380                self.fetch_command(args[2]).print_help(self.prog_name, args[2])
    381381        elif subcommand == 'version':
    382382            sys.stdout.write(parser.get_version() + '\n')
    383         # Special-cases: We want 'django-admin.py --version' and
    384         # 'django-admin.py --help' to work, for backwards compatibility.
    385         elif self.argv[1:] == ['--version']:
    386             # LaxOptionParser already takes care of printing the version.
    387             pass
    388         elif self.argv[1:] in (['--help'], ['-h']):
    389             parser.print_lax_help()
    390             sys.stdout.write(self.main_help_text() + '\n')
    391383        else:
    392384            self.fetch_command(subcommand).run_from_argv(self.argv)
    393385
  • tests/admin_scripts/tests.py

    diff --git a/tests/admin_scripts/tests.py b/tests/admin_scripts/tests.py
    index baec168..d255318 100644
    a b class CommandTypes(AdminScriptTestCase): 
    12171217        "--version is equivalent to version"
    12181218        args1, args2 = ['version'], ['--version']
    12191219        self.assertEqual(self.run_manage(args1), self.run_manage(args2))
     1220        # Whatever the accompanying command, --version always quit
     1221        args3 = ['grumble', '--version']
     1222        self.assertEqual(self.run_manage(args1), self.run_manage(args3))
    12201223
    12211224    def test_help(self):
    12221225        "help is handled as a special case"
Back to Top