Class: Pry::ClassCommand

Inherits:
Command show all
Defined in:
lib/pry/command.rb

Overview

A super-class ofr 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 Pry::Command.options, for example to require any gems your command needs to run, or to set up state.

Constant Summary

Constant Summary

Constants inherited from Command

Pry::Command::VOID_VALUE

Instance Attribute Summary (collapse)

Attributes inherited from Command

#_pry_, #arg_string, #captures, #command_block, #command_set, #context, #eval_string, #output, #target

Instance Method Summary (collapse)

Methods inherited from Command

banner, #block, #call_safely, #call_with_hooks, #check_for_command_collision, #command_name, #command_options, command_regex, #commands, convert_to_regex, #correct_arg_arity, #dependencies_met?, #description, group, hooks, #initialize, inspect, #interpolate_string, #match, match_score, matches?, #name, name, options, #pass_block, #process_line, #run, subclass, #target_self, #text, #tokenize, #void

Methods included from Helpers::CommandHelpers

#absolute_index_number, absolute_index_number, absolute_index_range, #absolute_index_range, blocking_flag_for_editor, #blocking_flag_for_editor, command_error, #command_error, editor_name, #editor_name, file_and_line_from_binding, #file_and_line_from_binding, #get_method_or_raise, get_method_or_raise, #invoke_editor, invoke_editor, make_header, #make_header, one_index_number, #one_index_number, one_index_range, #one_index_range, one_index_range_or_number, #one_index_range_or_number, #render_output, render_output, #restrict_to_lines, restrict_to_lines, #start_line_syntax_for_editor, start_line_syntax_for_editor, #temp_file, temp_file, #unindent, unindent

Methods included from Helpers::OptionsHelpers

method_object, #method_object, method_options, #method_options

Methods included from Helpers::BaseHelpers

colorize_code, #colorize_code, command_dependencies_met?, #command_dependencies_met?, #create_command_stub, create_command_stub, #find_command, find_command, #gem_installed?, gem_installed?, #heading, heading, highlight, #highlight, jruby?, #jruby?, #lesspipe, lesspipe, #mri_18?, mri_18?, mri_19?, #mri_19?, #not_a_real_file?, not_a_real_file?, #page_size, page_size, #rbx?, rbx?, set_file_and_dir_locals, #set_file_and_dir_locals, silence_warnings, #silence_warnings, simple_pager, #simple_pager, stagger_output, #stagger_output, #stub_proc, stub_proc, #use_ansi_codes?, use_ansi_codes?, #windows?, windows?

Constructor Details

This class inherits a constructor from Pry::Command

Instance Attribute Details

- (Object) args

Returns the value of attribute args



444
445
446
# File 'lib/pry/command.rb', line 444

def args
  @args
end

- (Object) opts

Returns the value of attribute opts



443
444
445
# File 'lib/pry/command.rb', line 443

def opts
  @opts
end

Instance Method Details

- (Object) call(*args)

Set up opts and args, and then call process

This function will display help if necessary.

Parameters:

  • *String

    the arguments passed

Returns:

  • Object the return value of process or VOID_VALUE



452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/pry/command.rb', line 452

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

- (Object) help

Return the help generated by Slop for this command.



467
468
469
# File 'lib/pry/command.rb', line 467

def help
  slop.help
end

- (Object) options(opt)

A function 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.

Examples:

def options(opt)
  opt.banner "Gists methods or classes"
  opt.on(:c, :class, "gist a class") do
    @action = :class
  end
end


505
# File 'lib/pry/command.rb', line 505

def options(opt); end

- (Object) process

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.

Examples:

def process
  if opts.present?(:class)
    gist_class
  else
    gist_method
  end
end

Raises:



523
# File 'lib/pry/command.rb', line 523

def process; raise CommandError, "command '#{command_name}' not implemented" end

- (Object) setup

A function called just before options(opt) as part of call.

This function can be used to set up any context your command needs to run, for example requiring gems, or setting default values for options.

Examples:

def setup;
  require 'gist'
  @action = :method
end


490
# File 'lib/pry/command.rb', line 490

def setup; end

- (Object) slop

Return an instance of Slop that can parse the options that this command accepts.



472
473
474
475
476
477
478
# File 'lib/pry/command.rb', line 472

def slop
  Slop.new do |opt|
    opt.banner(unindent(self.class.banner))
    options(opt)
    opt.on(:h, :help, "Show this message.")
  end
end