Class: Pry::ClassCommand

Inherits:
Command show all
Defined in:
lib/pry/class_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::DocumentationHelpers

Helpers::DocumentationHelpers::YARD_TAGS

Constants included from Helpers::Text

Helpers::Text::COLORS

Instance Attribute Summary collapse

Attributes inherited from Command

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

#_pry_, #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, default_options, #description, #find_hooks, group, #initialize, inspect, #interpolate_string, #match, match_score, matches?, name, #name, #normalize_method_args, options, #pass_block, #process_line, #run, #source, state, #state, subclass, #target_self, #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_range, #get_method_or_raise, #internal_binding?, #one_index_number, #one_index_range, #one_index_range_or_number, #restrict_to_lines, #set_file_and_dir_locals, #temp_file, #unindent

Methods included from Helpers::OptionsHelpers

method_object, #method_object, method_options, #method_options

Methods included from Helpers::BaseHelpers

#colorize_code, #find_command, #heading, #highlight, #not_a_real_file?, #safe_send, #silence_warnings, #stagger_output, #use_ansi_codes?

Constructor Details

This class inherits a constructor from Pry::Command

Instance Attribute Details

#argsObject

Returns the value of attribute args


64
65
66
# File 'lib/pry/class_command.rb', line 64

def args
  @args
end

#optsObject

Returns the value of attribute opts


63
64
65
# File 'lib/pry/class_command.rb', line 63

def opts
  @opts
end

Class Method Details

.docObject


29
30
31
# File 'lib/pry/class_command.rb', line 29

def doc
  new.help
end

.inherited(klass) ⇒ Object

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


19
20
21
22
23
# File 'lib/pry/class_command.rb', line 19

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

.sourceObject


25
26
27
# File 'lib/pry/class_command.rb', line 25

def source
  source_object.source
end

.source_fileObject Also known as: file


37
38
39
# File 'lib/pry/class_command.rb', line 37

def source_file
  source_object.source_file
end

.source_lineObject Also known as: line


42
43
44
# File 'lib/pry/class_command.rb', line 42

def source_line
  source_object.source_line
end

.source_locationObject


33
34
35
# File 'lib/pry/class_command.rb', line 33

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.


54
55
56
57
58
59
60
# File 'lib/pry/class_command.rb', line 54

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


72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/pry/class_command.rb', line 72

def call(*args)
  setup

  self.opts = slop
  self.args = opts.parse!(args)

  if opts.present?(:help)
    output.puts slop.help
    void
  else
    process(*normalize_method_args(method(:process), 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


105
106
107
108
109
# File 'lib/pry/class_command.rb', line 105

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.


87
88
89
# File 'lib/pry/class_command.rb', line 87

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

171
# File 'lib/pry/class_command.rb', line 171

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:


190
191
192
# File 'lib/pry/class_command.rb', line 190

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

121
# File 'lib/pry/class_command.rb', line 121

def setup; end

#slopObject

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


93
94
95
96
97
98
99
100
# File 'lib/pry/class_command.rb', line 93

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

156
# File 'lib/pry/class_command.rb', line 156

def subcommands(cmd); end