class Opt::Option

A command line option consisting of multiple switches, possibly arguments and options about allowed numbers etc.

@api private

Attributes

block[R]

Block for processing arguments.

default[R]

Option default value.

@return [Object] Default value.

name[R]

Option's name.

@return [String] Frozen name string.

nargs[R]

Number of arguments as a range.

@return [Range] Argument number range.

options[R]

Options passed to {#initialize}.

@return [Hash] Option hash.

switches[R]

Set of switches triggering this option.

Avoid direct manipulation.

@return [Set<Switch>] Set of switches.

value[R]

Option value returned if switch is given.

Will be ignored if option takes arguments.

@return [Object] Option value.

Public Class Methods

new(definition, options = {}, &block) click to toggle source
# File lib/opt/option.rb, line 54
def initialize(definition, options = {}, &block)
  @options  = options
  @default  = options.fetch(:default, nil)
  @value    = options.fetch(:value, true)
  @nargs    = Option.parse_nargs options.fetch(:nargs, 0)
  @block    = block || Opt::Identity

  if definition.to_s =~ /\A[[:word:]]+\z/
    @switches = Set.new
    @name     = options.fetch(:name, definition).to_s.freeze

    unless nargs.min > 0 || nargs.max > 0
      raise 'A text option must consist of at least one argument.'
    end
  else
    @switches = Switch.parse(definition)
    @name     = options.fetch(:name, switches.first.name).to_s.freeze
  end
end
parse_nargs(num) click to toggle source
# File lib/opt/option.rb, line 144
def parse_nargs(num)
  case num
    when Range
      if num.min && num.max
        if num.min >= 0
          num
        else
          raise ArgumentError.new \
            'Argument number must not be less than zero.'
        end
      else
        raise ArgumentError.new \
          'Range must be ordered.'
      end
    when Numeric
      parse_nargs num..num
    else
      i = Integer(num.to_s)
      parse_nargs i..i
  end
end

Public Instance Methods

collide?(option) click to toggle source
# File lib/opt/option.rb, line 86
def collide?(option)
  name == option.name ||
  switches.any?{|s1| option.switches.any?{|s2| s1.eql?(s2) }}
end
parse!(argv, result) click to toggle source
# File lib/opt/option.rb, line 91
def parse!(argv, result)
  if text?
    parse_text!(argv, result)
  else
    parse_switches!(argv, result)
  end
end
parse_args!(argv, result) click to toggle source
# File lib/opt/option.rb, line 117
def parse_args!(argv, result)
  if nargs == (0..0)
    result[name] = value
  else
    args = []
    if argv.any? && argv.first.text?
      while argv.any? && argv.first.text? && args.size < nargs.max
        args << argv.shift.value
      end
    elsif argv.any? && argv.first.short?
      args << argv.shift.value
    end

    if nargs.include?(args.size)
      if nargs == (1..1)
        result[name] = block.call args.first
      else
        result[name] = block.call args
      end
    else
      # raise Opt::MissingArgument
      raise "wrong number of arguments (#{args.size} for #{nargs})"
    end
  end
end
parse_switches!(argv, result) click to toggle source
# File lib/opt/option.rb, line 106
def parse_switches!(argv, result)
  switches.each do |switch|
    next unless switch.match!(argv)

    parse_args!(argv, result)
    return true
  end

  false
end
parse_text!(argv, result) click to toggle source
# File lib/opt/option.rb, line 99
def parse_text!(argv, result)
  return false unless argv.first.text?

  parse_args!(argv, result)
  true
end
switch?() click to toggle source

Check if option is triggered by at least on CLI switch.

# File lib/opt/option.rb, line 76
def switch?
  switches.any?
end
text?() click to toggle source

Check if option is a free-text option.

# File lib/opt/option.rb, line 82
def text?
  !switch?
end