Class: VV::OptionRouter

Inherits:
Object show all
Defined in:
lib/vv/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil) {|_self| ... } ⇒ OptionRouter

Returns a new instance of OptionRouter.

Yields:

  • (_self)

Yield Parameters:



169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/vv/cli.rb', line 169

def initialize name: nil
  @flag_settings = LookupTable.new
  @commands = Hash.new
  @current_flag = nil

  @name   = name
  @name ||= "check".style :lightblue, :italic

  self.set_reserved_flags
  self.set_reserved_commands

  yield self if block_given?
end

Instance Attribute Details

#flag_settingsObject (readonly)

Returns the value of attribute flag_settings.



165
166
167
# File 'lib/vv/cli.rb', line 165

def flag_settings
  @flag_settings
end

#helpObject

Returns the value of attribute help.



167
168
169
# File 'lib/vv/cli.rb', line 167

def help
  @help
end

#nameObject (readonly)

Returns the value of attribute name.



165
166
167
# File 'lib/vv/cli.rb', line 165

def name
  @name
end

#testingObject

Returns the value of attribute testing.



167
168
169
# File 'lib/vv/cli.rb', line 167

def testing
  @testing
end

#versionObject

Returns the value of attribute version.



167
168
169
# File 'lib/vv/cli.rb', line 167

def version
  @version
end

Instance Method Details

#add_input_arg(arg) ⇒ Object



378
379
380
381
# File 'lib/vv/cli.rb', line 378

def add_input_arg arg
  @parsed_arguments[:input_arguments] ||= []
  @parsed_arguments[:input_arguments] << arg
end

#cease_flag_considerationObject



403
404
405
406
407
408
409
# File 'lib/vv/cli.rb', line 403

def cease_flag_consideration
  @flags_cease  = true
  @current_flag = nil
  @flag.concat String.equals_sign, @value if @value
  add_input_arg @flag
  @flag = @value = nil
end

#create_flag(flag, type: nil) ⇒ Object

Raises:

  • (NotImplementedError)


285
286
287
# File 'lib/vv/cli.rb', line 285

def create_flag flag, type: nil
  raise NotImplementedError
end

#decimal?Boolean

Returns:

  • (Boolean)


415
416
417
# File 'lib/vv/cli.rb', line 415

def decimal?
  flag_type == :decimal
end

#end_of_commandsObject



277
278
279
# File 'lib/vv/cli.rb', line 277

def end_of_commands
  "--"
end

#ensure_known_flag!Object



398
399
400
401
# File 'lib/vv/cli.rb', line 398

def ensure_known_flag!
  message = "Unknown flag `#{@flag}` provided."
  fail message if @value.present? or self.long_flag?
end

#flag_typeObject



411
412
413
# File 'lib/vv/cli.rb', line 411

def flag_type
  @flag_settings[@flag][:type]
end

#handle_commandObject



338
339
340
341
342
343
344
345
346
347
# File 'lib/vv/cli.rb', line 338

def handle_command
  command = @commands[@flag]

  if command.first == :alias_flag
    @flag = command.second
    self.set_flag
  else
    raise NotImplementedError
  end
end

#handle_currentObject



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/vv/cli.rb', line 361

def handle_current

  @value = @flag
  @flag  = @current_flag

  collection = \
  @flag_settings[@flag][:type] == :collection
  if collection
    @parsed_arguments[@flag] ||= []
    @parsed_arguments[@flag] << @value
  else
    @current_flag = nil
    self.set_flag
  end

end

#handle_short_flagsObject



349
350
351
352
353
354
355
356
357
358
359
# File 'lib/vv/cli.rb', line 349

def handle_short_flags
  short_flags = \
  @flag.after(String.dash).split String.empty_string

  duplicates = \
  short_flags.count != short_flags.uniq.count

  message = \
  "Duplicate command line flags in #{@flag}."
  fail message if duplicates
end

#help_docObject



321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/vv/cli.rb', line 321

def help_doc
  ending_flag = %w[ -- ]
  keys  = @flag_settings.canonical_keys - ending_flag
  keys += ending_flag

  cli_flags = keys.map do |key|
    flags = [ key ] + @flag_settings.aliases[key].to_a
    "[#{flags.join(" | ")}]"
  end

  { "usage: #{@name}" => cli_flags }
end

#long_flag?Boolean

Returns:

  • (Boolean)


394
395
396
# File 'lib/vv/cli.rb', line 394

def long_flag?
  @flag.starts_with? 2.dashes
end

#lookup(flag) ⇒ Object



183
184
185
186
# File 'lib/vv/cli.rb', line 183

def lookup flag
  flag = lookup_canonical_flag flag
  @parsed_arguments[flag]
end

#lookup_canonical_flag(flag) ⇒ Object



317
318
319
# File 'lib/vv/cli.rb', line 317

def lookup_canonical_flag flag
  @flag_settings.lookup_canonical flag
end

#parse(argv) ⇒ Object

This needs a refactor.



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/vv/cli.rb', line 250

def parse argv
  @flags_cease = false
  @parsed_arguments = {}

  argv.each do |arg|
    next add_input_arg arg if @flags_cease

    @flag, *@value = arg.split String.equals_sign
    self.standardize_value

    next @flags_cease = true if self.termination_flag?
    next handle_command if @commands.include? @flag
    next set_flag  if @flag_settings.include? @flag

    self.ensure_known_flag!

    next handle_short_flags if self.short_flag?
    next handle_current     if @current_flag.present?

    self.cease_flag_consideration
  end

  @flag = @value = @current_flag = nil

  @parsed_arguments
end

#register(flags, type: :string) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/vv/cli.rb', line 188

def register flags, type: :string
  flags = [ flags.to_s ] unless flags.is_a? Array
  type.one_of! :string,
               :integer,
               :decimal,
               :float,
               :boolean,
               :trilean,
               :ternary,
               :reserved

  help = block_given? ? yield.squish : nil

  first_flag = flags.first

  flags.each do |flag|
    if @flag_settings[flag].blank?
      next if flag == first_flag
      @flag_settings.alias key: flag, to: first_flag
      next
    end

    set_type = @flag_settings[flag][:type]
    type_ok = set_type != :reserved

    message = "Duplicate flag `#{flag}` cannot be set."
    fail message if type_ok
    fail "Reserved flag `#{flag}` cannot be set."
  end

  settings = { type: type }
  settings[:help] = help unless help.blank?

  @flag_settings[first_flag] = settings
end

#reserve(flags) ⇒ Object



281
282
283
# File 'lib/vv/cli.rb', line 281

def reserve flags
  register flags, type: :reserved
end

#set_command(command, alias_flag: nil) ⇒ Object



308
309
310
311
312
313
314
315
# File 'lib/vv/cli.rb', line 308

def set_command command, alias_flag: nil
  command = command.to_s
  if @commands.include? command
    raise "Command #{command} already set."
  end

  @commands[command] = [ :alias_flag, alias_flag ]
end

#set_flagObject



234
235
236
237
238
239
240
# File 'lib/vv/cli.rb', line 234

def set_flag
  @flag = lookup_canonical_flag @flag

  @current_flag = @flag if @value.nil?

  self.set_value
end

#set_new_flagObject



224
225
226
227
228
229
230
231
232
# File 'lib/vv/cli.rb', line 224

def set_new_flag
  message = \
  "Duplicate command line flag #{@flag} encountered."

  @flag = lookup_canonical_flag @flag
  fail message if @parsed_arguments.include? @flag

  self.set_flag
end

#set_reserved_commandsObject



289
290
291
292
# File 'lib/vv/cli.rb', line 289

def set_reserved_commands
  set_command :help,    alias_flag: "-h"
  set_command :version, alias_flag: "-V"
end

#set_reserved_flagsObject



294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/vv/cli.rb', line 294

def set_reserved_flags
  [ %w[ -h -?  --help              ],
    %w[ -V     --version           ],
    %w[ -v     --verbose           ],
    %w[ -vv    --very-verbose      ],
    %w[ -vvv   --very-very-verbose ],
    %w[ -q     --quiet             ],
    %w[ -s -qq --absolute-silence  ],
    %w[ -- ] ].each do |flags|

    self.register flags, type: :reserved
  end
end

#set_valueObject



242
243
244
245
246
247
# File 'lib/vv/cli.rb', line 242

def set_value
  value   = @value
  value &&= @value.to_d if decimal?
  value ||= true
  @parsed_arguments[@flag] = value
end

#short_flag?Boolean

Returns:

  • (Boolean)


389
390
391
392
# File 'lib/vv/cli.rb', line 389

def short_flag?
  return false if long_flag?
  @flag.starts_with? String.dash
end

#standardize_valueObject



383
384
385
386
387
# File 'lib/vv/cli.rb', line 383

def standardize_value
  present = @value.present?
  @value = @value.join( String.equals_sign ) if present
  @value = nil unless @value.present?
end

#termination_flag?Boolean

Returns:

  • (Boolean)


334
335
336
# File 'lib/vv/cli.rb', line 334

def termination_flag?
  @flag == "--"
end