Is it possible to process a list of files using Ruby's OptionParser, or does ARGV have to be parsed separately?
I am trying to parse command line parameters for a Ruby script running in Linux. Currently my script is invoked using a style like: ./run.rb /dir/* and I parse ARGV to get the list of files that it operates on. However, now I have a couple of actions that I am switching between, so rather than edit the script I want to add a command line option. So the script would be invoked as ./run.rb -a action -i /dir/*.
When I was trying to get this working I ran into problems with only getting the first file that is in the directory. "Parsing command-line arguments as wildcards" explains that the reason is that the shell is expanding the list of files and that when OptionParser processes an Array it looks for comma separated values. The answer also says:
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.
Is this correct that OptionParser doesn't have a way to processes space separated values as would be returned by shell expansion? I haven't found anything so I'm think my interpretation is correct. But I also think this would be a commonly used feature so I'm surprised that this is the case, hence the reason for this question.