Ruby GetoptLong 修改 ARGV?
Ruby 的 GetoptLong 的文档给我的印象是将从 ARGV 中删除已解析的选项。这是有问题的段落:
例如,如果 -a 不需要参数并且 -b 可选地接受参数, 解析 '-a one -b 两个三' 将导致 ('-a','') 和 ('-b', 'two') 被处理为选项/参数对,并且 'one','third' 被处理留在ARGV中。
然而,情况似乎并非如此。这是我的程序:
#!/usr/bin/env ruby
require "getoptlong"
opts = GetoptLong.new(
["--start", "-s", GetoptLong::REQUIRED_ARGUMENT],
["--base", "-b", GetoptLong::REQUIRED_ARGUMENT]
)
puts ARGV
这是输出:(
$ number-photos --start 2 --base foo *
--start
2
--base
foo
aac-to-mp3
backup-wp-uploads
exiv-webcam-imgs
get-updates
music-to-lily
number-photos
ogg-to-mp3
rename-music
restore-uploads
resymlink
sprints
sync-jt
sync-st
timestamp
unix-names
我确实检查过我使用的是 ruby 1.9.2。)
我可以手动删除这些选项,但这会有点令人头痛,因为这取决于你如何传递它们其中,每个可以占用数组中的一个或两个槽(--base=foo
与 --base foo
)。如果 GetoptLong 可以帮我删除它们,那就方便多了。有什么办法可以做到这一点吗?
The documentation for Ruby's GetoptLong gave me the impression that it would remove the parsed options from ARGV. Here's the passage in question:
For example, if -a does not require an argument and -b optionally takes an argument,
parsing ’-a one -b two three’ would result in (’-a’,’’) and (’-b’, ‘two’) being processed as option/arg pairs, and ‘one’,’three’ being left in ARGV.
However, this doesn't seem to be the case. Here is my program:
#!/usr/bin/env ruby
require "getoptlong"
opts = GetoptLong.new(
["--start", "-s", GetoptLong::REQUIRED_ARGUMENT],
["--base", "-b", GetoptLong::REQUIRED_ARGUMENT]
)
puts ARGV
And here is the output:
$ number-photos --start 2 --base foo *
--start
2
--base
foo
aac-to-mp3
backup-wp-uploads
exiv-webcam-imgs
get-updates
music-to-lily
number-photos
ogg-to-mp3
rename-music
restore-uploads
resymlink
sprints
sync-jt
sync-st
timestamp
unix-names
(I did check that I'm on ruby 1.9.2.)
I could remove the options manually, but this would be a bit of a headache since, depending on how you pass them in, each could take up either one or two slots in the array (--base=foo
vs. --base foo
). It would be much more convenient if GetoptLong could remove them for me. Is there any way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我理解这意味着该项目没有放入 getopt 组中..它留在 ARGV 中,其他所有内容也是如此...为什么它会修改 ARGV?
更新 eww。对文档中的示例进行一些实验后,它不会从 ARGV 中删除项目,直到 opts.each 运行...看起来删除是访问 opts 中的项目的副作用。
形式非常糟糕。 :(
I guess I understood that to mean that the item was not put in the getopt group.. it's left in ARGV, as is everything else... Why would it modify ARGV?
update eww. after some experimentation with the example in the documentation, it does not remove items from ARGV until the opts.each is run... it appear that removal is a side effect of visiting the item in opts.
Very bad form. :(