Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何约束每次只能使用一个可选参数调用Python脚本_Python_Scripting_Argparse - Fatal编程技术网

如何约束每次只能使用一个可选参数调用Python脚本

如何约束每次只能使用一个可选参数调用Python脚本,python,scripting,argparse,Python,Scripting,Argparse,我的目标是不能同时使用可选参数,这意味着每次只能使用其中一个选项调用脚本,下面是我的Python代码: parser = argparse.ArgumentParser() parser.add_argument("-a", help="List out all the hostnames", action='store_true') parser.add_argument("-v", help="List out stude

我的目标是不能同时使用可选参数,这意味着每次只能使用其中一个选项调用脚本,下面是我的Python代码:

parser = argparse.ArgumentParser()
parser.add_argument("-a", help="List out all the hostnames", action='store_true')
parser.add_argument("-v", help="List out student information", action='store_true')
parser.add_argument("-d", help="List out all the qualified domain name")
parser.add_argument("-c", help="List out all the qualified IPV4", choices=['A', 'B', 'C'])
parser.add_argument("filename", help="dataset name")
args = parser.parse_args()
另外,如果可选参数超出了选项(-a,-v,-d,-c)的范围,如何创建自定义输出,而不是使用
args=parser.parse_args()
显示默认错误


谢谢你们,我真的很感谢你们的帮助

当用英语描述这种行为时,会弹出“互斥”表达式,快速查看Argparse可以确认Argparse有一个看起来有用的
add\u mutual\u exclusive\u group
方法