Ticket #11407: command_parser.diff
File command_parser.diff, 3.4 KB (added by , 13 years ago) |
---|
-
django/core/management/__init__.py
147 147 148 148 return klass.execute(*args, **defaults) 149 149 150 class LaxOptionParser(OptionParser):150 class ManagementUtilityOptionParser(OptionParser): 151 151 """ 152 152 An option parser that doesn't raise any errors on unknown options. 153 153 154 154 This is needed because the --settings and --pythonpath options affect 155 155 the commands (and thus the options) that are available to the user. 156 156 """ 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 157 165 def error(self, msg): 158 166 pass 159 167 160 168 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 165 171 """ 166 pass 172 self.print_lax_help() 173 sys.stdout.write(self.main_help_text + '\n') 167 174 168 175 def print_lax_help(self): 169 176 """Output the basic options available to every command. … … 197 204 # either way, add it to the args list so we can keep 198 205 # dealing with options 199 206 del rargs[0] 200 raise Exception 201 except: 207 raise Exception() 208 209 except Exception: 202 210 largs.append(arg) 203 211 204 212 class ManagementUtility(object): … … 327 335 # Preprocess options to extract --settings and --pythonpath. 328 336 # These options could affect the commands that are available, so they 329 337 # must be processed early. 330 parser = LaxOptionParser(usage="%prog subcommand [options] [args]",338 parser = ManagementUtilityOptionParser(usage="%prog subcommand [options] [args]", 331 339 version=get_version(), 332 option_list=BaseCommand.option_list) 340 option_list=BaseCommand.option_list, 341 main_help_text=self.main_help_text()) 342 333 343 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) 339 346 340 347 try: 341 348 subcommand = self.argv[1] … … 351 358 sys.exit(1) 352 359 # Special-cases: We want 'django-admin.py --version' and 353 360 # '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. 360 362 else: 361 363 self.fetch_command(subcommand).run_from_argv(self.argv) 362 364