Class: Flow

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Flow

Returns a new instance of Flow.



124
125
126
# File 'lib/args.rb', line 124

def initialize
  @cells = [ ]
end

Instance Attribute Details

#usage_string ⇒ Object

Returns the value of attribute usage_string.



122
123
124
# File 'lib/args.rb', line 122

def usage_string
  @usage_string
end

Instance Method Details

#add(*cells) ⇒ Object



128
129
130
131
132
# File 'lib/args.rb', line 128

def add(*cells)
  cells.each { |cell|
    @cells << cell
  }
end

#add_errline(str, error) ⇒ Object



221
222
223
224
225
226
227
# File 'lib/args.rb', line 221

def add_errline(str, error)
  if str
    str + "\n" + error
  else
    error
  end
end

#arghash ⇒ Object



202
203
204
205
206
207
208
209
# File 'lib/args.rb', line 202

def arghash
  result = { }
  @cells.each { |cell|
    result[cell.name] = cell.value
  }
  result["command"] = command
  result
end

#command ⇒ Object



196
197
198
199
200
# File 'lib/args.rb', line 196

def command
  if @cells[-1]
    @cells[-1].command
  end
end

#missing_args ⇒ Object



177
178
179
180
181
182
183
184
185
# File 'lib/args.rb', line 177

def missing_args
  missing = [ ]
  @cells.each { |cell|
    if cell.missing_value
      missing << cell.name
    end
  }
  missing
end

#parse(args, obj = nil) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/args.rb', line 134

def parse(args, obj = nil)
  @target = obj
  rest = [ ]
  args.each_with_index { |arg, index|
    name, value = Util.parse_line(arg)
    if value.nil?
      value = name
      error, finished = setvalue(value)
    else
      error, finished = set(name, value)
    end
    if error
      result = add_errline(result, error)
      finished = :finished
    end
    if finished
      rest = args[index..-1]
      break
    end
  }
  missing = missing_args
  unless missing.empty?
    result = add_errline(result, "missing args: #{missing}")
  end
  # dputs "parse for class #{obj.class} result=#{result}, rest=#{rest}"
  return result, rest
end

#set(name, value) ⇒ Object



187
188
189
190
191
192
193
194
# File 'lib/args.rb', line 187

def set(name, value)
  @cells.each { |cell|
    if cell.name == name
      return cell.set(value, @target), nil
    end
  }
  return "argument named #{name} not found", nil
end

#setvalue(value) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/args.rb', line 162

def setvalue(value)
  @cells.each { |cell|
    if cell.full?
      next
    else
      error = cell.set(value, @target)
      if error && cell.repeatable
        next
      end
      return error, nil
    end
  }
  return nil, :finished
end

#usage ⇒ Object



211
212
213
214
215
216
217
218
219
# File 'lib/args.rb', line 211

def usage
  names = [ ]
  desc = [ ]
  @cells.each { |cell|
    names << cell.name
    desc << cell.desc
  }
  return names, desc
end