Hi,

At Wed, 31 Jul 2002 15:13:19 +0900,
Patrick Bennett wrote:
> Sorry, that won't work.
> Even with REQUIRE_ORDER GetoptLong still eats the first non-option it
> hits so further processing won't see that argument.

AFAIK, GetoptLong never eat non-option arguments regardless
ordering mode.

What about this?

require 'getoptlong'

$suboptions = {}
class Foo
  attr_reader :foo
  def parse_option(opt, arg)
    case opt
    when "--foo"
      @foo = arg
    end
  end
  $suboptions['foo'] = [
    self,
    GetoptLong.new(["--foo", GetoptLong::REQUIRED_ARGUMENT])
  ]
end

class Bar
  attr_reader :bar
  def parse_option(opt, arg)
    case opt
    when "--bar"
      @bar = arg
    end
  end
  $suboptions['bar'] = [
    self,
    GetoptLong.new(["--bar", GetoptLong::OPTIONAL_ARGUMENT]),
  ]
end

$logfile = nil
opts = GetoptLong.new(["-l", "--logfile", GetoptLong::NO_ARGUMENT],
		      [ "-c", GetoptLong::NO_ARGUMENT])
opts.ordering = GetoptLong::REQUIRE_ORDER
opts.each do |opt, arg|
  case opt
  when "-c"
    opts.terminate
  when "-l"
    $logfile = true
  end
end
raise "missing command" if ARGV.empty?
command, opts = $suboptions[ARGV[0]]
command or raise "unknown command: #{ARGV[0]}"
ARGV.shift
cmd = command.new
opts.each(&cmd.method(:parse_option))
p :$logfile => $logfile, :cmd => cmd


> That, plus, the ordering of the arguments isn't always going to be
> conveniently in the 'right order.'

Don't you want to separate arguments by the order, before
subcommand and after?

-- 
Nobu Nakada