Class: Pry::ClassCommand
Overview
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 Pry::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.
Direct Known Subclasses
Pry::Command::AmendLine, Pry::Command::Bang, Pry::Command::BangPry, Pry::Command::Cat, Pry::Command::Cd, Pry::Command::ChangeInspector, Pry::Command::ChangePrompt, Pry::Command::ClearScreen, Pry::Command::DisablePry, Pry::Command::Edit, Pry::Command::Exit, Pry::Command::ExitAll, Pry::Command::ExitProgram, Pry::Command::FindMethod, Pry::Command::FixIndent, Pry::Command::Help, Pry::Command::Hist, Pry::Command::ImportSet, Pry::Command::JumpTo, Pry::Command::ListInspectors, Pry::Command::Ls, Pry::Command::Nesting, Pry::Command::Play, Pry::Command::PryBacktrace, Pry::Command::RaiseUp, Pry::Command::ReloadCode, Pry::Command::Reset, Pry::Command::Ri, Pry::Command::SaveFile, Pry::Command::ShellCommand, Pry::Command::ShellMode, Pry::Command::ShowInfo, Pry::Command::ShowInput, Pry::Command::Stat, Pry::Command::SwitchTo, Pry::Command::ToggleColor, Pry::Command::Version, Pry::Command::WatchExpression, Pry::Command::Whereami, Pry::Command::Wtf
Constant Summary
Constants inherited from Command
Constants included from Helpers::DocumentationHelpers
Helpers::DocumentationHelpers::YARD_TAGS
Constants included from Helpers::Text
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#opts ⇒ Object
Returns the value of attribute opts.
Attributes inherited from Command
#arg_string, #captures, #command_block, #command_set, #context, #eval_string, #hooks, #output, #pry_instance, #target
Class Method Summary collapse
- .doc ⇒ Object
-
.inherited(klass) ⇒ Object
Ensure that subclasses inherit the options, description and match from a ClassCommand super class.
- .source ⇒ Object
- .source_file ⇒ Object (also: file)
- .source_line ⇒ Object (also: line)
- .source_location ⇒ Object
-
.source_object ⇒ Pry::WrappedModule, Pry::Method
private
The object used to extract the source for the command.
Instance Method Summary collapse
-
#call(*args) ⇒ Object
Set up
opts
andargs
, and then callprocess
. -
#complete(search) ⇒ Array<String>
Generate shell completions.
-
#help ⇒ Object
Return the help generated by Pry::Slop for this command.
-
#options(opt) ⇒ Object
A method to setup Pry::Slop so it can parse the options your command expects.
-
#process ⇒ Object
The actual body of your command should go here.
-
#setup ⇒ Object
A method called just before
options(opt)
as part ofcall
. -
#slop ⇒ Object
Return an instance of Pry::Slop that can parse either subcommands or the options that this command accepts.
-
#subcommands(cmd) ⇒ Object
A method to setup Pry::Slop commands so it can parse the subcommands your command expects.
Methods inherited from Command
#_pry_, #after_hooks, banner, #before_hooks, #block, #call_safely, #call_with_hooks, #check_for_command_collision, command_name, #command_name, #command_options, command_regex, #commands, convert_to_regex, default_options, #description, #find_hooks, group, #initialize, inspect, #interpolate_string, #match, match_score, matches?, name, #name, #normalize_method_args, options, #pass_block, #process_line, #run, #source, state, #state, subclass, #target_self, #tokenize, #use_unpatched_symbol, #void
Methods included from Helpers::DocumentationHelpers
#get_comment_content, get_comment_content, process_comment_markup, #process_comment_markup, process_rdoc, #process_rdoc, #process_yardoc, process_yardoc, process_yardoc_tag, #process_yardoc_tag, #strip_comments_from_c_code, strip_comments_from_c_code, #strip_leading_whitespace, strip_leading_whitespace
Methods included from Pry::CodeObject::Helpers
#c_method?, #c_module?, #command?, #module_with_yard_docs?, #real_method_object?
Methods included from Helpers::Text
#bold, #default, #indent, #no_color, #no_pager, #strip_color, #with_line_numbers
Methods included from Helpers::CommandHelpers
#absolute_index_number, #absolute_index_range, #get_method_or_raise, #internal_binding?, #one_index_number, #one_index_range, #one_index_range_or_number, #restrict_to_lines, #set_file_and_dir_locals, #temp_file, #unindent
Methods included from Helpers::OptionsHelpers
#method_object, method_object, method_options, #method_options
Methods included from Helpers::BaseHelpers
#colorize_code, #find_command, #heading, #highlight, #not_a_real_file?, #safe_send, #silence_warnings, #stagger_output, #use_ansi_codes?
Constructor Details
This class inherits a constructor from Pry::Command
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
64 65 66 |
# File 'lib/pry/class_command.rb', line 64 def args @args end |
#opts ⇒ Object
Returns the value of attribute opts.
63 64 65 |
# File 'lib/pry/class_command.rb', line 63 def opts @opts end |
Class Method Details
.doc ⇒ Object
29 30 31 |
# File 'lib/pry/class_command.rb', line 29 def doc new.help end |
.inherited(klass) ⇒ Object
Ensure that subclasses inherit the options, description and match from a ClassCommand super class.
19 20 21 22 23 |
# File 'lib/pry/class_command.rb', line 19 def inherited(klass) klass.match match klass.description description klass. end |
.source ⇒ Object
25 26 27 |
# File 'lib/pry/class_command.rb', line 25 def source source_object.source end |
.source_file ⇒ Object Also known as: file
37 38 39 |
# File 'lib/pry/class_command.rb', line 37 def source_file source_object.source_file end |
.source_line ⇒ Object Also known as: line
42 43 44 |
# File 'lib/pry/class_command.rb', line 42 def source_line source_object.source_line end |
.source_location ⇒ Object
33 34 35 |
# File 'lib/pry/class_command.rb', line 33 def source_location source_object.source_location end |
.source_object ⇒ Pry::WrappedModule, Pry::Method (private)
The object used to extract the source for the command.
This should be a Pry::Method(block)
for a command made with create_command
and a Pry::WrappedModule(self)
for a command that's a standard class.
54 55 56 57 58 59 60 |
# File 'lib/pry/class_command.rb', line 54 def source_object @source_object ||= if name =~ /^[A-Z]/ Pry::WrappedModule(self) else Pry::Method(block) end end |
Instance Method Details
#call(*args) ⇒ Object
Set up opts
and args
, and then call process
.
This method will display help if necessary.
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/pry/class_command.rb', line 72 def call(*args) setup self.opts = slop self.args = opts.parse!(args) if opts.present?(:help) output.puts slop.help void else process(*normalize_method_args(method(:process), args)) end end |
#complete(search) ⇒ Array<String>
Generate shell completions
105 106 107 108 109 |
# File 'lib/pry/class_command.rb', line 105 def complete(search) slop.flat_map do |opt| [opt.long && "--#{opt.long} " || opt.short && "-#{opt.short}"] end.compact + super end |
#help ⇒ Object
Return the help generated by Pry::Slop for this command.
87 88 89 |
# File 'lib/pry/class_command.rb', line 87 def help slop.help end |
#options(opt) ⇒ Object
Please don't do anything side-effecty in the main part of this
A method to setup Pry::Slop so it can parse the options your command expects.
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.
171 |
# File 'lib/pry/class_command.rb', line 171 def (opt); end |
#process ⇒ Object
The actual body of your command should go here.
The opts
mehod can be called to get the options that Pry::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.
190 191 192 |
# File 'lib/pry/class_command.rb', line 190 def process raise CommandError, "command '#{command_name}' not implemented" end |
#setup ⇒ Object
A method called just before options(opt)
as part of call
.
This method can be used to set up any context your command needs to run, for example requiring gems, or setting default values for options.
121 |
# File 'lib/pry/class_command.rb', line 121 def setup; end |
#slop ⇒ Object
Return an instance of Pry::Slop that can parse either subcommands or the options that this command accepts.
93 94 95 96 97 98 99 100 |
# File 'lib/pry/class_command.rb', line 93 def slop Pry::Slop.new do |opt| opt.(unindent(self.class.)) subcommands(opt) (opt) opt.on :h, :help, 'Show this message.' end end |
#subcommands(cmd) ⇒ Object
A method to setup Pry::Slop commands so it can parse the subcommands your
command expects. If you need to set up default values, use setup
instead.
156 |
# File 'lib/pry/class_command.rb', line 156 def subcommands(cmd); end |