class Fuelcell::Parser::OptHandler

Process options that are flags or use another arg as their value. This handles both short and long options. Flags are assigned a value of true while all other values are casted during value assignment

Public Instance Methods

call(cmd, args, opts) click to toggle source

@param cmd [Fuelcell::Command] @param args [Array] raw args from ARGV @param opts [Hash] stores the found opts @return [Boolean]

# File lib/fuelcell/parser/opt_handler.rb, line 11
def call(cmd, args, opts)
  arg = take_first_arg(args) do |text|
    !(text =~ /^(--\w+(?:-\w+)*|-[a-zA-Z])$/).nil?
  end
  return false unless arg

  opt = find_opt(cmd, arg)
  return true if handled_flag?(opts, opt)

  # We know we are not a flag, so we are expecting a value.
  # We also know there are no other values available so for now
  # this is considered a failure condition
  fail_value_required(opt) if args.empty?

  handle_value(opts, opt, arg, args)
  true
end

Private Instance Methods

fail_value_required(opt) click to toggle source
# File lib/fuelcell/parser/opt_handler.rb, line 50
def fail_value_required(opt)
  return if opt.callable? || opt.cmd_path?

  cli_opts = opt.cli_labels.join(' or ')
  fail "value not found for #{cli_opts} when value is required"
end
handle_value(opts, opt, arg, args) click to toggle source
# File lib/fuelcell/parser/opt_handler.rb, line 31
def handle_value(opts, opt, arg, args)
  value = args.shift
  unless arg?(value)
    if opt.default?
      value = opt.default
    else
      fail_value_required(opt)
    end
  end

  assign_opt_value(opts, opt, value, arg)
end
handled_flag?(opts, opt) click to toggle source
# File lib/fuelcell/parser/opt_handler.rb, line 44
def handled_flag?(opts, opt)
  return false unless opt.flag?
  found_opt_flag(opts, opt)
  true
end