Class: Optimist::ShortNames

Inherits:
Object
  • Object
show all
Defined in:
lib/optimist.rb

Constant Summary collapse

INVALID_ARG_REGEX =

:nodoc:

/[\d-]/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeShortNames

Returns a new instance of ShortNames.



731
732
733
734
# File 'lib/optimist.rb', line 731

def initialize
  @chars = []
  @auto = true
end

Instance Attribute Details

#autoObject (readonly)

Returns the value of attribute auto.



736
737
738
# File 'lib/optimist.rb', line 736

def auto
  @auto
end

#charsObject (readonly)

Returns the value of attribute chars.



736
737
738
# File 'lib/optimist.rb', line 736

def chars
  @chars
end

Instance Method Details

#add(values) ⇒ Object



738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
# File 'lib/optimist.rb', line 738

def add(values)
  values = [values] unless values.is_a?(Array) # box the value
  values = values.compact
  if values.include?(:none)
    if values.size == 1
      @auto = false
      return
    end
    raise ArgumentError, "Cannot use :none with any other values in short option: #{values.inspect}"
  end
  values.each do |val|
    strval = val.to_s
    sopt = case strval
           when /^-(.)$/ then $1
           when /^.$/ then strval
           else raise ArgumentError, "invalid short option name '#{val.inspect}'"
           end

    if sopt =~ INVALID_ARG_REGEX
      raise ArgumentError, "short option name '#{sopt}' can't be a number or a dash" 
    end
    @chars << sopt
  end
end