Class: Pry::CLI

Inherits:
Object show all
Defined in:
lib/pry/cli.rb

Overview

Manage the processing of command line options

Constant Summary

NoOptionsError =
Class.new(StandardError)

Class Attribute Summary (collapse)

Class Method Summary (collapse)

Class Attribute Details

+ (Array<String>) input_args

Returns The input array of strings to process as CLI options.

Returns:

  • (Array<String>)

    The input array of strings to process as CLI options.



20
21
22
# File 'lib/pry/cli.rb', line 20

def input_args
  @input_args
end

+ (Array) option_processors

Returns The Procs that process the parsed options. Plugins can utilize this facility in order to add and process their own Pry options.

Returns:

  • (Array)

    The Procs that process the parsed options. Plugins can utilize this facility in order to add and process their own Pry options.



16
17
18
# File 'lib/pry/cli.rb', line 16

def option_processors
  @option_processors
end

+ (Proc) options

Returns The Proc defining the valid command line options.

Returns:

  • (Proc)

    The Proc defining the valid command line options.



11
12
13
# File 'lib/pry/cli.rb', line 11

def options
  @options
end

Class Method Details

+ (Object) add_option_processor(&block)

Add a block responsible for processing parsed options.



47
48
49
50
51
52
# File 'lib/pry/cli.rb', line 47

def add_option_processor(&block)
  self.option_processors ||= []
  option_processors << block

  self
end

+ (Object) add_options(&block)

Add another set of CLI options (a Slop block)



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pry/cli.rb', line 23

def add_options(&block)
  if options
    old_options = options
    self.options = proc do
      instance_exec(&old_options)
      instance_exec(&block)
    end
  else
    self.options = block
  end

  self
end

+ (Object) add_plugin_options

Bring in options defined in plugins



38
39
40
41
42
43
44
# File 'lib/pry/cli.rb', line 38

def add_plugin_options
  Pry.plugins.values.each do |plugin|
    plugin.load_cli_options
  end

  self
end

+ (Object) parse_options(args = ARGV)



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/pry/cli.rb', line 60

def parse_options(args=ARGV)
  unless options
    raise NoOptionsError, "No command line options defined! Use Pry::CLI.add_options to add command line options."
  end

  self.input_args = args

  begin
    opts = Slop.parse!(
      args,
      :help => true,
      :multiple_switches => false,
      :strict => true,
      &options
    )
  rescue Slop::InvalidOptionError
    # Display help message on unknown switches and exit.
    puts Slop.new(&options)
    exit
  end

  # Option processors are optional.
  if option_processors
    option_processors.each { |processor| processor.call(opts) }
  end

  self
end

+ (Object) reset

Clear options and option_processors



55
56
57
58
# File 'lib/pry/cli.rb', line 55

def reset
  self.options           = nil
  self.option_processors = nil
end