问题
Is there a way to pass command line arguments to a script using django runscript
?
The script I am trying to run uses argparse
to accept command line arguments.
Command line execution of the script:
./consumer --arg1 arg1 --arg2 arg2
Both arg1
and arg2
are required options.
We tried using script-args
but unable to figure out how to use it in this context.
回答1:
Take a look at Django's Commands
class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument('--arg1', help="Something helpful")
def handle(self, *args, **options):
print options['arg1']
And when run:
$python manage.py your_command --arg1 arg
arg
Command works nicely for this sort of thing, as Selcuk said, you can't use arguments in this fashion using RunScript extension.
来源:https://stackoverflow.com/questions/38330829/passing-command-line-arguments-in-django-runscript