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 options, for example to require any gems your command needs to run, or to set up state.

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, #state, 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, #internal_binding?, internal_binding?, #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?, #windows_ansi?, windows_ansi?

Constructor Details

This class inherits a constructor from Pry::Command

Instance Attribute Details

#argsObject

Returns the value of attribute args.



454
455
456
# File 'lib/pry/command.rb', line 454

def args
  @args
end

#optsObject

Returns the value of attribute opts.



453
454
455
# File 'lib/pry/command.rb', line 453

def opts
  @opts
end

Instance Method Details

#call(*args) ⇒ Object

Set up opts and args, and then call process.

This function will display help if necessary.

Parameters:

  • args (Array<String>)

    The arguments passed

Returns:

  • (Object)

    The return value of process or VOID_VALUE



462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'lib/pry/command.rb', line 462

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

#helpObject

Return the help generated by Slop for this command.



477
478
479
# File 'lib/pry/command.rb', line 477

def help
  slop.help
end

#options(opt) ⇒ Object

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


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

def options(opt); end

#processObject

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:



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

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

#setupObject

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


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

def setup; end

#slopObject

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



482
483
484
485
486
487
488
# File 'lib/pry/command.rb', line 482

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