class VV::OptionRouter

Attributes

flag_settings[R]
help[RW]
name[R]
testing[RW]
version[RW]

Public Class Methods

new(name: nil) { |self| ... } click to toggle source
# 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

Public Instance Methods

add_input_arg(arg) click to toggle source
# File lib/vv/cli.rb, line 378
def add_input_arg arg
  @parsed_arguments[:input_arguments] ||= []
  @parsed_arguments[:input_arguments] << arg
end
cease_flag_consideration() click to toggle source
# 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) click to toggle source
# File lib/vv/cli.rb, line 285
def create_flag flag, type: nil
  raise NotImplementedError
end
decimal?() click to toggle source
# File lib/vv/cli.rb, line 415
def decimal?
  flag_type == :decimal
end
end_of_commands() click to toggle source
# File lib/vv/cli.rb, line 277
def end_of_commands
  "--"
end
ensure_known_flag!() click to toggle source
# 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_type() click to toggle source
# File lib/vv/cli.rb, line 411
def flag_type
  @flag_settings[@flag][:type]
end
handle_command() click to toggle source
# 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_current() click to toggle source
# 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_flags() click to toggle source
# 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_doc() click to toggle source
# 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?() click to toggle source
# File lib/vv/cli.rb, line 394
def long_flag?
  @flag.starts_with? 2.dashes
end
lookup(flag) click to toggle source
# File lib/vv/cli.rb, line 183
def lookup flag
  flag = lookup_canonical_flag flag
  @parsed_arguments[flag]
end
lookup_canonical_flag(flag) click to toggle source
# File lib/vv/cli.rb, line 317
def lookup_canonical_flag flag
  @flag_settings.lookup_canonical flag
end
parse(argv) click to toggle source

This needs a refactor.

# 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) { || ... } click to toggle source
# 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) click to toggle source
# File lib/vv/cli.rb, line 281
def reserve flags
  register flags, type: :reserved
end
set_command(command, alias_flag: nil) click to toggle source
# 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_flag() click to toggle source
# 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_flag() click to toggle source
# 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_commands() click to toggle source
# 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_flags() click to toggle source
# 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_value() click to toggle source
# 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?() click to toggle source
# File lib/vv/cli.rb, line 389
def short_flag?
  return false if long_flag?
  @flag.starts_with? String.dash
end
standardize_value() click to toggle source
# 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?() click to toggle source
# File lib/vv/cli.rb, line 334
def termination_flag?
  @flag == "--"
end