Ticket #6017: 6017-r6718.diff

File 6017-r6718.diff, 4.3 KB (added by Russell Keith-Magee, 17 years ago)

Revised patch for lax option parser; comment and test case changes.

  • django/core/management/__init__.py

     
    146146    """
    147147    def error(self, msg):
    148148        pass
     149   
     150    def _process_args(self, largs, rargs, values):
     151        """
     152        Overrides OptionParser._process_args to exclusively handle default
     153        options and ignore args and other options.
     154       
     155        This overrides the behavior of the super class, which stop parsing
     156        at the first unrecognized option.
     157        """
     158        while rargs:
     159            arg = rargs[0]
     160            try:
     161                if arg[0:2] == "--" and len(arg) > 2:
     162                    # process a single long option (possibly with value(s))
     163                    # the superclass code pops the arg off rargs
     164                    self._process_long_opt(rargs, values)
     165                elif arg[:1] == "-" and len(arg) > 1:
     166                    # process a cluster of short options (possibly with
     167                    # value(s) for the last one only)
     168                    # the superclass code pops the arg off rargs
     169                    self._process_short_opts(rargs, values)
     170                else:
     171                    # it's either a non-default option or an arg
     172                    # either way, add it to the args list so we can keep
     173                    # dealing with options
     174                    del rargs[0]
     175                    raise error
     176            except:
     177                largs.append(arg)
    149178
    150179class ManagementUtility(object):
    151180    """
  • tests/regressiontests/management/tests.py

     
     1"""
     2# Tests for LaxOptionParser in core.management
     3
     4# The handling of command line options in the management command requires a 2
     5# pass approach; # --settings and --pythonpath must be extracted on the first
     6# pass, but on the second pass, --settings and --pythonpath can be ignored.
     7# The LaxOptionParser implements the first pass, ignoring any option it
     8# doesn't know about, and providing a list of arguments filtered of any
     9# --settings or --pythonpath values. Any erroneous settings will be caught by
     10# the second pass.
     11
     12>>> from django.core.management import LaxOptionParser, get_version
     13>>> from django.core.management.base import BaseCommand
     14>>> parser = LaxOptionParser(version=get_version(), option_list=BaseCommand.option_list)
     15
     16# Invoke a command with options after the settings and python path
     17>>> options, args = parser.parse_args('django-admin.py command --settings=settingsmodule --pythonpath=/home/user/django-dir --option'.split())
     18>>> print options
     19{'pythonpath': '/home/user/django-dir', 'settings': 'settingsmodule'}
     20>>> args
     21['django-admin.py', 'command', '--option']
     22
     23# Invoke a command with options between the settings and python path
     24>>> options, args = parser.parse_args('django-admin.py command --settings=settingsmodule --option --pythonpath=/home/user/django-dir'.split())
     25>>> print options
     26{'pythonpath': '/home/user/django-dir', 'settings': 'settingsmodule'}
     27>>> args
     28['django-admin.py', 'command', '--option']
     29
     30# Invoke a command with options between the python path and settings
     31>>> options, args = parser.parse_args('django-admin.py command --pythonpath=/home/user/django-dir --option --settings=settingsmodule '.split())
     32>>> print options
     33{'pythonpath': '/home/user/django-dir', 'settings': 'settingsmodule'}
     34>>> args
     35['django-admin.py', 'command', '--option']
     36
     37# Invoke a command with options before the settings and python path
     38>>> options, args = parser.parse_args('django-admin.py command --option --settings=settingsmodule --pythonpath=/home/user/django-dir'.split())
     39>>> print options
     40{'pythonpath': '/home/user/django-dir', 'settings': 'settingsmodule'}
     41>>> args
     42['django-admin.py', 'command', '--option']
     43
     44
     45"""
     46 No newline at end of file
Back to Top