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.

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?, #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?, mri_2?, #mri_2?, #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

#argsObject

Returns the value of attribute args



564
565
566
# File 'lib/pry/command.rb', line 564

def args
  @args
end

#optsObject

Returns the value of attribute opts



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

def opts
  @opts
end

Class Method Details

.docObject



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.command_options options
end

.sourceObject



525
526
527
# File 'lib/pry/command.rb', line 525

def source
  source_object.source
end

.source_fileObject Also known as: file



537
538
539
# File 'lib/pry/command.rb', line 537

def source_file
  source_object.source_file
end

.source_lineObject Also known as: line



542
543
544
# File 'lib/pry/command.rb', line 542

def source_line
  source_object.source_line
end

.source_locationObject



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

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.



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

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.



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.flat_map do |opt|
    [opt.long && "--#{opt.long} " || opt.short && "-#{opt.short}"]
  end.compact + super
end

#helpObject

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

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


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

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:



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

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


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

def setup; end

#slopObject

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


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

def subcommands(cmd); end