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

Constant Summary

Constants inherited from Command

Pry::Command::VOID_VALUE

Constants included from Helpers::Text

Helpers::Text::COLORS

Instance Attribute Summary collapse

Attributes inherited from Command

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

#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, #correct_arg_arity, default_options, #dependencies_met?, #description, #find_hooks, 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, #use_unpatched_symbol, #void

Methods included from Pry::CodeObject::Helpers

#c_method?, #c_module?, #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::Text

#bold, #default, #indent, #no_color, #no_pager, #strip_color, #with_line_numbers

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, #command_dependencies_met?, #find_command, #heading, #highlight, #not_a_real_file?, #safe_send, #silence_warnings, #stagger_output, #use_ansi_codes?

Methods included from Platform

#jruby?, #jruby_19?, #known_engines, #linux?, #mac_osx?, #mri?, #mri_19?, #mri_2?, #rbx?, #windows?, #windows_ansi?

Constructor Details

This class inherits a constructor from Pry::Command

Instance Attribute Details

#argsObject

Returns the value of attribute args



597
598
599
# File 'lib/pry/command.rb', line 597

def args
  @args
end

#optsObject

Returns the value of attribute opts



596
597
598
# File 'lib/pry/command.rb', line 596

def opts
  @opts
end

Class Method Details

.docObject



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

def doc
  new.help
end

.inherited(klass) ⇒ Object

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



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

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

.sourceObject



558
559
560
# File 'lib/pry/command.rb', line 558

def source
  source_object.source
end

.source_fileObject Also known as: file



570
571
572
# File 'lib/pry/command.rb', line 570

def source_file
  source_object.source_file
end

.source_lineObject Also known as: line



575
576
577
# File 'lib/pry/command.rb', line 575

def source_line
  source_object.source_line
end

.source_locationObject



566
567
568
# File 'lib/pry/command.rb', line 566

def source_location
  source_object.source_location
end

.source_objectPry::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.



587
588
589
590
591
592
593
# File 'lib/pry/command.rb', line 587

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.

Parameters:

  • args (Array<String>)

    The arguments passed

Returns:

  • (Object)

    The return value of process or VOID_VALUE



605
606
607
608
609
610
611
612
613
614
615
616
617
# File 'lib/pry/command.rb', line 605

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

Parameters:

  • search (String)

    The line typed so far

Returns:

  • (Array<String>)

    the words to complete



638
639
640
641
642
# File 'lib/pry/command.rb', line 638

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

#helpObject

Return the help generated by Pry::Slop for this command.



620
621
622
# File 'lib/pry/command.rb', line 620

def help
  slop.help
end

#options(opt) ⇒ Object

Note:

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.

Examples:

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


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

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

Examples:

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

Raises:



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

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

#setupObject

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


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

def setup; end

#slopObject

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



626
627
628
629
630
631
632
633
# File 'lib/pry/command.rb', line 626

def slop
  Pry::Slop.new do |opt|
    opt.banner(unindent(self.class.banner))
    subcommands(opt)
    options(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.

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


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

def subcommands(cmd); end