如何为 argparse 中的现有参数添加位置选项 [英] How to add positional options for existing arguments in argparse

查看:33
本文介绍了如何为 argparse 中的现有参数添加位置选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个(Python 3.x)脚本(由其他人编写),其中输入和输出当前使用标记的可选参数指定,如下所示:

I'm dealing with a (Python 3.x) script (written by someone else) where the input and output are currently specified with flagged optional arguments like so:

parser.add_argument('-i', '--input', nargs='?', type = argparse.FileType('r'),
                    default=sys.stdin, dest='inputfile')
parser.add_argument('-o', '--output-file', nargs='?', type=argparse.FileType('w'),
                    default=sys.stdout, dest='outputfile')

我想升级此脚本,以便可以将输入和输出文件指定为位置参数,同时保持现有标志参数以实现向后兼容性.我还想智能地处理可能来自混合标记参数和位置参数的潜在冲突(即,如果只给出了 -i-o 之一然后一个位置参数会自动传递给另一个,两个位置参数会引发冗余错误,而如果同时给出 -i-o,则任何位置参数都会引发冗余错误冗余错误).

I'd like to upgrade this script so that the input and output file can be specified as positional arguments while maintaining the existing flag arguments for backwards compatibility. I'd also like to intelligently handle the potential conflicts which might come from mixing the flagged argument with the positional argument (i.e. if only one of -i or -o is given then a single positional argument is automatically passed to the other and two positional arguments raises a redundancy error while if both -i and -o are given, then any positional arguments raises the redundancy error).

注意:当前编写的脚本不接受任何位置参数,尽管它确实接受其他标志,除了与输入和输出文件相关的标志之外,有些有参数有些没有.

Note: the script as currently written does not accept any positional arguments, though it does accept other flags, some with arguments some without, besides the ones related to the input and output file.

这是否可以通过 argparse 实现(如果可以,如何实现),或者我是否必须使用其他方法重写参数解析(如果可以,您有什么建议)?

Is this possible with argparse (and if so, how) or do I have to rewrite the argument parsing using something else (and if so, what do you suggest)?

推荐答案

坚持使用 FileType 会很尴尬.该 type 打开或创建文件.因此,当您只需要 2 个文件时,可能会有 4 个打开的文件.但如果其中一个文件是 stdinout,您就不想关闭它.而且你不能处理一个 positional ,它可以被读取或写入,这取决于给定的其他参数.

Sticking with the FileType will be awkward. That type opens or creates the file. So potentially you'll have 4 open files when you only want 2. But if one of those files is stdin or out you don't want to close it. And you can't handle a positional which could either be read or write depending what other arguments are given.

您可以尝试定义 4 个默认字符串参数,2 个标记和 2 个 nargs='?' 位置.给他们不同的dest.然后,您可以将您的智慧应用于 4 个可能的值.默认的默认 None 应该是一个足够清楚的指示,表明没有提供值.一旦确定了两个文件名,就可以打开并使用它们.较新的 Python 建议使用 with 上下文,尽管当文件已经打开时(例如 sys.stdin)这可能会很尴尬.

You could try defining 4 default string arguments, 2 flagged, and 2 nargs='?' positional. Give them different dest. Then you can apply your intelligence to the 4 possible values. The default default None should be a clear enough indication that a value wasn't provided. Once you've decided on the two filenames, then you can open and use them. Newer Python recommends using with contexts, though that can be awkward when a file is already open (e.g. sys.stdin).

我认为您不应该尝试在 argparse 中实现该逻辑.解析后做.

I don't think you should try to implement that logic within argparse. Do it after parsing.

这篇关于如何为 argparse 中的现有参数添加位置选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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