在 Python 中处理(可能的)可选参数
我正在开发一系列命令行工具,它们连接到同一服务器并执行相关但不同的操作。我希望用户能够拥有一个配置文件,他们可以在其中放置通用参数,例如可以在所有工具之间共享的连接信息。理想情况下,我想要为我执行以下操作的东西:
- 如果在命令行中指定了服务器地址,则使用此地址并忽略任何其他值
- 如果服务器地址未在命令行中指定,但位于配置文件中在命令行中指定使用这个地址。忽略任何其他值。
- 如果服务器地址未在命令行中指定,也未在命令中指定配置文件,但在用户主目录的配置文件中可用(例如 .myapprc),请使用此值。
- 如果在上述任何机制中未指定服务器地址,则退出并显示错误消息。
我见过的最接近的是 configparse 模块,据我所知提供了一个选项解析器,它也会查看配置文件,但似乎没有我需要的“必须在某处指定”的概念。
有谁知道现有的模块可以涵盖我上面的用例?如果没有,对 optparse、configparse 或我尚未审查的其他模块的简单扩展也将不胜感激。
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:
- If the server address is specified at the command line use this and ignore any other values
- 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.
- 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.
- 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此方模块 configparse 是为了扩展 optparse 来自标准 Python 库。正如我指出的 optparse 文档提到的那样,“optparse 不会阻止您实现所需的选项,但也不会为您提供太多帮助”(尽管它后面有几个 URL 向您展示了执行此操作的方法) 。最简单的是使用 默认值 功能:指定一个默认值实际上不是一个合法值(对于像服务器地址这样的东西,这很容易)——然后,一旦处理了选项,验证指定的值是否合法(无论如何这是一个好主意!-)并引发适当的异常,否则。
This-party module configparse is written to extend optparse from the standard Python library. As the optparse docs I pointed to mention, "optparse doesn’t prevent you from implementing required options, but doesn’t give you much help at it either" (though it follows with a couple of URLs that show you ways to do it). Simplest is to use the default value functionality: specify a default value that's not actually a legal value (for something like a server's address, that's pretty easy) -- then, once options are processed, verify that the specified value is legal (which is a good idea anyway!-) and raise the appropriate exception otherwise.
我将 opster 的中间件功能与 SafeConfigParser 按照您的要求实现类似(但稍微简单)的效果。您必须实现您自己描述的特定逻辑,但它足以帮助您使其相对轻松。 opster 中间件使用的一个示例位于其 test/test.py 中 示例。
I've used opster's middleware feature together with SafeConfigParser to achieve a similar (but slightly simpler) effect as you ask. You have to implement the specific logic you described yourself, but it assists you enough to make it relatively painless. An example of opster's middleware use is in its test/test.py example.
使用字典来存储程序的选项。
首先解析用户目录中的选项文件并将每个选项存储在字典中(欢迎 configparse 或任何其他模块)。然后解析命令行(使用您想要的任何模块,optparse可能很适合),如果参数指定配置文件,则解析字典中的指定文件并根据您读取的内容更新您的选项(
dict.update 合并 2 个字典真的很方便)。然后将所有其他参数存储到另一个字典中,并再次合并它们(再次
dict.update
...)。这样,您就可以确定存储选项的字典包含您想要的值,该值可以从用户的文件、指定的配置文件或直接从命令行读取。如果它不包含所需的值,则退出并显示错误。
use a dict to store options to your program.
first parse the option file in the user's directory and store every options in a dict (configparse or any other module is welcome). then parse the command line (using any module you want, optparse might fit well), if an arguments specifies a config file, parse the specified file in a dict and update your options from what you read (
dict.update
is really handy to merge 2 dict). then store all other arguments into another dict, and merge them again (dict.update
again...).this way, you are sure that the dict in which you stored the options contains the value you want, which was either read from the user's file, from the specified config file or directly from the command line. if it does not contain a required value, exit with an error.