Class | Pry::CommandSet |
In: |
lib/pry/command_set.rb
|
Parent: | Object |
This class is used to create sets of commands. Commands can be imported from different sets, aliased, removed, etc.
commands | [R] | |
helper_module | [R] |
@param [Array<CommandSet>] imported_sets Sets which will be imported
automatically
@yield Optional block run to define commands
Add a given command object to this set. @param [Command] command The subclass of Pry::Command you wish to add.
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. @param [String, Regexp] search The match or listing of the command. @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
Aliases a command @param [String, Regex] match The match of the alias (can be a regex). @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 command description to be passed this way too as `:desc`
@example Creating an alias for `ls -M`
Pry.config.commands.alias_command "lM", "ls -M"
@example Pass explicit description (overriding default).
Pry.config.commands.alias_command "lM", "ls -M", :desc => "cutiepie"
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. @param [String, Regexp] search The match or listing of the command. @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
Defines a new Pry command. @param [String, Regexp] match The start of invocations of this command. @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).
@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 help and by show-command. Necessary for commands with regex matches.
@option options [Boolean] :use_prefix Whether the command uses
`Pry.config.command_prefix` prefix (if one is defined). Defaults to true.
@option options [Boolean] :shellwords Whether the command‘s arguments
should be split using Shellwords instead of just split on spaces. Defaults to true.
@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
MyCommands = Pry::CommandSet.new do 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
@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
Defines a new Pry command class.
@param [String, Regexp] match The start of invocations of this command. @param [String] description A description of the command. @param [Hash] options The optional configuration parameters, see {command} @yield The class body‘s definition.
@example
Pry::Commands.create_command "echo", "echo's the input", :shellwords => false do 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
Removes some commands from the set @param [Array<String>] searches the matches or listings of the commands to remove
Sets or gets the description for a command (replacing the old description). Returns current description if no description parameter provided. @param [String, Regexp] search The command match. @param [String?] description (nil) The command description. @example Setting
MyCommands = Pry::CommandSet.new do desc "help", "help description" end
@example Getting
Pry.config.commands.desc "amend-line"
Find a command that matches the given line @param [String] val The line that might be a command invocation @return [Pry::Command, nil]
Find the command that the user might be trying to refer to. @param [String] search The user‘s search. @return [Pry::Command?]
Imports all the commands from one or more sets. @param [Array<CommandSet>] sets Command sets, all of the commands of which
will be imported.
@return [Pry::CommandSet] Returns the reciever (a command set).
Imports some commands from a set @param [CommandSet] set Set to import commands from @param [Array<String>] matches Commands to import @return [Pry::CommandSet] Returns the reciever (a command set).
Process the given line to see whether it needs executing as a command. @param [String] val The line to execute @param [Hash] context The context to execute the commands with @return [CommandSet::Result]
Rename a command. Accepts either match or listing for the search.
@param [String, Regexp] new_match The new match for the command. @param [String, Regexp] search The command‘s current match or listing. @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"