Class: Docopt::Option

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parse) ⇒ Option



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/docopt.rb', line 11

def initialize parse
  @argcount                = 0
  options, _, description = parse.strip.partition('  ')
  options                 = options.sub(',', ' ').sub('=', ' ')

  for s in options.split
    if s.start_with? '--'
      @long = s
    elsif s.start_with? '-'
      @short = s
    else
      @argcount = 1
    end
  end
  
  if @argcount == 1
    matched = description.scan(/\[default: (.*)\]/)[0]
    @value = matched ? matched[0] : nil
  end
end

Instance Attribute Details

#argcountObject (readonly)

Returns the value of attribute argcount.



9
10
11
# File 'lib/docopt.rb', line 9

def argcount
  @argcount
end

#longObject (readonly)

Returns the value of attribute long.



9
10
11
# File 'lib/docopt.rb', line 9

def long
  @long
end

#shortObject (readonly)

Returns the value of attribute short.



9
10
11
# File 'lib/docopt.rb', line 9

def short
  @short
end

#valueObject (readonly)

Returns the value of attribute value.



9
10
11
# File 'lib/docopt.rb', line 9

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object



58
59
60
# File 'lib/docopt.rb', line 58

def == other
  self.inspect == other.inspect
end

#getoptObject



50
51
52
# File 'lib/docopt.rb', line 50

def getopt
  [long, short, argcount].compact
end

#inspectObject



54
55
56
# File 'lib/docopt.rb', line 54

def inspect
  "#<Docopt::Option short: #{short}, long: #{long}, argcount: #{argcount}, value: #{value}>"
end

#set_value(val) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/docopt.rb', line 32

def set_value val
  if argcount.zero?
    @value = true
  else
    @value = val
  end
end

#symbolsObject



44
45
46
47
48
# File 'lib/docopt.rb', line 44

def symbols
  [short, long].compact.map do |name|
    name.gsub(/^-+/, '').to_sym
  end
end

#synonymsObject



40
41
42
# File 'lib/docopt.rb', line 40

def synonyms
  ([short, long] + symbols).compact
end