class Pry::CommandSet
Experimental RBS support (using type sampling data from the type_fusion
project).
# sig/pry/command_set.rbs class Pry::CommandSet def add_command: (Class command) -> Class end
different sets, aliased, removed, etc.
This class is used to create sets of commands. Commands can be imported from
def [](pattern)
-
(Pry::Command, nil)
-
Parameters:
-
pattern
(String
) -- The line that might be a command invocation
def [](pattern) commands = @commands.values.select do |command| command.matches?(pattern) end commands.max_by { |command| command.match_score(pattern) } end
def []=(pattern, command)
-
(Pry::Command)
-
Parameters:
-
command
(Pry::Command
) -- -
pattern
(Regexp, String
) --
def []=(pattern, command) if command.equal?(nil) @commands.delete(pattern) return end unless command.is_a?(Class) && command < Pry::Command 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
def add_command(command)
Experimental RBS support (using type sampling data from the type_fusion
project).
def add_command: (Class command) -> Class
This signature was generated using 1 sample from 1 application.
-
command
(Command
) --
def add_command(command) self[command.match] = command end
def alias_command(match, action, options = {})
- Example: Pass explicit description (overriding default). -
Example: Creating an alias for `ls -M` -
Parameters:
-
options
(Hash
) -- The optional configuration parameters, -
action
(String
) -- The action to be performed (typically -
match
(String, Regex
) -- The match of the alias (can be a regex).
def alias_command(match, action, options = {}) (cmd = find_command(action)) || raise("command: '#{action}' not found") original_options = cmd.options.dup options = original_options.merge!( desc: "Alias for `#{action}`", listing: match.is_a?(String) ? match : match.inspect ).merge!(options) # ensure default description is used if desc is nil desc = options.delete(:desc).to_s c = block_command match, desc, options do |*args| run action, *args end # TODO: untested. What's this about? c.class_eval do define_method(:complete) do |input| cmd.new(context).complete(input) end end c.group "Aliases" c end
def block_command(match, description = "No description.", options = {}, &block)
- Example: Regexp command -
Other tags:
- Yield: - The action to perform. The parameters in the block
Options Hash:
(**options)
-
:shellwords
(Boolean
) -- Whether the command's arguments -
:use_prefix
(Boolean
) -- Whether the command uses -
:listing
(String
) -- The listing name of the -
:interpolate
(Boolean
) -- Whether string #{} based -
:keep_retval
(Boolean
) -- Whether or not to use return value
Parameters:
-
options
(Hash
) -- The optional configuration parameters. -
description
(String
) -- A description of the command. -
match
(String, Regexp
) -- The start of invocations of this command.
def block_command(match, description = "No description.", options = {}, &block) if description.is_a?(Hash) options = description description = "No description." end options = Pry::Command.default_options(match).merge!(options) @commands[match] = Pry::BlockCommand.subclass( match, description, options, helper_module, &block ) end
def complete(search, context = {})
-
(Array
-)
Parameters:
-
context
(Hash
) -- The context to create the command with -
search
(String
) -- The line to search for
def complete(search, context = {}) if (command = find_command(search)) command.new(context).complete(search) else keys = @commands.keys.select do |key| key.is_a?(String) && key.start_with?(search) end keys.map { |key| key + " " } end end
def create_command(match, description = "No description.", options = {}, &block)
- Yield: - The class body's definition.
Parameters:
-
options
(Hash
) -- The optional configuration parameters, see {#command} -
description
(String
) -- A description of the command. -
match
(String, Regexp
) -- The start of invocations of this command.
def create_command(match, description = "No description.", options = {}, &block) if description.is_a?(Hash) options = description description = "No description." end options = Pry::Command.default_options(match).merge!(options) @commands[match] = Pry::ClassCommand.subclass( match, description, options, helper_module, &block ) @commands[match].class_eval(&block) @commands[match] end
def delete(*searches)
-
searches
(Array
) -- the matches or listings of the commands
def delete(*searches) searches.each do |search| cmd = find_command_by_match_or_listing(search) @commands.delete cmd.match end end
def desc(search, description = nil)
- Example: Getting -
Example: Setting -
Parameters:
-
description
(String?
) -- (nil) The command description. -
search
(String, Regexp
) -- The command match.
def desc(search, description = nil) cmd = find_command_by_match_or_listing(search) return cmd.description unless description cmd.description = description end
def each(&block)
def each(&block) @commands.each(&block) end
def find_command_by_match_or_listing(match_or_listing)
-
(Command)
- The command object matched.
Parameters:
-
match_or_listing
(String, Regexp
) -- The match or listing of a command.
def find_command_by_match_or_listing(match_or_listing) cmd = (@commands[match_or_listing] || Pry::Helpers::BaseHelpers.find_command(match_or_listing, @commands)) cmd || raise(ArgumentError, "cannot find a command: '#{match_or_listing}'") end
def find_command_for_help(search)
-
(Pry::Command?)
-
Parameters:
-
search
(String
) -- The user's search.
def find_command_for_help(search) find_command(search) || (begin find_command_by_match_or_listing(search) rescue ArgumentError nil end) end
def helpers(&block)
- Yield: - A block defining helper methods
def helpers(&block) helper_module.class_eval(&block) end
def import(*sets)
-
(Pry::CommandSet)
- Returns the reciever (a command set).
Parameters:
-
sets
(Array
) -- Command sets, all of the commands of which
def import(*sets) sets.each do |set| @commands.merge! set.to_hash helper_module.send :include, set.helper_module end self end
def import_from(set, *matches)
-
(Pry::CommandSet)
- Returns the reciever (a command set).
Parameters:
-
matches
(Array
) -- Commands to import -
set
(CommandSet
) -- Set to import commands from
def import_from(set, *matches) helper_module.send :include, set.helper_module matches.each do |match| cmd = set.find_command_by_match_or_listing(match) @commands[cmd.match] = cmd end self end
def initialize(*imported_sets, &block)
- Yield: - Optional block run to define commands
Parameters:
-
imported_sets
(Array
) --
def initialize(*imported_sets, &block) @commands = {} @helper_module = Module.new import(*imported_sets) instance_eval(&block) if block end
def list_commands
-
(Array)
-
def list_commands @commands.keys end
def process_line(val, context = {})
-
(CommandSet::Result)
-
Parameters:
-
context
(Hash
) -- The context to execute the commands with -
val
(String
) -- The line to execute
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
def rename_command(new_match, search, options = {})
- Example: Renaming the `ls` command and changing its description. -
Parameters:
-
options
(Hash
) -- The optional configuration parameters, -
search
(String, Regexp
) -- The command's current match or listing. -
new_match
(String, Regexp
) -- The new match for the command.
def rename_command(new_match, search, options = {}) cmd = find_command_by_match_or_listing(search) options = { listing: new_match, description: cmd.description }.merge!(options) @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) end
def to_hash
def to_hash @commands.dup end
def valid_command?(val)
-
(Boolean)
-
Parameters:
-
val
(String
) --
def valid_command?(val) !!find_command(val) end