Ruby 的文档 GetoptLong给我的印象是它会从 ARGV 中删除已解析的选项。这是有问题的段落:
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.
然而,事实似乎并非如此。这是我的程序:
#!/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 可以帮我删除它们,那就方便多了。有什么办法可以做到这一点吗?
最佳答案
我想我理解这意味着该项目没有放入 getopt 组中..它留在 ARGV 中,其他所有内容也是如此...为什么它会修改 ARGV?
更新 eww。对文档中的示例进行一些实验后,它不会从 ARGV 中删除项目,直到运行 opts.each...看来删除是访问 opts 中的项目的副作用。
非常糟糕的形式。 :(
关于Ruby GetoptLong 修改 ARGV?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6132772/