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

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

问题描述

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

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='?').

我的可选参数未传递任何其他参数(action ="store_true"),并且仅用于启用排序,稍后将实现该排序.

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()    

如果在其他任何位置参数之间输入我的-s,则会出现错误.

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

用法:1 [-h] [-s]文件名[源IP] [目标IP]
1:错误:无法识别的参数: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

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

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

推荐答案

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

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).

简短的回答:通常,可选参数应该是所有位置或所有开关.混合和匹配会带来复杂性,使解析复杂的递归过程变得复杂,只能通过试探性猜测正确的解析(尤其是nargs='*'nargs='+',但即使您使用'?'也会出现问题).从sourcedest中删除nargs限定词,或将它们保留为可选内容并转换为开关,将允许以任意顺序传递-s.

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天全站免登陆