【问题标题】:Passing command line arguments in django runscript在 django runscript 中传递命令行参数
【发布时间】:2016-11-14 19:27:16
【问题描述】:

有没有办法使用 django runscript 将命令行参数传递给脚本? 我尝试运行的脚本使用argparse 来接受命令行参数。

脚本的命令行执行:

./consumer --arg1 arg1 --arg2 arg2

arg1arg2 都是必需选项。

我们尝试使用script-args,但无法弄清楚如何在这种情况下使用它。

【问题讨论】:

  • runscript 不能运行任何 Python 脚本,但需要一个实现 run(*args) 方法的特殊脚本。您不能以这种方式将参数传递给脚本。

标签: python django python-2.7 django-extensions


【解决方案1】:

看看Django的Commands

class Command(BaseCommand):
    def add_arguments(self, parser):
        parser.add_argument('--arg1', help="Something helpful")

    def handle(self, *args, **options):
        print options['arg1']

运行时:

$python manage.py your_command --arg1 arg
arg

Command 非常适合这类事情,正如 Selcuk 所说,您不能使用 RunScript 扩展以这种方式使用参数。

【讨论】:

    【解决方案2】:

    【讨论】:

    • Django-extensions 是一个很好的包,但是它的文档很差。
    猜你喜欢
    • 2016-03-25
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    • 2012-07-11
    • 2020-08-24
    • 2018-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多