Ruby CLI 应用程序配置/参数管理

发布于 2024-09-15 03:41:55 字数 408 浏览 20 评论 0原文

我目前正在 Ruby 中开发 CLI 应用程序,我正在使用 Trollop (http://trollop.rubyforge.org /) 用于处理 cli 参数。

我还想实现将所需选项存储在 ~/.mycfg 以及 cwd/.mycfg 中的可能性,后者优先。

我试图实现的行为是:

如果 .mycfg 存在于当前工作目录中,则从那里加载默认选项,否则,如果它存在于用户的主目录中,则从那里加载选项。

如果这些选项也作为参数传递,我将覆盖它们。

问题是,配置文件应该采用什么格式?我考虑过 YAML,但是如何将 Trollop 为参数生成的数组与 YAML 合并,或者反之亦然。

I'm currently working on a CLI app in Ruby, I'm using Trollop (http://trollop.rubyforge.org/) for dealing with cli arguments.

I'd also like to implement the possibility of storing the required options in ~/.mycfg as well as cwd/.mycfg, the latter taking precedence.

The behaviour I'm trying to implement is:

If .mycfg exists in the current working directory, load default options from there, otherwise, if it exists in the user's home directory, load options from there.

I will then override those options if they are also passed as arguments.

The question is, what format should the config file be in? I've thought about YAML, but then how to I merge the array that Trollop generates for the parameters with the YAML, or the other way around.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角卖回忆 2024-09-22 03:41:55

您可以尝试的一件简单的事情是在 Trollop 处理之前修改 ARGV。首先,读取配置文件并将存储在其中的数据转换为等效命令行选项的数组。现在,将该数组添加到 ARGV 之前,然后然后将 Trollop 设置为宽松。这样,Trollop 将解析您的所有参数(无论它们来自配置文件还是来自 CLI)。由于您的配置文件参数现在列在 CLI 参数之前,因此任何 CLI 选项都将覆盖匹配的配置文件选项(因为后面的选项会覆盖前面的选项)。

您的配置文件可以采用您想要使用的任何格式,但有时最简单的形式可能是最简单的。尝试使用纯文本格式,其中配置文件内容只是您想要传递给脚本的确切参数。也就是说,文件的内容被写入使得调用:

your_script.rb `cat optionsfile` -more -CLI -options

将按预期工作。以这种格式存储选项使它们易于编辑和处理。只需读取文件(应该是单行)并调用 String.split(' ') 将选项拆分为数组,就像它们来自 ARGV 时出现的那样代码>.一种变体是拥有一个多行配置文件,每行一个 CLI 参数。在这种情况下,您可以使用类似 File.each_line(configfile) {|x| 的方式一次一行地构建选项数组。选项_数组<< x}。

One easy thing you can try is to modify ARGV before Trollop processes it. First, read in your config file and convert the data stored there into an array of equivalent command-line options. Now, prepend that array to ARGV and then set Trollop loose. That way, Trollop will parse all of your arguments (whether they came from the config file or from the CLI). Since your config file parameters are now listed before the CLI parameters, any CLI option will override a matching config file option (since later options override earlier options).

Your config file can be in any format you want to use, but sometimes the simplest form can be the easiest. Try a plain text format where the config file contents are simply the exact parameters that you want to pass to your script. That is to say, the contents of the file are written such that calling:

your_script.rb `cat optionsfile` -more -CLI -options

would work as expected. Storing options in this format makes them easy to edit and easy to process. Simply read in the file (should be a single line) and call String.split(' ') to split the options into an array, just like they would appear when coming from ARGV. A variation is to have a multi-line config file with one CLI parameter per line. In that case, you would build up your array of options one line at a time using something like File.each_line(configfile) {|x| options_array << x}.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文