更改argparse用法消息自变量顺序 [英] Change argparse usage message argument order

查看:102
本文介绍了更改argparse用法消息自变量顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python3中使用argparse.在我的脚本中,我有一些子解析器,一个位置参数和一些可选参数.我有一个可选参数来传递任意数量的文件路径,它使用的是nargs='*'.我的脚本的用法消息显示如下:

usage: myprog.py subparser1 [-h] [--dir DIR] 
                            [--files [FILES [FILES ...]]]
                            positional_arg

但是,如果您实际上按照此用法消息的建议将positional_arg放在--files标志之后,我认为解析器最终会将其作为文件路径使用(因为nargs='*'),然后抛出错误,因为找不到所需的positional_arg.

我认为使用情况消息的打印方式具有误导性.由于我有多个子解析器,因此我想找到一种方法来更改所有使用情况消息(而无需使用usage=参数手动键入所有内容),以便首先显示位置参数,以期消除混乱./p>

所以我的主要问题是如何更改argparse消息使用部分中参数的排序方式?

编辑以解决一种对我不起作用的可能解决方案.

我不想将--files标志添加为nargs='*'的位置参数.首先,因为这使得它现在显示在帮助消息的位置参数"部分中,而不是与其余可选参数一起显示在可选参数"部分中,因此即使您可以传递0个参数.

其次,因为因为如果我想拥有一个除--files(例如--folders)外还带有任意数量参数的标志,并且如果我将它们都设置为两个位置参数,那么我将遇到相同的位置第一个会一直消耗参数直到最后的问题.如果我将一个位置和一个位置设为可选,那么误导性用法消息仍然是一个问题.

解决方案

这里有两个问题-如何解析参数以及如何格式化用法.

一些快速修复方法:

使用--标记可选列表的结尾和位置的开头.

另一种可能性是将您的positional变成optional.也就是说,给它一个简单的标志,并在需要时提供required参数.然后可以将其放置在子解析器的输入中的任何位置.


https://stackoverflow.com/a/26986546/901925 解决了*可选之后的位置问题.它提出了相同的快速建议.它还显示了如何更改帮助格式器,以不将位置移动到用法的结尾.但这并不能解决多行使用情况.

在最近的SO问题中也讨论了使用顺序, https://stackoverflow.com/a/29619171/901925


如果您不喜欢默认参数分组(位置和可选参数),则可以将argument_groups与不同的标题和描述一起使用.像?"这样的nargs和"*"以及必需"参数,诸如可选"和位置"之类的名称可能会造成混淆.我正在尝试将已标记"用于可选",希望它不会造成混淆.

I'm using argparse in python3. In my script, I have some subparsers, a positional argument and some optional arguments. I have an optional argument to pass in any number of file paths, and it's using nargs='*'. The usage message for my script displays like this:

usage: myprog.py subparser1 [-h] [--dir DIR] 
                            [--files [FILES [FILES ...]]]
                            positional_arg

But if you actually put the positional_arg after the --files flag as suggested by this usage message, I think the parser ends up consuming it as a file path instead (because of the nargs='*'), and then throws an error since it doesn't find the required positional_arg.

I think the way the usage message is printed is misleading. Since I have multiple subparsers, I would like to find a way to change all the usage messages (without typing them all by hand using the usage= argument) just so that the positional argument is displayed first, hopefully clearing up the confusion.

So my main question is how do I change the way the arguments are ordered inside the usage portion of the argparse message?

EDIT to address one possible solution that doesn't work for me.

I don't want to add the --files flag as a positional argument with nargs='*'. Firstly, because that makes it now shows up in the "positional arguments" section of the help message instead of in the "optional arguments" section with the rest of my optional arguments so it makes it look like it is required, even though you could pass it 0 arguments.

Secondly, because because if I wanted to have another flag that takes any number of arguments in addition to the --files (e.g. --folders), and if I made them both positional arguments, I'd run into the same problem where the first one would keep consuming arguments until the end. If I made one positional and one optional then the misleading usage message is still a problem.

解决方案

There are a couple of issues here - how arguments are parsed, and how the usage is formatted.

A couple of quick fixes:

Use -- to mark the end of the optionals list, and the start of the positional(s).

Another possibility is to turn your positional into a optional. That is, give it a simple flag, and required parameter if needed. Then it can be placed anywhere in the subparser's input.


https://stackoverflow.com/a/26986546/901925 addresses this issue of a positional following a * optional. It makes the same quick suggestions. It also shows how the help formatter can be changed to not move the positional to the end of the usage. But it doesn't address the multi-line usage case.

Usage order is also discussed in a more recent SO question, https://stackoverflow.com/a/29619171/901925


If you don't like the default argument groupings (positionals and optionals), you can use argument_groups with different titles and descriptions. With nargs like '?' and '*', and 'required' parameters, names like 'optionals' and 'positionals' can be confusing. I'm trying to use 'flagged' for 'optionals', in the hope that it is less confusing.

这篇关于更改argparse用法消息自变量顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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