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 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::DisablePry, Pry::Command::Edit, Pry::Command::Exit, Pry::Command::ExitAll, Pry::Command::ExitProgram, Pry::Command::FindMethod, Pry::Command::FixIndent, Pry::Command::GemCd, Pry::Command::GemInstall, Pry::Command::GemList, Pry::Command::GemOpen, Pry::Command::Gist, Pry::Command::Help, Pry::Command::Hist, Pry::Command::ImportSet, Pry::Command::InstallCommand, Pry::Command::JumpTo, Pry::Command::ListInspectors, Pry::Command::ListPrompts, 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::SimplePrompt, 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
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#opts ⇒ Object
Returns the value of attribute opts.
Attributes inherited from Command
#_pry_, #arg_string, #captures, #command_block, #command_set, #context, #eval_string, #output, #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
Instance Method Summary collapse
-
#call(*args) ⇒ Object
Set up ‘opts` and `args`, and then call `process`.
-
#complete(search) ⇒ Array<String>
Generate shell completions.
-
#help ⇒ Object
Return the help generated by Slop for this command.
-
#options(opt) ⇒ Object
A method to setup 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 of `call`.
-
#slop ⇒ Object
Return an instance of Slop that can parse either subcommands or the options that this command accepts.
-
#subcommands(cmd) ⇒ Object
A method to setup Slop commands so it can parse the subcommands your command expects.
Methods inherited from Command
banner, #block, #call_safely, #check_for_command_collision, #command_name, command_name, #command_options, command_regex, #commands, convert_to_regex, default_options, #dependencies_met?, #description, group, hooks, #initialize, inspect, #interpolate_string, #match, match_score, matches?, name, #name, options, #process_line, #run, #source, #source_location, #state, subclass, #target_self, #text, #tokenize, #void
Methods included from Helpers::DocumentationHelpers
get_comment_content, process_comment_markup, process_rdoc, process_yardoc, process_yardoc_tag, strip_comments_from_c_code, strip_leading_whitespace
Methods included from Pry::CodeObject::Helpers
#c_method?, #command?, #module_with_yard_docs?, #real_method_object?
Methods included from Helpers::CommandHelpers
absolute_index_number, absolute_index_range, command_error, 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
Methods included from Helpers::BaseHelpers
colorize_code, command_dependencies_met?, find_command, heading, highlight, jruby?, jruby_19?, mri?, mri_19?, mri_20?, mri_21?, mri_2?, not_a_real_file?, rbx?, #safe_send, safe_send, silence_warnings, stagger_output, use_ansi_codes?, windows?, windows_ansi?
Constructor Details
This class inherits a constructor from Pry::Command
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
564 565 566 |
# File 'lib/pry/command.rb', line 564 def args @args end |
#opts ⇒ Object
Returns the value of attribute opts.
563 564 565 |
# File 'lib/pry/command.rb', line 563 def opts @opts end |
Class Method Details
.doc ⇒ Object
529 530 531 |
# File 'lib/pry/command.rb', line 529 def doc new.help end |
.inherited(klass) ⇒ Object
Ensure that subclasses inherit the options, description and match from a ClassCommand super class.
519 520 521 522 523 |
# File 'lib/pry/command.rb', line 519 def inherited(klass) klass.match match klass.description description klass. end |
.source ⇒ Object
525 526 527 |
# File 'lib/pry/command.rb', line 525 def source source_object.source end |
.source_file ⇒ Object Also known as: file
537 538 539 |
# File 'lib/pry/command.rb', line 537 def source_file source_object.source_file end |
.source_line ⇒ Object Also known as: line
542 543 544 |
# File 'lib/pry/command.rb', line 542 def source_line source_object.source_line end |
.source_location ⇒ Object
533 534 535 |
# File 'lib/pry/command.rb', line 533 def source_location source_object.source_location end |
Instance Method Details
#call(*args) ⇒ Object
Set up ‘opts` and `args`, and then call `process`.
This method will display help if necessary.
572 573 574 575 576 577 578 579 580 581 582 583 584 |
# File 'lib/pry/command.rb', line 572 def call(*args) setup self.opts = slop self.args = self.opts.parse!(args) if opts.present?(:help) output.puts slop.help void else process(*correct_arg_arity(method(:process).arity, args)) end end |
#complete(search) ⇒ Array<String>
Generate shell completions
605 606 607 608 609 |
# File 'lib/pry/command.rb', line 605 def complete(search) slop.map do |opt| [opt.long && "--#{opt.long} " || opt.short && "-#{opt.short}"] end.flatten(1).compact + super end |
#help ⇒ Object
Return the help generated by Slop for this command.
587 588 589 |
# File 'lib/pry/command.rb', line 587 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 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.
671 |
# File 'lib/pry/command.rb', line 671 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 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.
690 |
# File 'lib/pry/command.rb', line 690 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.
621 |
# File 'lib/pry/command.rb', line 621 def setup; end |
#slop ⇒ Object
Return an instance of Slop that can parse either subcommands or the options that this command accepts.
593 594 595 596 597 598 599 600 |
# File 'lib/pry/command.rb', line 593 def slop 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 Slop commands so it can parse the subcommands your command expects. If you need to set up default values, use ‘setup` instead.
656 |
# File 'lib/pry/command.rb', line 656 def subcommands(cmd); end |