python click app失败,并显示“缺少参数".但找不到问题 [英] python click app is failing with indication of "missing argument" but cannot locate the issue

查看:302
本文介绍了python click app失败,并显示“缺少参数".但找不到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在python 3.6中用Click创建一个应用程序.我似乎有一个找不到的错误.我已经在下面发布了我的代码.

I am creating an app with Click in python 3.6. I seem to have an error that I cannot find. I have posted my code below.

问题是当我键入:

python clickapp.py --help

一切都很好.但是当我输入

all is good. But when I type

python clickapp.py simplerequest --help

我收到一条错误消息,指示我缺少参数目录",但是没有回溯来指示错误的来源.确切的输出是:

I get an error indicating that I am missing the argument "directory", but there is no traceback to indicate the source of the error. The exact output is:

Usage: clickapp.py [OPTIONS] STARTDATE ENDDATE DIRECTORY COMMAND [ARGS]...

Error: Missing argument "directory".

这是我使用的代码.这是文件名为clickapp.py的最小示例.

Here is the code that I used. This is a minimal example with filename clickapp.py.

import click

@click.group()
@click.argument('startdate')
@click.argument('enddate')
@click.argument('directory', type=click.Path())
@click.pass_context
def cli(ctx,
        startdate,
        enddate,
        directory):

    ctx.obj['directory'] = directory 
    ctx.obj['startdate'] = startdate
    ctx.obj['enddate'] = enddate


@click.command()
@click.argument('arg1', type=str)
@click.argument('arg2', type=click.Path(exists=True))
@click.argument('arg3', type=int)
@click.pass_context
def SimpleRequest(ctx,
                  arg1,
                  arg2,
                  arg3):

    """Simple request processor"""

    ctx.obj['arg1'] = arg1
    ctx.obj['arg2'] = arg2
    ctx.obj['arg3'] = arg3

cli.add_command(SimpleRequest)

if __name__ == '__main__':
    cli(obj={})

任何人都可以看到错误的根源吗?

Can anyone see the source of the error?

我尝试了几件事:

  1. 我删除了arg2中的type=click.Path(exists=True),以查看是否可能引起问题.但这并没有改变任何东西.

  1. I removed the type=click.Path(exists=True) in arg2 to see if that might be causing the problem. But that did not change anything.

我还尝试删除了一些有关将变量插入到ctx字典中的验证逻辑,但是这也没有帮助.

I also tried to remove some validation logic around inserting the variables into the ctx dictionary, but that did not help either.

推荐答案

您的帮助解析问题是click试图首先填充startdate endate directory参数.

Your help parsing problem is that click is attempting to fill in the startdate endate directory arguments first.

因此,您的simplerequest --help填写前两个,然后抱怨缺少第三个(directory).

So your simplerequest --help fills in the first two and then it complains that the third (directory) is missing.

通常,命令(simplerequest)将是第一个参数.然后,您可以在前面添加可选的args,因为click可以轻松将其与命令区分开.

Usually the command (simplerequest) would be the first argument. You could then add optional args before, since click can easily distinguish those from the command.

或者您也可以强制单击以将第一个arg视为有效命令并进行帮助,但这是非标准的,并且需要一些代码.此外,尽管它们很容易区分,但它会排除命令名称作为开始日期.

Or you could coerce click to see the first arg as a valid command and do the help anyways, but this is non-standard and would require some code. Also it would preclude the command name being the start date, although they are likely easily differentiated.

这篇关于python click app失败,并显示“缺少参数".但找不到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆