class Cl::Runner::Default

Attributes

args[R]
const[R]
ctx[R]
opts[R]

Public Class Methods

new(ctx, args) click to toggle source
# File lib/cl/runner/default.rb, line 20
def initialize(ctx, args)
  @ctx = ctx
  @const, @args = lookup(args)
end

Public Instance Methods

cmd() click to toggle source
# File lib/cl/runner/default.rb, line 31
def cmd
  @cmd ||= const.new(ctx, args)
end
help() click to toggle source
# File lib/cl/runner/default.rb, line 35
def help
  cmd.is_a?(Help) ? cmd : Help.new(ctx, [cmd.registry_key])
end
run() click to toggle source
# File lib/cl/runner/default.rb, line 25
def run
  cmd.help? ? help.run : cmd.send(self.class.run_method)
rescue OptionParser::InvalidOption => e
  raise UnknownOption.new(const, e.message)
end
suggestions(args) click to toggle source
# File lib/cl/runner/default.rb, line 39
def suggestions(args)
  keys = args.inject([]) { |keys, arg| keys << [keys.last, arg].compact.join(':') }
  keys.map { |key| suggest(providers.map(&:to_s), key) }.flatten
end

Private Instance Methods

lookup(args) click to toggle source

Finds a command class to run for the given arguments.

Stopping at any arg that starts with a dash, find the command with the key matching the most args when joined with “:”, and remove these used args from the array

For example, if there are commands registered with the keys

git:pull
git:push

then for the arguments:

git push master

the method `lookup` will find the constant registered as `git:push`, remove these from the `args` array, and return both the constant, and the remaining args.

@param args [Array<String>] arguments to run (usually ARGV)

# File lib/cl/runner/default.rb, line 66
def lookup(args)
  keys = args.take_while { |key| !key.start_with?('-') }

  keys = keys.inject([[], []]) do |keys, key|
    keys[1] << key
    keys[0] << [Cmd[keys[1].join(':')], keys[1].dup] if Cmd.registered?(keys[1].join(':'))
    keys
  end

  cmd, keys = keys[0].last
  raise UnknownCmd.new(self, args) if cmd.nil? || cmd.abstract?
  keys.each { |key| args.delete_at(args.index(key)) }
  [cmd, args]
end
providers() click to toggle source
# File lib/cl/runner/default.rb, line 81
def providers
  Cmd.registry.keys
end