| Class | Pry::ClassCommand |
| In: |
lib/pry/command.rb
|
| Parent: | Command |
A super-class of Commands with structure.
This class implements the bare-minimum functionality that a command should have, namely a —help switch, and then delegates actual processing to its subclasses.
Create subclasses using {Pry::CommandSet#create_command}, and override the `options(opt)` method to set up an instance of Slop, and the `process` method to actually run the command. If necessary, you can also override `setup` which will be called before `options`, for example to require any gems your command needs to run, or to set up state.
| source_file | -> | file |
| source_line | -> | line |
| args | [RW] | |
| opts | [RW] |
Ensure that subclasses inherit the options, description and match from a ClassCommand super class.
Generate shell completions @param [String] search The line typed so far @return [Array<String>] the words to complete
A method to setup Slop so it can parse the options your command expects.
@note Please don‘t do anything side-effecty in the main part of this method, as it may be called by Pry at any time for introspection reasons. If you need to set up default values, use `setup` instead.
@example
def options(opt)
opt.banner "Gists methods or classes"
opt.on(:c, :class, "gist a class") do
@action = :class
end
end
The actual body of your command should go here.
The `opts` mehod can be called to get the options that Slop has passed, and `args` gives the remaining, unparsed arguments.
The return value of this method is discarded unless the command was created with `:keep_retval => true`, in which case it is returned to the repl.
@example
def process
if opts.present?(:class)
gist_class
else
gist_method
end
end
Return an instance of Slop that can parse either subcommands or the options that this command accepts.
A method to setup Slop commands so it can parse the subcommands your command expects. If you need to set up default values, use `setup` instead.
@example A minimal example
def subcommands(cmd)
cmd.command :download do |opt|
description 'Downloads a content from a server'
opt.on :verbose, 'Use verbose output'
run do |options, arguments|
ContentDownloader.download(options, arguments)
end
end
end
@example Define the invokation block anywhere you want
def subcommands(cmd)
cmd.command :download do |opt|
description 'Downloads a content from a server'
opt.on :verbose, 'Use verbose output'
end
end
def process
# Perform calculations...
opts.fetch_command(:download).run do |options, arguments|
ContentDownloader.download(options, arguments)
end
# More calculations...
end