OptionParser : conditional option

Hi,
In OptionParser, would it be possible to have an option B depending on another option A, giving an error if option A is missing?

Prob would be easiest to just handle it in your code after option parsing. E.g. raise "Missing option A" if b && !a?

Yes, that’s what I thought too, but I was wondering if there was a trick with OptionParser.

I’m a bit curious about how you’d like to define command-line option dependencies.

If you want to handle everything only with OptionParser, would it look like this?

parser = OptionParser.new do |p|
  p.on("-a", "--alpha", "Enable alpha") do
    has_a = true
  end

  p.on("-b VALUE", "--beta=VALUE", "Beta option (requires -a)") do |v|
    unless has_a
      STDERR.puts "ERROR: --beta requires --alpha"
      STDERR.puts p
      exit(1)
    end
    value_b = v
  end

Yes, excellent suggestion.
Thanks