class Rootage::Option

Option is an option item for PIONE command.

Public Instance Methods

define_validator(&b) click to toggle source
# File lib/rootage/option.rb, line 38
def define_validator(&b)
  self.validator = b
end
setup(opt, cmd) click to toggle source

Setup an OptionParser’s option by this item.

@param opt [OptionParser]

option parser

@param cmd [Command::PlainCommand]

command object

@return [void]

# File lib/rootage/option.rb, line 49
def setup(opt, cmd)
  if not(arg.nil?) and type.nil?
    raise OptionError.new(cmd, "Option type is undefined for the option " + inspect)
  end

  # build OptionParser#on arguments
  args = [short_for_optparse, long_for_optparse, desc].compact

  # call #on with the argument
  opt.on(*args) {|val| specify(cmd, val)}
end

Private Instance Methods

long_for_optparse() click to toggle source

Return log option string for optparse.rb.

# File lib/rootage/option.rb, line 69
def long_for_optparse
  (arg and not(long.nil?)) ? ("%s %s" % [long, arg]) : long
end
short_for_optparse() click to toggle source

Return short option string for optparse.rb.

# File lib/rootage/option.rb, line 64
def short_for_optparse
  (arg and long.nil?) ? ("%s %s" % [short, arg]) : short
end
specify(cmd, val) click to toggle source

Specify the option value.

@param cmd [Command::PlainCommand]

command object

@param val [Object]

option value

@return [void]

# File lib/rootage/option.rb, line 80
def specify(cmd, val)
  # set default value
  if val.nil? and not(self.default.nil?)
    val = self.default
  end

  # normalization
  if val
    val = Normalizer.normalize(type, val)
  end

  if range.nil? or range.include?(val)
    if processes.empty?
      cmd.model.specify(key, val)
    else
      execute(cmd, val)
    end
  else
    arg = {value: _val, range: range}
    raise OptionError.new(cmd, '"%{value}" is out of %{range}' % arg)
  end
end