From: Stefano Crocco Date: 2007-12-21T05:20:33+09:00 Subject: Re: OptionParser Question --Boundary-00=_6ssaHM22JY+f3K/ Content-Type: text/plain; charset="iso-8859-6" Content-Transfer-Encoding: 7bit Alle Thursday 20 December 2007, Bryan Richardson ha scritto: > Hello all, > > I'm using OptionParser to parse command line options. I've figured out how > to handle options for each flag (i.e. opts.on('--port PORT')), but I'm not > sure how to handle command line arguments that aren't associated with a > flag. For example, say I wanted to parse a file with my file_parser > executable using a certain type of parser. I'd like to say "file_parser > --type CDF ". I know how to get the type of parser to use (see > above), but I'm not so sure how to easily get the name of the file I want > to parse. > > Any suggestions? Thanks! -- BTR Command line arguments (that is, those not associated with an option) can be obtained in a different way, depending on wheter you use OptionParser#parse or OptionParser#parse!. The former returns an array with all the arguments; the second modifies its argument, removing all the options (and their arguments) and leaving only the command line arguments. For example: require 'optparse' options= {} o = OptionParser.new do |o| o.on('--type TYPE', 'Use TYPE parser'){|t| options[:type]=t} end args = o.parse ARGV puts "The arguments are #{args.inspect}" If you want to use parse!, replace the last two lines with: o.parse! ARGV puts "The arguments are #{ARGV.inspect}" I hope this helps Stefano --Boundary-00=_6ssaHM22JY+f3K/--