Class: Pry::CLI

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

Overview

Manage the processing of command line options

Constant Summary collapse

NoOptionsError =
Class.new(StandardError)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.input_argsArray<String>

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

Returns:

  • (Array<String>)

    The input array of strings to process as CLI options.



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

def input_args
  @input_args
end

.option_processorsArray

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.



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

def option_processors
  @option_processors
end

.optionsProc

Returns The Proc defining the valid command line options.

Returns:

  • (Proc)

    The Proc defining the valid command line options.



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

def options
  @options
end

Class Method Details

.add_option_processor(&block) ⇒ Object

Add a block responsible for processing parsed options.



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

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

  self
end

.add_options(&block) ⇒ Object

Add another set of CLI options (a Pry::Slop block)



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

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

.add_plugin_optionsObject

Bring in options defined in plugins



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

def add_plugin_options
  Pry.plugins.values.each(&:load_cli_options)

  self
end

.parse_options(args = ARGV) ⇒ Object



59
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
88
89
90
91
92
93
94
95
# File 'lib/pry/cli.rb', line 59

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

  @pass_argv = args.index { |cli_arg| %w[- --].include?(cli_arg) }
  if @pass_argv
    slop_args = args[0...@pass_argv]
    self.input_args = args.replace(args[@pass_argv + 1..-1])
  else
    self.input_args = slop_args = args
  end

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

  Pry.initial_session_setup
  Pry.final_session_setup

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

  opts
end

.resetObject

Clear ‘options` and `option_processors`



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

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

.start(opts) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/pry/cli.rb', line 97

def start(opts)
  Kernel.exit if opts.help?

  # invoked via cli
  Pry.cli = true

  # create the actual context
  if opts[:context]
    Pry.initial_session_setup
    context = Pry.binding_for(eval(opts[:context])) # rubocop:disable Security/Eval
    Pry.final_session_setup
  else
    context = Pry.toplevel_binding
  end

  if !@pass_argv && Pry::CLI.input_args.any? && Pry::CLI.input_args != ["pry"]
    full_name = File.expand_path(Pry::CLI.input_args.first)
    Pry.load_file_through_repl(full_name)
    Kernel.exit
  end

  # Start the session (running any code passed with -e, if there is any)
  Pry.start(context, input: StringIO.new(Pry.config.exec_string))
end