Class: OptimistXL::ShortNames

Inherits:
Object
  • Object
show all
Defined in:
lib/optimist_xl.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.



829
830
831
832
# File 'lib/optimist_xl.rb', line 829

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

Instance Attribute Details

#autoObject (readonly)

Returns the value of attribute auto.



834
835
836
# File 'lib/optimist_xl.rb', line 834

def auto
  @auto
end

#charsObject (readonly)

Returns the value of attribute chars.



834
835
836
# File 'lib/optimist_xl.rb', line 834

def chars
  @chars
end

Instance Method Details

#add(values) ⇒ Object



836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
# File 'lib/optimist_xl.rb', line 836

def add(values)
  values = [values] unless values.is_a?(Array) # box the value
  values.compact.each do |val|
    if val == :none
      @auto = false
      raise "Cannot set short to :none if short-chars have been defined '#{@chars}'" unless chars.empty?
      next
    end
    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