Passing command line arguments in django runscript

拟墨画扇 提交于 2019-12-12 01:51:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!