我可以使用 Ruby 的 OptionParser 接受任意参数模式吗?

发布于 2024-09-10 15:43:52 字数 1079 浏览 3 评论 0原文

假设我有一个简单的 Ruby 应用程序,我希望第一个参数(如果有)指定环境:TESTDEVELOPMENTProduction (如果没有给出参数,则默认为 DEVELOPMENT)。例如,

ruby myapp.rb test

将在 TEST 模式下运行它。此外,还应该接受简写,以便例如

ruby myapp.rb t

可以在 TEST 模式下运行应用程序,并

ruby myapp.rb dev

DEVELOPMENT 模式下运行应用程序。

我想使用 OptionParser,但它的行为非常奇怪。如果 myapp.rb 是,

require 'optparse'

environment = 'DEVELOPMENT'
opts = OptionParser.new
opts.on('test')        { environment = 'TEST' }
opts.on('production')  { environment = 'PRODUCTION' }
opts.parse!(ARGV)

那么无论我传递什么参数,环境都会变成 PRODUCTION ;由于某种原因,opts.on('product') 总是执行它的块。 (如果我使用像 '-product' 这样的标志样式字符串,则不会。)并且我无法让 OptionParser 查找以 't' 开头的字符串,而不是确切的字符串。字符串“测试”。

也许 OptionParser 不适合这项工作。显然,我自己拆分 ARGV 是微不足道的。我只是想知道这种行为是怎么回事。我使用的是 Ruby 1.9.2。

Let's say that I have a simple Ruby app where I want the first argument (if any) to specify the environment: TEST, DEVELOPMENT or PRODUCTION (with DEVELOPMENT being the default if no argument is given). For instance,

ruby myapp.rb test

would run it in TEST mode. Also, shorthands should be accepted, so that for instance

ruby myapp.rb t

would run the app in TEST mode and

ruby myapp.rb dev

would run it in DEVELOPMENT mode.

I'd like to use OptionParser, but it behaves very weirdly. If myapp.rb is

require 'optparse'

environment = 'DEVELOPMENT'
opts = OptionParser.new
opts.on('test')        { environment = 'TEST' }
opts.on('production')  { environment = 'PRODUCTION' }
opts.parse!(ARGV)

then environment becomes PRODUCTION no matter what arguments I pass; for some reason, opts.on('production') always executes its block. (It doesn't if I use a flag-style string like '-production' instead.) And there's no way I can see to have OptionParser look for strings starting with 't' rather than the exact string 'test'.

Maybe OptionParser is the wrong tool for the job. Obviously it would be trivial to split up ARGV myself. I'm just wondering what's going on with this behavior. I'm on Ruby 1.9.2.

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

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

发布评论

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

评论(2

粉红×色少女 2024-09-17 15:43:52

如果您使用 parse! 方法,则 opts.on 调用处理的任何参数都会从 ARGV 数组中破坏性地。这意味着在 parse! 方法之后,ARGV 数组的原始内容将不再包含这些标志。

我建议通过将 ARGV 与包含“测试”和“生产”的数组进行比较来手动解析剩余的参数集。

查看文档:
http://ruby -doc.org/stdlib-1.9.3/libdoc/optparse/rdoc/OptionParser.html#method-i-parse-21

Provided you use the parse! method, any arguments handled by opts.on calls are stripped from the ARGV array destructively. This means that the original contents of the ARGV array will no longer contain those flags after the parse! method.

I recommend parsing the remaining set of arguments manually by comparing ARGV to an array containing 'test' and 'production'.

Check out the doc:
http://ruby-doc.org/stdlib-1.9.3/libdoc/optparse/rdoc/OptionParser.html#method-i-parse-21

晨曦÷微暖 2024-09-17 15:43:52

我想说,在运行 OptionParser 之前,您需要从 ARGV 中解析出类似的参数,

例如

env = ARGV.select{|arg| arg =~ /dev/test/prod/i}.first

PS我推荐特罗洛普。我发现它更简单,而且选择默认值也很好。

I'd say you need to parse out arguments like that from ARGV before running OptionParser

e.g.

env = ARGV.select{|arg| arg =~ /dev/test/prod/i}.first

P.S. I'd recommend Trollop. I find it much simpler, and it's good about picking defaults.

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