如何在python中编码argparse组合选项 [英] How to code argparse combinational options in python

查看:54
本文介绍了如何在python中编码argparse组合选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对要完成的这一小部分工作感到困扰.我做了一些实验,但未能达到结果.

I have been troubled with this small piece of activity to be completed. I did do some experiment, but was not able to achieve the result.

要求:

   test2.py [-c/-v] -f

用法或规则:

  1. -c(比较)带有2个参数.

  1. -c (compare) takes 2 parameter.

-v(验证)带有1个参数.

-v (verify) takes 1 parameter.

必须存在,但不能同时存在.

输出:

我能够获得所需的输出,如下所示

I am able to get the desired output as shown below

kp@kp:~/Study/scripts$ ./test.py -c P1 P2 -f p
kp@kp:~/Study/scripts$ ./test.py -v P1 -f p
kp@kp:~/Study/scripts$ ./test.py -v P1 
usage: test.py <functional argument> <ouput target argument>
test.py: error: argument -f/--file is required
kp@kp:~/Study/scripts$ ./test.py -c P1 P2 
usage: test.py <functional argument> <ouput target argument>
test.py: error: argument -f/--file is required
kp@kp:~/Study/scripts$ 

问题是:

使用时,test.py -h
1. 输出将不会表明-c/-v 都是强制性的,但不是两个 .它指示所有参数都是可选的.
2.输出将在可选参数下指示-f选项,这是不正确的. -f是必需参数,我想在外部显示-可选参数.

When you use, test.py -h,
1. The output will not indicate that -c/-v either of them is mandatory but not both . It indicates all the arguments are optional.
2. The output will indicate -f option under optional arguments which is incorrect. -f is mandatory argument, and I want to display outside - optional arguments.

如何更改脚本,以使-h选项输出更加用户友好(无需任何外部验证)

How to change the script so that -h option output will be more user friendly (without any external validation)

usage: test.py <functional argument> <ouput target argument>

Package Compare/Verifier tool.

optional arguments:
  -h, --help            show this help message and exit
  -f outFileName, --file outFileName
                        File Name where result is stored.
  -c Package1 Package2, --compare Package1 Package2
                        Compare two packages.
  -v Package, --verify Package
                        Verify Content of package.
kiran@kiran-laptop:~/Study/scripts$ 

代码:

我正在使用以下代码来实现输出,

I am using the below code to achieve the output,

#!/usr/bin/python

import sys
import argparse

def main():
    usage='%(prog)s <functional argument> <ouput target argument>'
    description='Package Compare/Verifier tool.'
    parser = argparse.ArgumentParser(usage=usage,description=description)

    parser.add_argument('-f','--file',action='store',nargs=1,dest='outFileName',help='File Name where result is stored.',metavar="outFileName",required=True)


    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument('-c','--compare',action='store',nargs=2,dest='packageInfo',help='Compare two packages.',metavar=("Package1","Package2"))
    group.add_argument('-v','--verify',action='store',nargs=1,dest='packageName',help='Verify Content of package.',metavar='Package')
    args = parser.parse_args()

if __name__ == "__main__":
    main()

推荐答案

您在寻找什么确切的输出?我不知道在--help输出中表示互斥性的任何标准语法,如果对它们进行拼写,这可能会使您的用户感到困惑.我还假设argparse不支持它的语法(因为如果支持的话,它就已经可以使用了.)

What exact output are you looking for? I am not aware of any standard syntax for denoting mutual exclusitivity in a --help output, and it would likely be confusing for your users if you made one up. Also I assume that argparse doesn't support a syntax for it (since if it did, it would already be working).

我建议您保持简单,只向您的用户解释每个参数的帮助中的相互排斥.因此,请如下更改其帮助字符串:

I suggest you keep it simple and just explain to your users the mutual exclusion in the help for each of the arguments. So change their help strings as follows:

-c Package1 Package2, --compare Package1 Package2
                      Compare two packages (may not be used with -v).
-v Package, --verify Package
                      Verify Content of package (may not be used with -c).

那是非常明显的,而且很简洁.

That is extremely obvious and reasonably concise.

另一种选择就是不提及它,让用户通过尝试同时使用它们来发现它们是互斥的(argparse会自动生成用户友好的错误,例如"PROG: error: argument -c: not allowed with argument -v").

Another alternative would be just to not mention it, and have the user discover that they are mutually exclusive by trying to use them simultaneously (argparse automatically generates a user-friendly error such as "PROG: error: argument -c: not allowed with argument -v").

这篇关于如何在python中编码argparse组合选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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