Python argparse 可选参数仅在输入正确位置时才有效 [英] Python argparse Optional argument only works when it's entered in the right positions

查看:31
本文介绍了Python argparse 可选参数仅在输入正确位置时才有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的脚本中,我有 3 个位置参数和 1 个可选参数.三个位置参数之一是必需的,其余是可选的(如使用 narg='?' 指定的那样).

我的可选参数不传递任何其他参数(action ='store_true'),只是为了启用稍后实施的排序.

但是,我的问题是我的可选参数仅在它是脚本调用中的第一个或最后一个参数时才有效.

以下是我目前的脚本:

parser = argparse.ArgumentParser()parser.add_argument("-s", "--sort", help="IP 地址排序选项", action='store_true')parser.add_argument("file_name", help="包含tcpdump的文件名")parser.add_argument("source_IP", nargs='?', type=str, help="要搜索的源ip")parser.add_argument("dest_IP", nargs='?', type=str, help="要搜索的目标ip")args = parser.parse_args()

如果我在任何其他位置参数之间输入我的 -s,我会得到一个错误.

 例如:./my_script file.txt -s 192.168.152.32 192.168.0.25

<块引用>

用法:1 [-h] [-s] file_name [source_IP] [dest_IP]
1:错误:无法识别的参数:192.168.152.32 192.168.0.25

我的目标是能够在脚本调用的任何位置输入我的可选参数 (-s) 并使其工作.

解决方案

您有三个位置参数,但由于 nargs='?',其中两个也是可选的.argparse 被搞砸了,因为它看到了位置文件名,然后不得不在将 -s 解释为可选的位置 source 之间任意选择,或作为开关.任何一种解释都是有效的(它没有做复杂的回溯解析来试图找到一些允许它完成的参数的合法解释;使用某些参数类型这样做可能会导致非常糟糕的行为,比如打开一个文件,然后回溯,关闭它,然后打开其他东西).

简短回答:通常,可选参数应该全部是位置参数,或者全部是开关.混合和匹配引入了复杂性,使解析成为一个复杂的递归过程,只能试探性地猜测正确的解析(特别是使用 nargs='*'nargs='+',但即使是 '?' 也会引起问题,如您所见).从 sourcedest 中删除 nargs 限定符,或将它们保留为可选并转换为开关将允许 -s以您喜欢的任何顺序传递.

In my script I have 3 positional arguments and 1 optional argument. One of the three positional arguments is required and the rest is optional (as specified using narg='?').

My optional argument doesn't pass any other arguments (action ='store_true') and is just there to enable sorting which will be implemented at a later time.

However, my problem is that my optional argument only works when it is the first or last argument in the script call.

Below is my script so far:

parser = argparse.ArgumentParser()
parser.add_argument("-s", "--sort", help="option to sort the ip addresses", action='store_true')
parser.add_argument("file_name", help="Name of the file containing the tcpdump")
parser.add_argument("source_IP", nargs='?', type=str, help="Source ip to search") 
parser.add_argument("dest_IP", nargs='?', type=str, help="Destination ip to search")

args = parser.parse_args()    

If I enter my -s between any of the other positional arguments I get an error.

 Ex: ./my_script file.txt -s 192.168.152.32 192.168.0.25

usage: 1 [-h] [-s] file_name [source_IP] [dest_IP]
1: error: unrecognized arguments: 192.168.152.32 192.168.0.25

My goal is to be able to enter my optional argument (-s) anywhere in the script call and have it working.

解决方案

You have three positional arguments, but two of them are also optional thanks to nargs='?'. argparse is getting screwed up because it sees the positional filename, and then has to choose arbitrarily between interpreting the -s as the optional positional source, or as the switch. Either interpretation is valid (it's not doing complicated backtracking parsing to try to find some legal interpretation of the arguments that would allow it to complete; doing so with some argument types could lead to very bad behavior, like opening a file, then backtracking, closing it, and opening something else).

Short answer: In general, optional arguments should be either all positional, or all switches. Mixing and matching introduces complications that would make parsing a complicated recursive process that could only heuristically guess at the correct parsing (particularly with nargs='*' and nargs='+', but even '?' causes problems as you see). Removing the nargs qualifier from source and dest, or leaving them optional and converting to switches will allow -s to be passed in whatever order you like.

这篇关于Python argparse 可选参数仅在输入正确位置时才有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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