Ruby GetoptLong 修改 ARGV?

标签 ruby getopt getopt-long

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/

相关文章:

ruby - 使用 apache tomcat 部署 JRuby 应用程序时出错

ruby - ruby 中的短 namespace 首字母缩写词

c - 如何在 C 中使用 getopt 和多个命令打印一次输出

c - getopt_long 问题

c - 是否可以通过 getopt_long 参数列表向后移动?

c - 使用getopt_long,如何在optarg中存储两个值?

ruby - 不调用 `Hash#to_a` 将 Ruby 哈希转换为数组

ruby - 院子@return不为空

c - 使用 getopt 获取 optarg

c - getopt 给出 -1 作为返回值