"Massimiliano Mirra - bard" <mmirra / libero.REMOVETHIS.it> schrieb im Newsbeitrag news:87ekjcrnwy.fsf / prism.localnet... > > I have a command line program invoked like this: > > $ prog --host localhost --port 8000 login --password secret > $ prog --host localhost --port 8000 list --open > > ...and so on. In other words: > > $ command [general options] subcommand [specific options] > > Like darcs or cvs. > > So far I've been doing fine with getoptlong: > > while ARGV.first =~ /^-/ > opt, arg = opts.get > case opt > when "--host" > config["host"] = arg > when "--port" > config["port"] = arg.to_i > end > end I don't think the code above is proper GetoptLong code. I don't remember exactly but the occurrence of ARGV in the loop makes me suspicious. > When the loop sees the first thing that does not begin with `-' > (option arguments don't count, they get eaten by opts.get), it assumes > it to be the subcommand, and leaves ARGV alone from that point on. > ARGV is later emptied by the subcommand specific options parsing. > > I'd like to migrate to optparse, though. But how can I stop argument > parsing at a certain point in optparse? Command line option parsing libs normally do that for you as long as you don't have any special requirements. If you need to stop parsing options after a special option #terminate might do what you need. There's a quite complete example at the optparse rdoc page: http://www.ruby-doc.org/stdlib/libdoc/optparse/rdoc/classes/OptionParser.html Reconsidering your situation this is what I'd do: iterative parse all remaining options starting with the full set at the beginning. Evaluate the first non option, this is your next command. Eat it and go to the first step only now remembering that options are stored for this command. Repeat until there are no more args left or the first non option is not a command (i.e. file name, ignorable or whatever). Kind regards robert