class GitCompound::Command::Arguments::Parser

Class responsible for parsing ARGV for given procedure

Public Class Methods

new(argv, global) click to toggle source
# File lib/git_compound/command/arguments/parser.rb, line 7
def initialize(argv, global)
  @global = global
  @args   = format_arguments(argv)
end

Public Instance Methods

command() click to toggle source
# File lib/git_compound/command/arguments/parser.rb, line 39
def command
  @args.find { |arg| arg.is_a?(String) }
end
global() click to toggle source
# File lib/git_compound/command/arguments/parser.rb, line 35
def global
  @args & @global
end
options() click to toggle source
# File lib/git_compound/command/arguments/parser.rb, line 18
def options
  arguments = @args - @global - [command]
  found = {}

  option_each(procedure.options) do |name, type|
    option = type.new(name, arguments)
    next unless option.valid?

    arguments -= option.used
    found.merge!(option.parse)
  end

  return found if arguments.empty?
  raise UnknownArgumentError,
        "Unknown arguments `#{arguments.inspect}`"
end
procedure() click to toggle source
# File lib/git_compound/command/arguments/parser.rb, line 12
def procedure
  Command.const_get("Procedure::#{command.capitalize}")
rescue
  Procedure::Help
end

Private Instance Methods

format_arguments(argv) click to toggle source
# File lib/git_compound/command/arguments/parser.rb, line 45
def format_arguments(argv)
  argv.map do |arg|
    arg.start_with?('--') ? arg.sub(/^--/, '').tr('-', '_').to_sym : arg
  end
end
option_each(expected) { |name, type| ... } click to toggle source
# File lib/git_compound/command/arguments/parser.rb, line 51
def option_each(expected)
  # parameters first, arguments last
  opts = expected.sort_by { |_key, value| value[:variant] }.reverse

  opts.each do |opt|
    name = opt.first
    type = option_type(opt.last)

    yield name, type
  end
end
option_type(metadata) click to toggle source
# File lib/git_compound/command/arguments/parser.rb, line 63
def option_type(metadata)
  variant = metadata[:variant].capitalize
  type = metadata[:type].capitalize
  Arguments::Type.const_get("#{variant}::#{type}")
rescue NameError
  raise GitCompoundError,
        "Unknown option variant or type `#{variant}`, `#{type}`"
end