From: Gavin Sinclair Date: 2004-08-24T13:57:19+09:00 Subject: Re: optparse [feature request: subcommand support] On Tuesday, August 24, 2004, 7:24:19 AM, Jim wrote: > Brett Williams said: > [...] >> The feature is the ability to do what I call CVS-style subcommands, i.e.: > [...] >> Does this sound reasonable? Is there anyone else who'd like this kind of >> thing? Or am I on my own? > RubyGems uses CVS style commands, so it would have been useful there. > One question: Is it worth loading up optparse with this functionality, or > does it make sense to put it in a different object? Different class, IMO. I think the RubyGems implementation is quite good, with the Command class. Each command (admin, checkout, ...) is a subclass of Command and specifies its own optparse options. In this way optparse and Command work together nicely. In CVS there are some options shared across commands, so you can do this, for instance: module LocalRecursiveOptions def add_local_recursive_options add_option('-L', 'Local command only, no recursion') do options[:domain] = :local end add_option('-r', 'Run command on all subdirectories recursively') do options[:domain] = :recursive end end end ... class UpdateCommand < Command include LocalRecursiveOptions def initialize add_option('-P', 'Prune empty directories') do ... end ... add_local_recursive_options # <-------------- end end class LogCommand < Command include LocalRecursiveOptions def initialize add_option('-h', 'Only print header') do ... end ... add_local_recursive_options # <-------------- end end I plan to put this command framework into its own library and include it in addlib -- sometime in the next 12 months. If others are interested in this approach or any other, please go ahead. Cheers, Gavin