我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用django.core.management.base.BaseCommand.option_list()。
def __init__(self, *args, **kwargs): # Options are defined in an __init__ method to support swapping out # custom user models in tests. super(Command, self).__init__(*args, **kwargs) self.UserModel = get_user_model() self.username_field = self.UserModel._meta.get_field(self.UserModel.USERNAME_FIELD) self.option_list = BaseCommand.option_list + ( make_option('--%s' % self.UserModel.USERNAME_FIELD, dest=self.UserModel.USERNAME_FIELD, default=None, help='Specifies the login for the superuser.'), make_option('--noinput', action='store_false', dest='interactive', default=True, help=('Tells Django to NOT prompt the user for input of any kind. ' 'You must use --%s with --noinput, along with an option for ' 'any other required field. Superusers created with --noinput will ' ' not be able to log in until they\'re given a valid password.' % self.UserModel.USERNAME_FIELD)), make_option('--database', action='store', dest='database', default=DEFAULT_DB_ALIAS, help='Specifies the database to use. Default is "default".'), ) + tuple( make_option('--%s' % field, dest=field, default=None, help='Specifies the %s for the superuser.' % field) for field in self.UserModel.REQUIRED_FIELDS )
def __init__(self, **kwargs): if self.options: self.option_list = self.__class__.option_list + self.options super(DanubeCloudCommand, self).__init__(**kwargs)
def create_parser(self, prog_name, subcommand): test_runner_class = get_runner(settings, self.test_runner) options = self.option_list + getattr( test_runner_class, 'option_list', ()) return OptionParser(prog=prog_name, usage=self.usage(subcommand), version=self.get_version(), option_list=options)
def call_command(name, *args, **options): """ Calls the given command, with the given options and args/kwargs. This is the primary API you should use for calling specific commands. Some examples: call_command('syncdb') call_command('shell', plain=True) call_command('sqlall', 'myapp') """ # Load the command object. try: app_name = get_commands()[name] except KeyError: raise CommandError("Unknown command: %r" % name) if isinstance(app_name, BaseCommand): # If the command is already loaded, use it directly. klass = app_name else: klass = load_command_class(app_name, name) # Grab out a list of defaults from the options. optparse does this for us # when the script runs from the command line, but since call_command can # be called programatically, we need to simulate the loading and handling # of defaults (see #10080 for details). defaults = {} for opt in klass.option_list: if opt.default is NO_DEFAULT: defaults[opt.dest] = None else: defaults[opt.dest] = opt.default defaults.update(options) return klass.execute(*args, **defaults)
def option_list(self): return [x for x in self.options if x._long_opts[0] not in self.skip_opts]
def execute(self): """ Given the command-line arguments, this figures out which subcommand is being run, creates a parser appropriate to that command, and runs it. """ # Preprocess options to extract --settings and --pythonpath. # These options could affect the commands that are available, so they # must be processed early. parser = LaxOptionParser(usage="%prog subcommand [options] [args]", version=get_version(), option_list=BaseCommand.option_list) self.autocomplete() try: options, args = parser.parse_args(self.argv) handle_default_options(options) except: pass # Ignore any option errors at this point. try: subcommand = self.argv[1] except IndexError: subcommand = 'help' # Display help if no arguments were given. if subcommand == 'help': if len(args) <= 2: parser.print_lax_help() sys.stdout.write(self.main_help_text() + '\n') elif args[2] == '--commands': sys.stdout.write(self.main_help_text(commands_only=True) + '\n') else: self.fetch_command(args[2]).print_help(self.prog_name, args[2]) elif subcommand == 'version': sys.stdout.write(parser.get_version() + '\n') # Special-cases: We want 'django-admin.py --version' and # 'django-admin.py --help' to work, for backwards compatibility. elif self.argv[1:] == ['--version']: # LaxOptionParser already takes care of printing the version. pass elif self.argv[1:] in (['--help'], ['-h']): parser.print_lax_help() sys.stdout.write(self.main_help_text() + '\n') else: self.fetch_command(subcommand).run_from_argv(self.argv)