Class: Pry::ClassCommand

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

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

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

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Command

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

Methods included from Pry::CodeObject::Helpers

#c_method?, #command?, #module_with_yard_docs?, #real_method_object?

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 Helpers::CommandHelpers

#absolute_index_number, absolute_index_number, absolute_index_range, #absolute_index_range, command_error, #command_error, #get_method_or_raise, get_method_or_raise, internal_binding?, #internal_binding?, one_index_number, #one_index_number, one_index_range, #one_index_range, #one_index_range_or_number, one_index_range_or_number, #restrict_to_lines, restrict_to_lines, #set_file_and_dir_locals, set_file_and_dir_locals, #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?, context_from_object_path, #context_from_object_path, find_command, #find_command, heading, #heading, #highlight, highlight, jruby?, #jruby?, jruby_19?, #jruby_19?, mri?, #mri?, #mri_19?, mri_19?, mri_20?, #mri_20?, #mri_21?, mri_21?, #not_a_real_file?, not_a_real_file?, #rbx?, rbx?, #safe_send, safe_send, silence_warnings, #silence_warnings, #stagger_output, stagger_output, 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

- (Object) args

Returns the value of attribute args



562
563
564
# File 'lib/pry/command.rb', line 562

def args
  @args
end

- (Object) opts

Returns the value of attribute opts



561
562
563
# File 'lib/pry/command.rb', line 561

def opts
  @opts
end

Class Method Details

+ (Object) doc



527
528
529
# File 'lib/pry/command.rb', line 527

def doc
  new.help
end

+ (Object) inherited(klass)

Ensure that subclasses inherit the options, description and match from a ClassCommand super class.



517
518
519
520
521
# File 'lib/pry/command.rb', line 517

def inherited(klass)
  klass.match  match
  klass.description  description
  klass.command_options options
end

+ (Object) source



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

def source
  source_object.source
end

+ (Object) source_file Also known as: file



535
536
537
# File 'lib/pry/command.rb', line 535

def source_file
  source_object.source_file
end

+ (Object) source_line Also known as: line



540
541
542
# File 'lib/pry/command.rb', line 540

def source_line
  source_object.source_line
end

+ (Object) source_location



531
532
533
# File 'lib/pry/command.rb', line 531

def source_location
  source_object.source_location
end

+ (Pry::WrappedModule, Pry::Method) source_object (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.



552
553
554
555
556
557
558
# File 'lib/pry/command.rb', line 552

def source_object
  @source_object ||= if name =~ /^[A-Z]/
                       Pry::WrappedModule(self)
                     else
                       Pry::Method(block)
                     end
end

Instance Method Details

- (Object) call(*args)

Set up opts and args, and then call process.

This method will display help if necessary.

Parameters:

  • args (Array<String>)

    The arguments passed

Returns:

  • (Object)

    The return value of process or VOID_VALUE



570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'lib/pry/command.rb', line 570

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

- (Array<String>) complete(search)

Generate shell completions

Parameters:

  • search (String)

    The line typed so far

Returns:

  • (Array<String>)

    the words to complete



603
604
605
606
607
# File 'lib/pry/command.rb', line 603

def complete(search)
  slop.map do |opt|
    [opt.long && "--#{opt.long} " || opt.short && "-#{opt.short}"]
  end.flatten(1).compact + super
end

- (Object) help

Return the help generated by Slop for this command.



585
586
587
# File 'lib/pry/command.rb', line 585

def help
  slop.help
end

- (Object) options(opt)

Note:

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.

Examples:

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


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

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:



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

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

- (Object) setup

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.

Examples:

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


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

def setup; end

- (Object) slop

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



591
592
593
594
595
596
597
598
# File 'lib/pry/command.rb', line 591

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

- (Object) subcommands(cmd)

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.

Examples:

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

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


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

def subcommands(cmd); end