Class: OptparseLite::OptSpec

Inherits:
Struct
  • Object
show all
Extended by:
Lingual
Defined in:
lib/optparse-lite.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Lingual

methodize, oxford_comma

Instance Attribute Details

#accessorObject

Returns the value of attribute accessor

Returns:

  • (Object)

    the current value of accessor



866
867
868
# File 'lib/optparse-lite.rb', line 866

def accessor
  @accessor
end

#arg_nameObject

Returns the value of attribute arg_name

Returns:

  • (Object)

    the current value of arg_name



866
867
868
# File 'lib/optparse-lite.rb', line 866

def arg_name
  @arg_name
end

#defaultObject

Returns the value of attribute default

Returns:

  • (Object)

    the current value of default



866
867
868
# File 'lib/optparse-lite.rb', line 866

def default
  @default
end

#descObject

Returns the value of attribute desc

Returns:

  • (Object)

    the current value of desc



866
867
868
# File 'lib/optparse-lite.rb', line 866

def desc
  @desc
end

#longObject

Returns the value of attribute long

Returns:

  • (Object)

    the current value of long



866
867
868
# File 'lib/optparse-lite.rb', line 866

def long
  @long
end

#namesObject

Returns the value of attribute names

Returns:

  • (Object)

    the current value of names



866
867
868
# File 'lib/optparse-lite.rb', line 866

def names
  @names
end

#noableObject

Returns the value of attribute noable

Returns:

  • (Object)

    the current value of noable



866
867
868
# File 'lib/optparse-lite.rb', line 866

def noable
  @noable
end

#optionalObject Also known as: optional?

Returns the value of attribute optional

Returns:

  • (Object)

    the current value of optional



866
867
868
# File 'lib/optparse-lite.rb', line 866

def optional
  @optional
end

#requiredObject Also known as: required?

Returns the value of attribute required

Returns:

  • (Object)

    the current value of required



866
867
868
# File 'lib/optparse-lite.rb', line 866

def required
  @required
end

#shortObject

Returns the value of attribute short

Returns:

  • (Object)

    the current value of short



866
867
868
# File 'lib/optparse-lite.rb', line 866

def short
  @short
end

#takes_argumentObject Also known as: takes_argument?

Returns the value of attribute takes_argument

Returns:

  • (Object)

    the current value of takes_argument



866
867
868
# File 'lib/optparse-lite.rb', line 866

def takes_argument
  @takes_argument
end

Class Method Details

.parse(str) ⇒ Object



886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
# File 'lib/optparse-lite.rb', line 886

def parse str
  names, reqs, opts, short, long, noable, caps = [],[],[],[],[], nil,nil
  str.split(/, */).each do |syn|
    failed(str.inspect) unless caps = @short_long.parse(syn)
    names.push(caps.short || caps.long)
    long.push "--#{caps.long}" if caps.long
    short.push "-#{caps.short}" if caps.short
    if caps.no
      failed("i dunno can u say no multiple times?") if noable
      noable = caps.no
      this = "#{caps.no}#{caps.long}"
      long.push "--#{this}"
      names.push this
    end
    if caps = @param.parse(syn)
      (caps.required ? reqs : opts).push(caps.required || caps.optional)
    end
    failed("don't know how to parse: #{syn.inspect}") unless syn.empty?
  end
  failed("can't have both required and optional arguments: "<<
    str.inspect) if reqs.any? && opts.any?
  arg_names = opts | reqs
  failed("let's not take arguments with no- style opts") if
    noable && arg_names.any?
  failed("spell the argument the same way each time: "<<
    oxford_comma(arg_names)) if arg_names.length > 1
  new(names, opts.any? || reqs.any?,
    reqs.any?, opts.any?, arg_names.first, short, long, noable)
end

Instance Method Details

#cannonical_nameObject

class << self



920
921
922
# File 'lib/optparse-lite.rb', line 920

def cannonical_name
  syntax_tokens.last
end

#doc_sexpObject



923
924
925
# File 'lib/optparse-lite.rb', line 923

def doc_sexp
  [[:opt, syntax_tokens*', ', * desc]]
end

#has_default?Boolean

Returns:

  • (Boolean)


926
927
928
# File 'lib/optparse-lite.rb', line 926

def has_default?
  ! default.nil? # whatever. i don't care about nil defaults
end

#normalized_keyObject



929
930
931
# File 'lib/optparse-lite.rb', line 929

def normalized_key
  accessor ? accessor.to_sym : names.last.to_sym
end

#syntax_tokensObject



932
933
934
935
936
937
938
939
940
# File 'lib/optparse-lite.rb', line 932

def syntax_tokens
  if noable
    ["--[#{noable}]#{names.first}"]
  else
    these = long + short
    these[these.length-1] = "#{these.last}#{arg_name}"
    these
  end
end