As mentioned above, the command-line is being parsed before the OS hands off the command to Ruby. The wildcard is being expanded into a list of space-delimited filenames.
You can see what will happen if you type something like echo *
at the command-line, then, instead of hitting Return, instead hit Esc then *. You should see the *
expanded into the list of matching files.
After hitting Return those names will be added to the ARGV array. OptionParser will walk through ARGV and find the flags you defined, grab the following elements if necessary, then remove them from ARGV. When OptionParser is finished any ARGV elements that didn't fit into the options will remain in the ARGV array where you can get at them.
In your code, you are looking for a single parameter for the '-a'
or '--add FILE'
option. OptionParser() has an Array
option which will grab comma-separated elements from the command line but will subsequent space-delimited ones.
require 'optparse'
options = []
opts = OptionParser.new
opts.on('-a', '--add FILE', Array) do |s|
options << s
end.parse!
print "options => ", options.join(', '), "\n"
print "ARGV => ", ARGV.join(', '), "\n"
Save that to a file and try your command line with -a one two three
, then with -a one,two,three
. You'll see how the Array
option grabs the elements differently depending on whether there are commas or spaces between the parameters.
Because the *
wildcard gets replaced with space delimited filenames you'll have to post-process ARGV after OptionParser has run against it, or programmatically glob the directory and build the list that way. ARGV has all the files except the one picked up in the -a
option so, personally, I'd drop the -a
option and let ARGV contain all the files.
You will have to glob the directory if *
has to too many files and exceeds the buffer size. You'll know if that happens because the OS will complain.