2011-04-24 10:25:07 -04:00
|
|
|
class Pry
|
|
|
|
class NoCommandError < StandardError
|
2012-04-01 19:00:59 -04:00
|
|
|
def initialize(match, owner)
|
|
|
|
super "Command '#{match}' not found in command set #{owner}"
|
2011-04-24 10:25:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-30 00:46:44 -04:00
|
|
|
# This class is used to create sets of commands. Commands can be imported from
|
2011-04-24 10:25:07 -04:00
|
|
|
# different sets, aliased, removed, etc.
|
|
|
|
class CommandSet
|
2011-05-30 06:56:50 -04:00
|
|
|
include Enumerable
|
2011-04-25 16:58:06 -04:00
|
|
|
include Pry::Helpers::BaseHelpers
|
2011-04-25 15:31:38 -04:00
|
|
|
|
2011-04-24 10:25:07 -04:00
|
|
|
attr_reader :commands
|
2011-04-30 06:28:58 -04:00
|
|
|
attr_reader :helper_module
|
2011-04-24 10:25:07 -04:00
|
|
|
|
|
|
|
# @param [Array<CommandSet>] imported_sets Sets which will be imported
|
|
|
|
# automatically
|
|
|
|
# @yield Optional block run to define commands
|
2011-05-07 01:32:05 -04:00
|
|
|
def initialize(*imported_sets, &block)
|
2011-04-30 06:28:58 -04:00
|
|
|
@commands = {}
|
|
|
|
@helper_module = Module.new
|
2011-04-24 10:25:07 -04:00
|
|
|
|
|
|
|
import(*imported_sets)
|
|
|
|
|
|
|
|
instance_eval(&block) if block
|
|
|
|
end
|
|
|
|
|
|
|
|
# Defines a new Pry command.
|
2012-04-01 19:00:59 -04:00
|
|
|
# @param [String, Regexp] match The start of invocations of this command.
|
2011-04-24 10:25:07 -04:00
|
|
|
# @param [String] description A description of the command.
|
|
|
|
# @param [Hash] options The optional configuration parameters.
|
|
|
|
# @option options [Boolean] :keep_retval Whether or not to use return value
|
|
|
|
# of the block for return of `command` or just to return `nil`
|
|
|
|
# (the default).
|
2011-05-28 10:23:56 -04:00
|
|
|
# @option options [Array<String>] :requires_gem Whether the command has
|
|
|
|
# any gem dependencies, if it does and dependencies not met then
|
|
|
|
# command is disabled and a stub proc giving instructions to
|
|
|
|
# install command is provided.
|
|
|
|
# @option options [Boolean] :interpolate Whether string #{} based
|
|
|
|
# interpolation is applied to the command arguments before
|
|
|
|
# executing the command. Defaults to true.
|
|
|
|
# @option options [String] :listing The listing name of the
|
|
|
|
# command. That is the name by which the command is looked up by
|
2012-04-01 19:00:59 -04:00
|
|
|
# help and by show-command. Necessary for commands with regex matches.
|
2011-07-26 05:42:44 -04:00
|
|
|
# @option options [Boolean] :use_prefix Whether the command uses
|
|
|
|
# `Pry.config.command_prefix` prefix (if one is defined). Defaults
|
|
|
|
# to true.
|
2011-10-02 03:53:23 -04:00
|
|
|
# @option options [Boolean] :shellwords Whether the command's arguments
|
|
|
|
# should be split using Shellwords instead of just split on spaces.
|
|
|
|
# Defaults to true.
|
2011-04-24 10:25:07 -04:00
|
|
|
# @yield The action to perform. The parameters in the block
|
|
|
|
# determines the parameters the command will receive. All
|
|
|
|
# parameters passed into the block will be strings. Successive
|
|
|
|
# command parameters are separated by whitespace at the Pry prompt.
|
|
|
|
# @example
|
2011-05-07 01:32:05 -04:00
|
|
|
# MyCommands = Pry::CommandSet.new do
|
2011-04-24 10:25:07 -04:00
|
|
|
# command "greet", "Greet somebody" do |name|
|
|
|
|
# puts "Good afternoon #{name.capitalize}!"
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# # From pry:
|
|
|
|
# # pry(main)> _pry_.commands = MyCommands
|
|
|
|
# # pry(main)> greet john
|
|
|
|
# # Good afternoon John!
|
|
|
|
# # pry(main)> help greet
|
|
|
|
# # Greet somebody
|
2011-05-28 10:23:56 -04:00
|
|
|
# @example Regexp command
|
|
|
|
# MyCommands = Pry::CommandSet.new do
|
|
|
|
# command /number-(\d+)/, "number-N regex command", :listing => "number" do |num, name|
|
|
|
|
# puts "hello #{name}, nice number: #{num}"
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# # From pry:
|
|
|
|
# # pry(main)> _pry_.commands = MyCommands
|
|
|
|
# # pry(main)> number-10 john
|
|
|
|
# # hello john, nice number: 10
|
|
|
|
# # pry(main)> help number
|
|
|
|
# # number-N regex command
|
2012-04-01 19:00:59 -04:00
|
|
|
def block_command(match, description="No description.", options={}, &block)
|
2012-01-13 03:08:52 -05:00
|
|
|
description, options = ["No description.", description] if description.is_a?(Hash)
|
2012-12-27 17:18:03 -05:00
|
|
|
options = Pry::Command.default_options(match).merge!(options)
|
2011-04-25 15:31:38 -04:00
|
|
|
|
2012-04-01 19:00:59 -04:00
|
|
|
commands[match] = Pry::BlockCommand.subclass(match, description, options, helper_module, &block)
|
2011-12-30 19:55:22 -05:00
|
|
|
end
|
2012-01-08 15:48:54 -05:00
|
|
|
alias_method :command, :block_command
|
2011-12-30 19:55:22 -05:00
|
|
|
|
2011-12-31 09:41:01 -05:00
|
|
|
# Defines a new Pry command class.
|
|
|
|
#
|
2012-04-01 19:00:59 -04:00
|
|
|
# @param [String, Regexp] match The start of invocations of this command.
|
2011-12-31 09:41:01 -05:00
|
|
|
# @param [String] description A description of the command.
|
|
|
|
# @param [Hash] options The optional configuration parameters, see {#command}
|
2012-06-27 01:30:00 -04:00
|
|
|
# @yield The class body's definition.
|
2011-12-31 09:41:01 -05:00
|
|
|
#
|
|
|
|
# @example
|
2012-01-08 16:09:45 -05:00
|
|
|
# Pry::Commands.create_command "echo", "echo's the input", :shellwords => false do
|
2011-12-31 09:41:01 -05:00
|
|
|
# def options(opt)
|
|
|
|
# opt.banner "Usage: echo [-u | -d] <string to echo>"
|
|
|
|
# opt.on :u, :upcase, "ensure the output is all upper-case"
|
|
|
|
# opt.on :d, :downcase, "ensure the output is all lower-case"
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def process
|
|
|
|
# raise Pry::CommandError, "-u and -d makes no sense" if opts.present?(:u) && opts.present?(:d)
|
|
|
|
# result = args.join(" ")
|
|
|
|
# result.downcase! if opts.present?(:downcase)
|
|
|
|
# result.upcase! if opts.present?(:upcase)
|
|
|
|
# output.puts result
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2012-04-01 19:00:59 -04:00
|
|
|
def create_command(match, description="No description.", options={}, &block)
|
2012-01-13 03:08:52 -05:00
|
|
|
description, options = ["No description.", description] if description.is_a?(Hash)
|
2012-12-27 17:18:03 -05:00
|
|
|
options = Pry::Command.default_options(match).merge!(options)
|
2011-12-30 19:55:22 -05:00
|
|
|
|
2012-04-01 19:00:59 -04:00
|
|
|
commands[match] = Pry::ClassCommand.subclass(match, description, options, helper_module, &block)
|
|
|
|
commands[match].class_eval(&block)
|
|
|
|
commands[match]
|
2011-04-24 10:25:07 -04:00
|
|
|
end
|
|
|
|
|
2011-11-14 09:12:19 -05:00
|
|
|
# Execute a block of code before a command is invoked. The block also
|
|
|
|
# gets access to parameters that will be passed to the command and
|
|
|
|
# is evaluated in the same context.
|
2012-04-01 19:00:59 -04:00
|
|
|
# @param [String, Regexp] search The match or listing of the command.
|
2011-11-14 09:12:19 -05:00
|
|
|
# @yield The block to be run before the command.
|
|
|
|
# @example Display parameter before invoking command
|
|
|
|
# Pry.commands.before_command("whereami") do |n|
|
|
|
|
# output.puts "parameter passed was #{n}"
|
|
|
|
# end
|
2012-04-01 19:00:59 -04:00
|
|
|
def before_command(search, &block)
|
|
|
|
cmd = find_command_by_match_or_listing(search)
|
2011-12-31 06:10:23 -05:00
|
|
|
cmd.hooks[:before].unshift block
|
2011-11-14 09:12:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Execute a block of code after a command is invoked. The block also
|
|
|
|
# gets access to parameters that will be passed to the command and
|
|
|
|
# is evaluated in the same context.
|
2012-04-01 19:00:59 -04:00
|
|
|
# @param [String, Regexp] search The match or listing of the command.
|
2011-11-14 09:12:19 -05:00
|
|
|
# @yield The block to be run after the command.
|
|
|
|
# @example Display text 'command complete' after invoking command
|
|
|
|
# Pry.commands.after_command("whereami") do |n|
|
|
|
|
# output.puts "command complete!"
|
|
|
|
# end
|
2012-04-01 19:00:59 -04:00
|
|
|
def after_command(search, &block)
|
|
|
|
cmd = find_command_by_match_or_listing(search)
|
2011-12-31 06:10:23 -05:00
|
|
|
cmd.hooks[:after] << block
|
2011-11-14 09:12:19 -05:00
|
|
|
end
|
|
|
|
|
2012-11-29 12:51:10 -05:00
|
|
|
def each(&block)
|
2011-07-26 05:42:44 -04:00
|
|
|
@commands.each(&block)
|
2011-05-30 06:56:50 -04:00
|
|
|
end
|
|
|
|
|
2011-04-24 10:25:07 -04:00
|
|
|
# Removes some commands from the set
|
2012-04-01 19:00:59 -04:00
|
|
|
# @param [Array<String>] searches the matches or listings of the commands to remove
|
|
|
|
def delete(*searches)
|
|
|
|
searches.each do |search|
|
|
|
|
cmd = find_command_by_match_or_listing(search)
|
|
|
|
commands.delete cmd.match
|
2011-11-15 08:51:17 -05:00
|
|
|
end
|
2011-04-24 10:25:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Imports all the commands from one or more sets.
|
|
|
|
# @param [Array<CommandSet>] sets Command sets, all of the commands of which
|
|
|
|
# will be imported.
|
2012-04-02 06:15:27 -04:00
|
|
|
# @return [Pry::CommandSet] Returns the reciever (a command set).
|
2011-04-24 10:25:07 -04:00
|
|
|
def import(*sets)
|
2011-04-30 06:28:58 -04:00
|
|
|
sets.each do |set|
|
|
|
|
commands.merge! set.commands
|
|
|
|
helper_module.send :include, set.helper_module
|
|
|
|
end
|
2012-04-02 06:15:27 -04:00
|
|
|
self
|
2011-04-24 10:25:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Imports some commands from a set
|
|
|
|
# @param [CommandSet] set Set to import commands from
|
2012-04-01 19:00:59 -04:00
|
|
|
# @param [Array<String>] matches Commands to import
|
2012-04-02 06:15:27 -04:00
|
|
|
# @return [Pry::CommandSet] Returns the reciever (a command set).
|
2012-04-01 19:00:59 -04:00
|
|
|
def import_from(set, *matches)
|
2011-05-01 02:19:50 -04:00
|
|
|
helper_module.send :include, set.helper_module
|
2012-04-01 19:00:59 -04:00
|
|
|
matches.each do |match|
|
|
|
|
cmd = set.find_command_by_match_or_listing(match)
|
|
|
|
commands[cmd.match] = cmd
|
2011-11-15 08:51:17 -05:00
|
|
|
end
|
2012-04-02 06:15:27 -04:00
|
|
|
self
|
2011-04-24 10:25:07 -04:00
|
|
|
end
|
|
|
|
|
2012-04-01 19:00:59 -04:00
|
|
|
# @param [String, Regexp] match_or_listing The match or listing of a command.
|
2011-11-24 00:27:30 -05:00
|
|
|
# of the command to retrieve.
|
|
|
|
# @return [Command] The command object matched.
|
2012-04-01 19:00:59 -04:00
|
|
|
def find_command_by_match_or_listing(match_or_listing)
|
2012-12-25 13:42:47 -05:00
|
|
|
cmd = (commands[match_or_listing] ||
|
|
|
|
Pry::Helpers::BaseHelpers.find_command(match_or_listing, commands))
|
|
|
|
cmd or raise ArgumentError, "Cannot find a command: '#{match_or_listing}'!"
|
2011-11-15 08:51:17 -05:00
|
|
|
end
|
|
|
|
|
2011-04-24 10:25:07 -04:00
|
|
|
# Aliases a command
|
2012-04-01 19:00:59 -04:00
|
|
|
# @param [String, Regex] match The match of the alias (can be a regex).
|
2012-03-25 07:26:48 -04:00
|
|
|
# @param [String] action The action to be performed (typically
|
|
|
|
# another command).
|
|
|
|
# @param [Hash] options The optional configuration parameters,
|
|
|
|
# accepts the same as the `command` method, but also allows the
|
2012-03-25 11:01:07 -04:00
|
|
|
# command description to be passed this way too as `:desc`
|
2012-03-25 07:26:48 -04:00
|
|
|
# @example Creating an alias for `ls -M`
|
|
|
|
# Pry.config.commands.alias_command "lM", "ls -M"
|
2012-03-25 11:01:07 -04:00
|
|
|
# @example Pass explicit description (overriding default).
|
|
|
|
# Pry.config.commands.alias_command "lM", "ls -M", :desc => "cutiepie"
|
2012-04-01 19:00:59 -04:00
|
|
|
def alias_command(match, action, options={})
|
2012-09-13 04:14:47 -04:00
|
|
|
cmd = find_command(action) or fail "Command: `#{action}` not found"
|
|
|
|
original_options = cmd.options.dup
|
2012-04-12 04:58:59 -04:00
|
|
|
|
|
|
|
options = original_options.merge!({
|
|
|
|
:desc => "Alias for `#{action}`",
|
|
|
|
:listing => match
|
|
|
|
}).merge!(options)
|
2012-03-25 07:26:48 -04:00
|
|
|
|
|
|
|
# ensure default description is used if desc is nil
|
2012-03-25 11:01:07 -04:00
|
|
|
desc = options.delete(:desc).to_s
|
2012-03-25 07:26:48 -04:00
|
|
|
|
2012-04-01 19:00:59 -04:00
|
|
|
c = block_command match, desc, options do |*args|
|
2012-03-27 12:05:58 -04:00
|
|
|
run action, *args
|
2012-03-25 07:26:48 -04:00
|
|
|
end
|
2012-03-27 12:05:58 -04:00
|
|
|
|
2012-11-08 04:18:15 -05:00
|
|
|
c.class_eval do
|
|
|
|
define_method(:complete) do |input|
|
|
|
|
cmd.new(context).complete(input)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-27 12:05:58 -04:00
|
|
|
c.group "Aliases"
|
|
|
|
|
|
|
|
c
|
2011-04-24 10:25:07 -04:00
|
|
|
end
|
|
|
|
|
2012-04-01 19:00:59 -04:00
|
|
|
# Rename a command. Accepts either match or listing for the search.
|
|
|
|
#
|
2012-06-27 01:30:00 -04:00
|
|
|
# @param [String, Regexp] new_match The new match for the command.
|
2012-04-01 19:00:59 -04:00
|
|
|
# @param [String, Regexp] search The command's current match or listing.
|
2011-11-24 00:27:30 -05:00
|
|
|
# @param [Hash] options The optional configuration parameters,
|
|
|
|
# accepts the same as the `command` method, but also allows the
|
|
|
|
# command description to be passed this way too.
|
|
|
|
# @example Renaming the `ls` command and changing its description.
|
|
|
|
# Pry.config.commands.rename "dir", "ls", :description => "DOS friendly ls"
|
2012-04-01 19:00:59 -04:00
|
|
|
def rename_command(new_match, search, options={})
|
|
|
|
cmd = find_command_by_match_or_listing(search)
|
2011-11-24 04:33:37 -05:00
|
|
|
|
2011-11-24 00:27:30 -05:00
|
|
|
options = {
|
2012-04-01 19:00:59 -04:00
|
|
|
:listing => new_match,
|
2011-11-24 04:33:37 -05:00
|
|
|
:description => cmd.description
|
2011-11-24 00:27:30 -05:00
|
|
|
}.merge!(options)
|
2011-11-24 04:33:37 -05:00
|
|
|
|
2012-04-01 19:00:59 -04:00
|
|
|
commands[new_match] = cmd.dup
|
|
|
|
commands[new_match].match = new_match
|
|
|
|
commands[new_match].description = options.delete(:description)
|
|
|
|
commands[new_match].options.merge!(options)
|
|
|
|
commands.delete(cmd.match)
|
2011-11-24 00:27:30 -05:00
|
|
|
end
|
|
|
|
|
2013-01-23 20:39:25 -05:00
|
|
|
def disabled_command(name_of_disabled_command, message, matcher=name_of_disabled_command)
|
|
|
|
create_command name_of_disabled_command do
|
2013-01-15 15:48:16 -05:00
|
|
|
match matcher
|
|
|
|
description ""
|
|
|
|
|
|
|
|
define_method(:process) do
|
2013-01-23 20:39:25 -05:00
|
|
|
output.puts "DISABLED: #{message}"
|
2013-01-15 15:48:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-15 08:51:17 -05:00
|
|
|
# Sets or gets the description for a command (replacing the old
|
|
|
|
# description). Returns current description if no description
|
|
|
|
# parameter provided.
|
2012-06-27 01:30:00 -04:00
|
|
|
# @param [String, Regexp] search The command match.
|
|
|
|
# @param [String?] description (nil) The command description.
|
2011-11-15 08:51:17 -05:00
|
|
|
# @example Setting
|
2011-05-07 01:32:05 -04:00
|
|
|
# MyCommands = Pry::CommandSet.new do
|
2011-04-25 02:04:55 -04:00
|
|
|
# desc "help", "help description"
|
|
|
|
# end
|
2011-11-15 08:51:17 -05:00
|
|
|
# @example Getting
|
|
|
|
# Pry.config.commands.desc "amend-line"
|
2012-04-01 19:00:59 -04:00
|
|
|
def desc(search, description=nil)
|
|
|
|
cmd = find_command_by_match_or_listing(search)
|
2011-11-15 08:51:17 -05:00
|
|
|
return cmd.description if !description
|
|
|
|
|
|
|
|
cmd.description = description
|
2011-04-25 02:04:55 -04:00
|
|
|
end
|
2011-04-25 07:26:25 -04:00
|
|
|
|
2011-04-30 06:28:58 -04:00
|
|
|
# Defines helpers methods for this command sets.
|
|
|
|
# Those helpers are only defined in this command set.
|
|
|
|
#
|
|
|
|
# @yield A block defining helper methods
|
|
|
|
# @example
|
|
|
|
# helpers do
|
|
|
|
# def hello
|
|
|
|
# puts "Hello!"
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# include OtherModule
|
|
|
|
# end
|
|
|
|
def helpers(&block)
|
|
|
|
helper_module.class_eval(&block)
|
|
|
|
end
|
|
|
|
|
2011-05-27 12:15:10 -04:00
|
|
|
|
2011-05-30 08:06:55 -04:00
|
|
|
# @return [Array] The list of commands provided by the command set.
|
|
|
|
def list_commands
|
|
|
|
commands.keys
|
|
|
|
end
|
|
|
|
|
2012-01-02 19:04:47 -05:00
|
|
|
# Find a command that matches the given line
|
2012-06-27 01:30:00 -04:00
|
|
|
# @param [String] val The line that might be a command invocation
|
2012-01-02 19:04:47 -05:00
|
|
|
# @return [Pry::Command, nil]
|
2013-09-21 15:16:28 -04:00
|
|
|
def [](pattern)
|
|
|
|
commands.values.select do |command|
|
|
|
|
command.matches?(pattern)
|
|
|
|
end.sort_by do |command|
|
|
|
|
command.match_score(pattern)
|
|
|
|
end.last
|
|
|
|
end
|
|
|
|
alias_method :find_command, :[]
|
|
|
|
|
|
|
|
#
|
|
|
|
# Re-assign the command found at _pattern_ with _command_.
|
|
|
|
#
|
|
|
|
# @param [Regexp, String] pattern
|
|
|
|
# The command to add or replace(found at _pattern_).
|
|
|
|
#
|
|
|
|
# @param [Pry::Command] command
|
|
|
|
# The command to add.
|
|
|
|
#
|
|
|
|
# @return [Pry::Command]
|
|
|
|
# Returns the new command (matched with "pattern".)
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# Pry.commands["help"] = MyHelpCommand
|
|
|
|
#
|
|
|
|
def []=(pattern, command)
|
|
|
|
if command.equal?(nil)
|
|
|
|
return commands.delete(pattern)
|
|
|
|
end
|
2013-09-22 05:39:10 -04:00
|
|
|
unless Class === command && command < Pry::Command
|
2013-09-21 15:16:28 -04:00
|
|
|
raise TypeError, "command is not a subclass of Pry::Command"
|
|
|
|
end
|
|
|
|
bind_command_to_pattern = pattern != command.match
|
|
|
|
if bind_command_to_pattern
|
|
|
|
command_copy = command.dup
|
|
|
|
command_copy.match = pattern
|
|
|
|
@commands[pattern] = command_copy
|
|
|
|
else
|
|
|
|
@commands[pattern] = command
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Add a command to set.
|
|
|
|
#
|
|
|
|
# @param [Command] command
|
|
|
|
# a subclass of Pry::Command.
|
|
|
|
#
|
|
|
|
def add_command(command)
|
|
|
|
self[command.match] = command
|
2012-01-02 19:04:47 -05:00
|
|
|
end
|
|
|
|
|
2012-02-22 00:07:38 -05:00
|
|
|
# Find the command that the user might be trying to refer to.
|
2012-06-27 01:30:00 -04:00
|
|
|
# @param [String] search The user's search.
|
|
|
|
# @return [Pry::Command?]
|
2012-02-22 00:07:38 -05:00
|
|
|
def find_command_for_help(search)
|
|
|
|
find_command(search) || (begin
|
2012-04-01 19:00:59 -04:00
|
|
|
find_command_by_match_or_listing(search)
|
2012-02-22 00:07:38 -05:00
|
|
|
rescue ArgumentError
|
|
|
|
nil
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2012-01-02 19:04:47 -05:00
|
|
|
# Is the given line a command invocation?
|
2012-06-27 01:30:00 -04:00
|
|
|
# @param [String] val
|
2012-01-02 19:04:47 -05:00
|
|
|
# @return [Boolean]
|
|
|
|
def valid_command?(val)
|
|
|
|
!!find_command(val)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Process the given line to see whether it needs executing as a command.
|
2012-06-27 01:30:00 -04:00
|
|
|
# @param [String] val The line to execute
|
|
|
|
# @param [Hash] context The context to execute the commands with
|
|
|
|
# @return [CommandSet::Result]
|
2012-01-02 19:04:47 -05:00
|
|
|
def process_line(val, context={})
|
|
|
|
if command = find_command(val)
|
|
|
|
context = context.merge(:command_set => self)
|
|
|
|
retval = command.new(context).process_line(val)
|
|
|
|
Result.new(true, retval)
|
|
|
|
else
|
|
|
|
Result.new(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-27 01:30:00 -04:00
|
|
|
# @private (used for testing)
|
2012-04-01 19:00:59 -04:00
|
|
|
def run_command(context, match, *args)
|
|
|
|
command = commands[match] or raise NoCommandError.new(match, self)
|
2011-12-31 06:22:38 -05:00
|
|
|
command.new(context).call_safely(*args)
|
2011-12-31 06:10:23 -05:00
|
|
|
end
|
|
|
|
|
2012-08-21 01:12:03 -04:00
|
|
|
# Generate completions for the user's search.
|
|
|
|
# @param [String] search The line to search for
|
|
|
|
# @param [Hash] context The context to create the command with
|
|
|
|
# @return [Array<String>]
|
|
|
|
def complete(search, context={})
|
|
|
|
if command = find_command(search)
|
|
|
|
command.new(context).complete(search)
|
|
|
|
else
|
2013-03-02 19:01:55 -05:00
|
|
|
commands.keys.select do |key|
|
|
|
|
String === key && key.start_with?(search)
|
2013-11-25 17:03:33 -05:00
|
|
|
end.map{ |key| key + " " } + Bond::DefaultMission.completions
|
2012-08-21 01:12:03 -04:00
|
|
|
end
|
|
|
|
end
|
2011-04-24 10:25:07 -04:00
|
|
|
end
|
2012-01-02 19:04:47 -05:00
|
|
|
|
|
|
|
# Wraps the return result of process_commands, indicates if the
|
|
|
|
# result IS a command and what kind of command (e.g void)
|
|
|
|
class Result
|
|
|
|
attr_reader :retval
|
|
|
|
|
|
|
|
def initialize(is_command, retval = nil)
|
|
|
|
@is_command, @retval = is_command, retval
|
|
|
|
end
|
|
|
|
|
|
|
|
# Is the result a command?
|
|
|
|
# @return [Boolean]
|
|
|
|
def command?
|
|
|
|
@is_command
|
|
|
|
end
|
|
|
|
|
|
|
|
# Is the result a command and if it is, is it a void command?
|
|
|
|
# (one that does not return a value)
|
|
|
|
# @return [Boolean]
|
|
|
|
def void_command?
|
|
|
|
retval == Command::VOID_VALUE
|
|
|
|
end
|
|
|
|
end
|
2011-04-24 10:25:07 -04:00
|
|
|
end
|