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:



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/vv/cli.rb', line 142

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.



138
139
140
# File 'lib/vv/cli.rb', line 138

def flag_settings
  @flag_settings
end

#helpObject

Returns the value of attribute help.



140
141
142
# File 'lib/vv/cli.rb', line 140

def help
  @help
end

#nameObject (readonly)

Returns the value of attribute name.



138
139
140
# File 'lib/vv/cli.rb', line 138

def name
  @name
end

#testingObject

Returns the value of attribute testing.



140
141
142
# File 'lib/vv/cli.rb', line 140

def testing
  @testing
end

#versionObject

Returns the value of attribute version.



140
141
142
# File 'lib/vv/cli.rb', line 140

def version
  @version
end

Instance Method Details

#add_input_arg(arg) ⇒ Object



346
347
348
349
# File 'lib/vv/cli.rb', line 346

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

#cease_flag_considerationObject



371
372
373
374
375
376
377
# File 'lib/vv/cli.rb', line 371

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)


253
254
255
# File 'lib/vv/cli.rb', line 253

def create_flag flag, type: nil
  raise NotImplementedError
end

#decimal?Boolean

Returns:

  • (Boolean)


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

def decimal?
  flag_type == :decimal
end

#end_of_commandsObject



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

def end_of_commands
  "--"
end

#ensure_known_flag!Object



366
367
368
369
# File 'lib/vv/cli.rb', line 366

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

#flag_typeObject



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

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

#handle_commandObject



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

def handle_command
  command = @commands[@flag]

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

#handle_currentObject



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/vv/cli.rb', line 329

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



317
318
319
320
321
322
323
324
325
326
327
# File 'lib/vv/cli.rb', line 317

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



289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/vv/cli.rb', line 289

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)


362
363
364
# File 'lib/vv/cli.rb', line 362

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

#lookup_canonical_flag(flag) ⇒ Object



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

def lookup_canonical_flag flag
  @flag_settings.lookup_canonical flag
end

#parse(argv) ⇒ Object

This needs a refactor.



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/vv/cli.rb', line 218

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



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/vv/cli.rb', line 156

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



249
250
251
# File 'lib/vv/cli.rb', line 249

def reserve flags
  register flags, type: :reserved
end

#set_command(command, alias_flag: nil) ⇒ Object



276
277
278
279
280
281
282
283
# File 'lib/vv/cli.rb', line 276

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



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

def set_flag
  @flag = lookup_canonical_flag @flag

  @current_flag = @flag if @value.nil?

  self.set_value
end

#set_new_flagObject



192
193
194
195
196
197
198
199
200
# File 'lib/vv/cli.rb', line 192

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



257
258
259
260
# File 'lib/vv/cli.rb', line 257

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

#set_reserved_flagsObject



262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/vv/cli.rb', line 262

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



210
211
212
213
214
215
# File 'lib/vv/cli.rb', line 210

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

#short_flag?Boolean

Returns:

  • (Boolean)


357
358
359
360
# File 'lib/vv/cli.rb', line 357

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

#standardize_valueObject



351
352
353
354
355
# File 'lib/vv/cli.rb', line 351

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)


302
303
304
# File 'lib/vv/cli.rb', line 302

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