You defined the argument with the '--type' flag and made it required . This command line will require a line or lines that look like --type avalue .
This looks like the corresponding part of call_command :
def call_command(name, *args, **options): .... parser = command.create_parser('', name) if command.use_argparse:
It creates a parser using its own arguments, plus the ones you add.
parser._actions if s_opt.option_strings are the arguments (actions) that accept the option flag (start with - or -). opt_mapping is the map between the flag lines (minus the leading -s) and the dest attribute.
arg_options converts your **kwargs into something that can be combined with parser output.
defaults = parser.parse_args(args=args) the actual parsing is performed. That is, this is the only code that actually uses the argparse parsing argparse . Thus, part of your *args call mimics the generation of sys.argv[1:] from an interactive call.
Based on this reading, I think this should work:
args = [ '--solr-url', 'http://127.0.0.1:8983/solr/collection1', '--type', 'opinions', '--update' '--everything', '--do_commit', '--traceback', } call_command('cl_update_index', *args)
Instead of **kwargs I pass the values as a list of strings. Or the two required arguments can be passed to args , and the rest to **kwargs .
args = ['--solr-url', 'http://127.0.0.1:8983/solr/collection1', '--type', 'opinions'] kwargs = { 'update': True, 'everything': True, 'do_commit': True, 'traceback': True, } call_command('cl_update_index', *args, **kwargs)
If the argument is required , it must go through *args . **kwargs bypasses the parser, forcing it to bypass missing arguments.
I downloaded the latest version of django but did not install it. But here is the call_command simulation, which should check the call parameters:
import argparse def call_command(name, *args, **options): """ Calls the given command, with the given options and args/kwargs. standalone simulation of django.core.mangement call_command """ command = name """ .... """
which produces:
python3 stack32036562.py args: ('argsvalue',) kwargs: {'optional': 'default', 'type': 'typevalue', 'update': True, 'skip_checks': True, 'foo': 'foovalue'} args: ('1', '2') kwargs: {'optional': 'default', 'update': True, 'foo': 'foovalue', 'type': 'opinions', 'skip_checks': True, 'everything': True, 'solr_url': 'http://127.0.0.1...'}
With a bunch of stubs, I can make your cl Command work with my BaseCommand , and the following call works:
clupdate = Command() args = ['--type','opinions','--solr-url','dummy'] kwargs = { 'solr_url': 'http://127.0.0.1:8983/solr/collection1', #'type': 'opinions', 'update': True, 'everything': True, 'do_commit': True, 'traceback': True, } call_command(clupdate, *args, **kwargs)
doing everything stubs.
Running in update mode... everything args: () options: {'type': 'opinions', 'query': None, 'solr_url': 'http://127.0.0.1:8983/solr/collection1', 'items': None, 'do_commit': True, 'update': True, 'delete': False, 'datetime': None, 'optimize': False, 'skip_checks': True, 'everything': True, 'traceback': True}