开发者

Ruby GetoptLong modifies ARGV?

开发者 https://www.devze.com 2023-03-08 16:06 出处:网络
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:

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?


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. :(

0

精彩评论

暂无评论...
验证码 换一张
取 消