Class: Docopt::Option

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parse) ⇒ Option

Returns a new instance of Option.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/pwqgen/docopt.rb', line 31

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.



29
30
31
# File 'lib/pwqgen/docopt.rb', line 29

def argcount
  @argcount
end

#longObject (readonly)

Returns the value of attribute long.



29
30
31
# File 'lib/pwqgen/docopt.rb', line 29

def long
  @long
end

#shortObject (readonly)

Returns the value of attribute short.



29
30
31
# File 'lib/pwqgen/docopt.rb', line 29

def short
  @short
end

#valueObject (readonly)

Returns the value of attribute value.



29
30
31
# File 'lib/pwqgen/docopt.rb', line 29

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object



78
79
80
# File 'lib/pwqgen/docopt.rb', line 78

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

#getoptObject



70
71
72
# File 'lib/pwqgen/docopt.rb', line 70

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

#inspectObject



74
75
76
# File 'lib/pwqgen/docopt.rb', line 74

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

#set_value(val) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/pwqgen/docopt.rb', line 52

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

#symbolsObject



64
65
66
67
68
# File 'lib/pwqgen/docopt.rb', line 64

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

#synonymsObject



60
61
62
# File 'lib/pwqgen/docopt.rb', line 60

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