问题标签 [optparse]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
6 回答
11618 浏览

python - 在 Python 中解析空选项

我有一个允许您将事件数据发送到自定义脚本的应用程序。您只需布置命令行参数并分配哪些事件数据与哪些参数一起使用。问题是这里没有真正的灵活性。您绘制的每个选项都将被使用,但并非每个选项都必然有数据。因此,当应用程序构建要发送到脚本的字符串时,一些参数是空白的,python 的 OptionParser 错误输出为“错误:--someargument 选项需要一个参数”

由于有超过 200 个数据点,我不能编写单独的脚本来处理可能参数的每种组合(需要 2^200 个脚本)。有没有办法在 python 的 optionparser 中处理空参数?

0 投票
5 回答
4592 浏览

python - Python 的 optparse 可以显示选项的默认值吗?

在使用 --help 显示帮助时,有没有办法让 Python 的 optparse 打印选项或标志的默认值?

0 投票
3 回答
1682 浏览

python - 使用 Python 的 optparse 模块时如何遵守 PEP 257 文档字符串?

根据PEP 257,命令行脚本的文档字符串应该是它的使用信息。

脚本(独立程序)的文档字符串应该可用作其“使用”消息,在使用不正确或缺少参数(或者可能使用“-h”选项,用于“帮助”)调用脚本时打印。这样的文档字符串应该记录脚本的函数和命令行语法、环境变量和文件。使用消息可以相当详细(几个屏幕已满),并且应该足以让新用户正确使用命令,以及对老练用户的所有选项和参数的完整快速参考。

所以我的文档字符串看起来像这样:

现在我想使用 optparse 模块。optparse 生成“选项”部分和解释命令行语法的“用法”:

所以调用带有“-h”标志的脚本会打印:

这可以修改如下:

这导致

但是我怎样才能在这里使用文档字符串呢?将文档字符串作为使用消息传递有两个问题。

  1. 如果文档字符串不以“Usage:”开头,则 optparse 会将“Usage:”附加到文档字符串中
  2. 占位符 '%prog' 必须在文档字符串中使用

结果

根据答案,似乎没有办法重用 optparse 模块预期的文档字符串。所以剩下的选择是手动解析文档字符串并构造OptionParser。(所以我会接受 S.Loot 的回答)

“Usage:”部分由 IndentedHelpFormatter 引入,可以替换为 OptionParser.__init__() 中的 formatter 参数。

0 投票
2 回答
1424 浏览

python - 我应该将参数转发为 *args 和 **kwargs 吗?

optparse我有一个使用 python模块在我的程序中处理命令行参数的类。它也被几个类继承以创建参数子集。为了封装选项解析机制,我只想展示一个add_option继承类的函数。这个函数所做的就是调用optparse.make_option.

add_option简单地让我的方法说它接受与optparse.make_option文档中相同的参数,并将参数转发为*args和,这是一个好习惯**kwargs吗?

我应该事先做一些参数检查吗?在某种程度上,我想避免这种情况,以便将该段代码与特定版本的optparse.

0 投票
3 回答
1085 浏览

python - Python optparse 默认值与函数默认值

我正在编写一个 python 脚本,我希望它既可以从命令行调用,也可以作为库函数导入。理想情况下,命令行选项和函数应该使用同一组默认值。让我在两个地方重用一组默认值的最佳方法是什么?

这是具有重复默认值的当前代码。

0 投票
2 回答
192 浏览

python - Optparse 库 - 存储 arg 时的回调操作

我的代码:

如果我有: script -p hello

我需要 options.test 将参数打印为字符串类型

0 投票
2 回答
2177 浏览

python - 如何使用 optparse 解析自定义字符串?

如何使用 optparse 而不是命令行参数解析自定义字符串?

我想解析一个我从使用中得到的字符串raw_input()。我该如何使用 optparse 呢?

0 投票
3 回答
2273 浏览

python - 如何使用 OptionParser 制作自定义命令行界面?

我正在使用 optparse 模块中的 OptionParser 来解析我使用 raw_input() 获得的命令。

我有这些问题。

1.) 我使用 OptionParser 来解析这个输入,例如。(获取多个参数)

我通过在'-c'的add_option()中设置action ='store_true'来做到这一点,现在如果有另一个带有多个参数的选项说-dxyz那么如何知道哪些参数来自哪个选项?另外,如果必须再次解析其中一个参数,例如

2.)如果我想做这样的事情..

现在每个条目都不能影响上一个命令设置的其他选项。如何完成这些?

0 投票
1 回答
14379 浏览

python - Python optparse 值实例

我怎样才能得到 opt 结果

并将其放在字典中?Python 调用 opt 一个“值实例”,我找不到任何方法将值实例转换为列表或字典。不能以这种方式从 opt 复制项目,

相反,它很笨拙,

这意味着每次添加选项时,我都必须更新此代码;应该有一种方法可以让它自行解决。

0 投票
3 回答
324 浏览

python - Processing (possibly) optional arguments in Python

I am working on a series of command line tools which connect to the same server and do related but different things. I'd like users to be able to have a single configuration file where they can place common arguments such as connection information that can be shared across all the tools. Ideally, I'd like something that does the following for me:

  1. If the server address is specified at the command line use this and ignore any other values
  2. If the server address is not specified at the command line but is in a config file that is specified at the command line use this address. Ignore any other values.
  3. If the server address is not specified at the command line or a config file specified at the command, but is available in a in a config file in the user's home directory (say .myapprc), use this value.
  4. If the server address is not specified in any of the above mechinisms exit with an error message.

The closest I've seen to this is the configparse module, which from what I can tell offers an option parser that will also look at config files, but does not seem to have the notion of "Must be specified somewhere" which I need.

Does anyone know of an existing module that can cover my use case above? If not, a simple extension to optparse, configparse, or some other module I have not reviewed would also be greatly appreciated.