4

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.

user3351750
  • 927
  • 13
  • 24
  • `runscript` can't run any Python script but requires a special script that implements a `run(*args)` method. You can't pass arguments to the script that way. – Selcuk Jul 12 '16 at 14:03

2 Answers2

3

Update in 2020: use --script-args

https://django-extensions.readthedocs.io/en/latest/runscript.html

Rachel
  • 2,858
  • 2
  • 26
  • 30
2

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.

enjoi
  • 274
  • 4
  • 15