Class: Docopt::Pattern

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

Direct Known Subclasses

ChildPattern, ParentPattern

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#childrenObject

Returns the value of attribute children


27
28
29
# File 'lib/docopt.rb', line 27

def children
  @children
end

Instance Method Details

#==(other) ⇒ Object


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

def ==(other)
  return self.inspect == other.inspect
end

#dumpObject


37
38
39
# File 'lib/docopt.rb', line 37

def dump
  puts ::Docopt::dump_patterns(self)
end

#eitherObject


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/docopt.rb', line 84

def either
  ret = []
  groups = [[self]]
  while groups.count > 0
    children = groups.shift
    types = children.map { |child| child.class }

    if types.include?(Either)
      either = children.select { |child| child.class == Either }[0]
      children.slice!(children.index(either))
      for c in either.children
        groups << [c] + children
      end
    elsif types.include?(Required)
      required = children.select { |child| child.class == Required }[0]
      children.slice!(children.index(required))
      groups << required.children + children

    elsif types.include?(Optional)
      optional = children.select { |child| child.class == Optional }[0]
      children.slice!(children.index(optional))
      groups << optional.children + children

    elsif types.include?(AnyOptions)
      anyoptions = children.select { |child| child.class == AnyOptions }[0]
      children.slice!(children.index(anyoptions))
      groups << anyoptions.children + children

    elsif types.include?(OneOrMore)
      oneormore = children.select { |child| child.class == OneOrMore }[0]
      children.slice!(children.index(oneormore))
      groups << (oneormore.children * 2) + children

    else
      ret << children
    end
  end

  args = ret.map { |e| Required.new(*e) }
  return Either.new(*args)
end

#fixObject


41
42
43
44
45
# File 'lib/docopt.rb', line 41

def fix
  fix_identities
  fix_repeating_arguments
  return self
end

#fix_identities(uniq = nil) ⇒ Object


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/docopt.rb', line 47

def fix_identities(uniq=nil)
  if not instance_variable_defined?(:@children)
    return self
  end
  uniq ||= flat.uniq

  @children.each_with_index do |c, i|
    if not c.instance_variable_defined?(:@children)
      if !uniq.include?(c)
        raise RuntimeError
      end
      @children[i] = uniq[uniq.index(c)]
    else
      c.fix_identities(uniq)
    end
  end
end

#fix_repeating_argumentsObject


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/docopt.rb', line 65

def fix_repeating_arguments
  either.children.map { |c| c.children }.each do |case_|
    case_.select { |c| case_.count(c) > 1 }.each do |e|
      if e.class == Argument or (e.class == Option and e.argcount > 0)
        if e.value == nil
          e.value = []
        elsif e.value.class != Array
          e.value = e.value.split
        end
      end
      if e.class == Command or (e.class == Option and e.argcount == 0)
        e.value = 0
      end
    end
  end

  return self
end

#to_strObject


33
34
35
# File 'lib/docopt.rb', line 33

def to_str
  return self.inspect
end