gpt4 book ai didi

python - 如何在 python 中编写 argparse 组合选项

转载 作者:IT老高 更新时间:2023-10-28 21:00:41 25 4
gpt4 key购买 nike

我一直对要完成的这一小块事件感到困扰。我确实做了一些实验,但无法达到结果。

要求:

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

用法或规则:

  1. -c(比较)接受2个参数。

    -v(验证)采用 1 个参数。

    其中一个必须存在,但不能同时存在

  2. -f 是强制参数(输出文件名)。

输出:

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

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 是强制参数,我想在外面显示 - 可选参数。

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

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$

代码:

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

#!/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()

最佳答案

将文件名设置为位置参数,并让argparse设置自己的使用信息:

$ python so.py --help
usage: so.py [-h] [-c Package1 Package2 | -v Package] outFileName

文件名应该是位置的,你应该让 argparse 写它​​自己的使用信息。

代码

#!/usr/bin/python

import sys
import argparse

def main():
description='Package Compare/Verifier tool.'
parser = argparse.ArgumentParser(description=description,
epilog='--compare and --verify are mutually exclusive')

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

group = parser.add_mutually_exclusive_group(required=False)
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()

帮助信息

$ python so.py --help
usage: so.py [-h] [-c Package1 Package2 | -v Package] outFileName

Package Compare/Verifier tool.

positional arguments:
outFileName File Name where result is stored.

optional arguments:
-h, --help show this help message and exit
-c Package1 Package2, --compare Package1 Package2
Compare two packages.
-v Package, --verify Package
Verify Content of package.

--compare and --verify are mutually exclusive

关于python - 如何在 python 中编写 argparse 组合选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5603364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com