From: Thomas Leitner Date: 2004-11-04T05:38:46+09:00 Subject: Re: From getoptlong to optparse On Tue, 02 Nov 2004 20:33:15 GMT Massimiliano Mirra - bard wrote: | | 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 | | 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? | | Massimiliano You may want to take a look at cmdparse.rubyforge.org. It is based on optparse, but provides the semantics of subcommands as you called them. Your command line $ command [general options] subcommand [specific options] is a prime example for the use of it. Have a look at the example at this page http://cmdparse.rubyforge.org/rdoc/classes/CommandParser.html *hth*, Thomas Leitner