2011-12-06 21:07:02 -05:00
|
|
|
class Pry
|
|
|
|
# Manage the processing of command line options
|
|
|
|
class CLI
|
2011-12-10 10:09:49 -05:00
|
|
|
NoOptionsError = Class.new(StandardError)
|
|
|
|
|
2011-12-06 21:07:02 -05:00
|
|
|
class << self
|
|
|
|
# @return [Proc] The Proc defining the valid command line options.
|
|
|
|
attr_accessor :options
|
|
|
|
|
2013-11-27 18:00:00 -05:00
|
|
|
# @return [Array] The Procs that process the parsed options. Plugins can
|
|
|
|
# utilize this facility in order to add and process their own Pry
|
|
|
|
# options.
|
2011-12-10 10:09:49 -05:00
|
|
|
attr_accessor :option_processors
|
2011-12-06 21:07:02 -05:00
|
|
|
|
2012-06-17 06:50:18 -04:00
|
|
|
# @return [Array<String>] The input array of strings to process
|
|
|
|
# as CLI options.
|
|
|
|
attr_accessor :input_args
|
|
|
|
|
2015-12-11 01:04:54 -05:00
|
|
|
# Add another set of CLI options (a Pry::Slop block)
|
2011-12-10 10:09:49 -05:00
|
|
|
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
|
2011-12-06 21:07:02 -05:00
|
|
|
end
|
|
|
|
|
2011-12-10 10:09:49 -05:00
|
|
|
self
|
|
|
|
end
|
2011-12-06 21:07:02 -05:00
|
|
|
|
2011-12-10 10:09:49 -05:00
|
|
|
# Bring in options defined in plugins
|
|
|
|
def add_plugin_options
|
|
|
|
Pry.plugins.values.each do |plugin|
|
|
|
|
plugin.load_cli_options
|
2011-12-06 21:07:02 -05:00
|
|
|
end
|
|
|
|
|
2011-12-10 10:09:49 -05:00
|
|
|
self
|
|
|
|
end
|
2011-12-06 21:07:02 -05:00
|
|
|
|
2011-12-10 10:09:49 -05:00
|
|
|
# Add a block responsible for processing parsed options.
|
2014-01-11 08:42:25 -05:00
|
|
|
def add_option_processor(&block)
|
2011-12-10 10:09:49 -05:00
|
|
|
self.option_processors ||= []
|
|
|
|
option_processors << block
|
2011-12-06 21:07:02 -05:00
|
|
|
|
2011-12-10 10:09:49 -05:00
|
|
|
self
|
|
|
|
end
|
2011-12-06 21:07:02 -05:00
|
|
|
|
2011-12-10 10:09:49 -05:00
|
|
|
# Clear `options` and `option_processors`
|
|
|
|
def reset
|
|
|
|
self.options = nil
|
|
|
|
self.option_processors = nil
|
|
|
|
end
|
2011-12-06 21:07:02 -05:00
|
|
|
|
2018-11-04 04:34:24 -05:00
|
|
|
def parse_options(args = ARGV)
|
2012-08-15 20:05:34 -04:00
|
|
|
unless options
|
|
|
|
raise NoOptionsError, "No command line options defined! Use Pry::CLI.add_options to add command line options."
|
|
|
|
end
|
2011-12-06 21:07:02 -05:00
|
|
|
|
2012-06-17 06:50:18 -04:00
|
|
|
self.input_args = args
|
|
|
|
|
2013-03-10 04:53:37 -04:00
|
|
|
begin
|
2015-12-11 01:04:54 -05:00
|
|
|
opts = Pry::Slop.parse!(
|
2013-03-10 04:53:37 -04:00
|
|
|
args,
|
2018-10-12 15:09:29 -04:00
|
|
|
help: true,
|
|
|
|
multiple_switches: false,
|
|
|
|
strict: true,
|
2013-03-10 04:53:37 -04:00
|
|
|
&options
|
|
|
|
)
|
2015-12-11 01:04:54 -05:00
|
|
|
rescue Pry::Slop::InvalidOptionError
|
2013-03-10 04:53:37 -04:00
|
|
|
# Display help message on unknown switches and exit.
|
2015-12-11 01:04:54 -05:00
|
|
|
puts Pry::Slop.new(&options)
|
2013-03-10 04:53:37 -04:00
|
|
|
exit
|
|
|
|
end
|
2012-08-15 20:05:34 -04:00
|
|
|
|
2018-10-21 17:24:46 -04:00
|
|
|
Pry.initial_session_setup
|
2015-06-17 20:54:47 -04:00
|
|
|
Pry.final_session_setup
|
|
|
|
|
2012-08-15 20:05:34 -04:00
|
|
|
# Option processors are optional.
|
|
|
|
if option_processors
|
|
|
|
option_processors.each { |processor| processor.call(opts) }
|
|
|
|
end
|
2011-12-06 21:07:02 -05:00
|
|
|
|
2015-03-11 16:53:21 -04:00
|
|
|
opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def start(opts)
|
|
|
|
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]))
|
2015-06-28 05:50:49 -04:00
|
|
|
Pry.final_session_setup
|
2015-03-11 16:53:21 -04:00
|
|
|
else
|
|
|
|
context = Pry.toplevel_binding
|
|
|
|
end
|
|
|
|
|
|
|
|
if 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)
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
|
|
|
# Start the session (running any code passed with -e, if there is any)
|
2018-10-12 15:09:29 -04:00
|
|
|
Pry.start(context, input: StringIO.new(Pry.config.exec_string))
|
2011-12-06 21:07:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-10 10:09:49 -05:00
|
|
|
reset
|
|
|
|
end
|
|
|
|
end
|
2011-12-06 21:07:02 -05:00
|
|
|
|
2011-12-10 10:09:49 -05:00
|
|
|
# Bring in options defined by plugins
|
2015-12-11 01:04:54 -05:00
|
|
|
Pry::Slop.new do
|
2012-12-08 19:25:59 -05:00
|
|
|
on "no-plugins" do
|
|
|
|
Pry.config.should_load_plugins = false
|
|
|
|
end
|
|
|
|
end.parse(ARGV.dup)
|
|
|
|
|
|
|
|
if Pry.config.should_load_plugins
|
2013-03-10 04:32:23 -04:00
|
|
|
Pry::CLI.add_plugin_options
|
2012-12-08 19:25:59 -05:00
|
|
|
end
|
2011-12-06 21:07:02 -05:00
|
|
|
|
2011-12-10 10:09:49 -05:00
|
|
|
# The default Pry command line options (before plugin options are included)
|
|
|
|
Pry::CLI.add_options do
|
2018-10-28 07:32:04 -04:00
|
|
|
banner(
|
|
|
|
"Usage: pry [OPTIONS]\n" \
|
|
|
|
"Start a Pry session.\n" \
|
|
|
|
"See http://pryrepl.org/ for more information.\n"
|
|
|
|
)
|
|
|
|
|
2013-01-30 08:10:40 -05:00
|
|
|
on :e, :exec=, "A line of code to execute in context before the session starts" do |input|
|
2015-03-26 19:30:21 -04:00
|
|
|
Pry.config.exec_string += "\n" if Pry.config.exec_string.length > 0
|
|
|
|
Pry.config.exec_string += input
|
2012-07-17 11:59:07 -04:00
|
|
|
end
|
2011-12-06 21:07:02 -05:00
|
|
|
|
2011-12-10 10:09:49 -05:00
|
|
|
on "no-pager", "Disable pager for long output" do
|
|
|
|
Pry.config.pager = false
|
|
|
|
end
|
2011-12-06 21:07:02 -05:00
|
|
|
|
2011-12-10 10:09:49 -05:00
|
|
|
on "no-history", "Disable history loading" do
|
|
|
|
Pry.config.history.should_load = false
|
|
|
|
end
|
2011-12-06 21:07:02 -05:00
|
|
|
|
2011-12-10 10:09:49 -05:00
|
|
|
on "no-color", "Disable syntax highlighting for session" do
|
2014-04-29 03:03:15 -04:00
|
|
|
Pry.config.color = false
|
2011-12-10 10:09:49 -05:00
|
|
|
end
|
2011-12-06 21:07:02 -05:00
|
|
|
|
2018-10-10 12:16:51 -04:00
|
|
|
on :f, "Suppress loading of pryrc" do
|
2011-12-10 10:09:49 -05:00
|
|
|
Pry.config.should_load_rc = false
|
2012-07-12 22:23:46 -04:00
|
|
|
Pry.config.should_load_local_rc = false
|
2011-12-10 10:09:49 -05:00
|
|
|
end
|
2011-12-06 21:07:02 -05:00
|
|
|
|
2013-01-30 08:10:40 -05:00
|
|
|
on :s, "select-plugin=", "Only load specified plugin (and no others)." do |plugin_name|
|
2012-06-26 07:44:13 -04:00
|
|
|
Pry.config.should_load_plugins = false
|
|
|
|
Pry.plugins[plugin_name].activate!
|
|
|
|
end
|
|
|
|
|
2013-01-30 08:10:40 -05:00
|
|
|
on :d, "disable-plugin=", "Disable a specific plugin." do |plugin_name|
|
2012-06-26 07:44:13 -04:00
|
|
|
Pry.plugins[plugin_name].disable!
|
|
|
|
end
|
|
|
|
|
2011-12-10 10:09:49 -05:00
|
|
|
on "no-plugins", "Suppress loading of plugins." do
|
2012-01-29 20:19:10 -05:00
|
|
|
Pry.config.should_load_plugins = false
|
2011-12-10 10:09:49 -05:00
|
|
|
end
|
|
|
|
|
2013-11-06 03:14:16 -05:00
|
|
|
on "plugins", "List installed plugins." do
|
2011-12-10 10:09:49 -05:00
|
|
|
puts "Installed Plugins:"
|
|
|
|
puts "--"
|
|
|
|
Pry.locate_plugins.each do |plugin|
|
2013-11-12 01:54:31 -05:00
|
|
|
puts "#{plugin.name}".ljust(18) << plugin.spec.summary
|
2011-12-06 21:07:02 -05:00
|
|
|
end
|
2011-12-10 10:09:49 -05:00
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
|
|
|
on "simple-prompt", "Enable simple prompt mode" do
|
2018-11-10 15:20:45 -05:00
|
|
|
Pry.config.prompt = Pry::Prompt[:simple]
|
2011-12-10 10:09:49 -05:00
|
|
|
end
|
|
|
|
|
2013-12-12 17:24:05 -05:00
|
|
|
on "noprompt", "No prompt mode" do
|
2018-11-10 15:20:45 -05:00
|
|
|
Pry.config.prompt = Pry::Prompt[:none]
|
2013-12-12 16:25:28 -05:00
|
|
|
end
|
|
|
|
|
2013-01-30 08:10:40 -05:00
|
|
|
on :r, :require=, "`require` a Ruby script at startup" do |file|
|
2011-12-10 10:09:49 -05:00
|
|
|
Pry.config.requires << file
|
|
|
|
end
|
|
|
|
|
2018-11-11 07:28:42 -05:00
|
|
|
on(:I=, "Add a path to the $LOAD_PATH", as: Array, delimiter: ":") do
|
2012-08-15 20:05:34 -04:00
|
|
|
load_path.map! do |path|
|
|
|
|
/\A\.\// =~ path ? path : File.expand_path(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
$LOAD_PATH.unshift(*load_path)
|
2011-12-10 10:09:49 -05:00
|
|
|
end
|
|
|
|
|
2018-11-11 07:22:03 -05:00
|
|
|
on "gem", "Shorthand for -I./lib -rgemname" do |_load_path|
|
2013-02-03 03:04:53 -05:00
|
|
|
$LOAD_PATH.unshift("./lib")
|
|
|
|
Dir["./lib/*.rb"].each do |file|
|
|
|
|
Pry.config.requires << file
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-12-10 10:09:49 -05:00
|
|
|
on :v, :version, "Display the Pry version" do
|
|
|
|
puts "Pry version #{Pry::VERSION} on Ruby #{RUBY_VERSION}"
|
|
|
|
exit
|
2011-12-06 21:07:02 -05:00
|
|
|
end
|
2011-12-10 10:09:49 -05:00
|
|
|
|
2013-01-30 08:10:40 -05:00
|
|
|
on(:c, :context=,
|
2011-12-10 10:09:49 -05:00
|
|
|
"Start the session in the specified context. Equivalent to `context.pry` in a session.",
|
2018-10-12 15:09:29 -04:00
|
|
|
default: "Pry.toplevel_binding"
|
2011-12-10 10:09:49 -05:00
|
|
|
)
|
2011-12-06 21:07:02 -05:00
|
|
|
end
|