class Commander::Runner

Constants

INBUILT_ERRORS

Run command parsing and execution process

Attributes

commands[R]

Array of commands.

global_slop[R]

The global Slop Options

help_formatter_aliases[R]

Hash of help formatter aliases.

trace[RW]

Display the backtrace in the event of an error

Public Class Methods

new(*inputs) click to toggle source
# File lib/commander/runner.rb, line 33
def initialize(*inputs)
  @program, @commands, @default_command, \
    @global_slop, @aliases, @args = inputs.map(&:dup)

  @commands['help'] ||= Command.new('help').tap do |c|
    c.syntax = "#{program(:name)} help [command]"
    c.description = 'Display global or [command] help documentation'
    c.example 'Display global help', "#{program(:name)} help"
    c.example "Display help for 'foo'", "#{program(:name)} help foo"
    c.when_called do |help_args, _|
      self.run_help_command(help_args)
    end
  end
  @help_formatter_aliases = help_formatter_alias_defaults
end

Public Instance Methods

alias?(name) click to toggle source

Check if command name is an alias.

# File lib/commander/runner.rb, line 141
def alias?(name)
  @aliases.include? name.to_s
end
command(name) click to toggle source

Creates and yields a command instance when a block is passed. Otherwise attempts to return the command, raising InvalidCommandError when it does not exist.

Examples

command :my_command do |c|
  c.when_called do |args|
    # Code
  end
end
# File lib/commander/runner.rb, line 134
def command(name)
  @commands[name.to_s]
end
program(key, default = nil) click to toggle source

The hash of program variables

# File lib/commander/runner.rb, line 115
def program(key, default = nil)
  @program[key] ||= default if default
  @program[key]
end
run() click to toggle source
# File lib/commander/runner.rb, line 57
def run
  require_program :version, :description

  # Determine where the arguments/ options start
  remaining_args = if alias? command_name_from_args
    @aliases[command_name_from_args.to_s] + args_without_command_name
  else
    args_without_command_name
  end

  # Combines the global and command options into a single parser
  global_opts = global_slop.options
  command_opts = active_command? ? active_command.slop.options : []
  opts = [*global_opts, *command_opts]
  parser = Slop::Parser.new(opts)

  # Parsers the arguments/opts and fetches the config
  parser.parse(remaining_args)
  opts = OpenStruct.new parser.parse(remaining_args).to_h
  remaining_args = parser.arguments
  config = program(:config).dup

  if opts.version
    # Return the version
    say version
    exit 0
  elsif opts.help && active_command?
    # Return help for the active_command
    run_help_command([active_command!.name])
  elsif active_command?
    # Run the active_command
    active_command.run!(remaining_args, opts, config)
  else
    # Return generic help
    run_help_command('')
  end
rescue => original
  error = original
  if INBUILT_ERRORS.include?(error.class)
    error = InternalCallableError.new(error.message) do
      $stderr.puts "\nUsage:\n\n"
      name = active_command? ? active_command.name : :error
      run_help_command([name])
    end
  end
  raise error
end
version() click to toggle source

Return program version.

# File lib/commander/runner.rb, line 108
def version
  format('%s %s', program(:name), program(:version))
end