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
|
|
|
|
|
2012-01-11 22:54:48 -05:00
|
|
|
# Add another set of CLI options (a 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
|
|
|
|
2013-09-05 20:11:29 -04: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
|
|
|
|
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
|
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
|
|
|
|
2011-12-10 10:09:49 -05:00
|
|
|
self
|
2011-12-06 21:07:02 -05:00
|
|
|
end
|
2011-12-10 10:09:49 -05:00
|
|
|
|
2011-12-06 21:07:02 -05:00
|
|
|
end
|
|
|
|
|
2011-12-10 10:09:49 -05:00
|
|
|
reset
|
|
|
|
end
|
|
|
|
end
|
2011-12-06 21:07:02 -05:00
|
|
|
|
2012-07-17 11:59:07 -04:00
|
|
|
|
|
|
|
# String that is built to be executed on start (created by -e and -exec switches)
|
|
|
|
exec_string = ""
|
|
|
|
|
2011-12-10 10:09:49 -05:00
|
|
|
# Bring in options defined by plugins
|
2012-12-08 19:25:59 -05:00
|
|
|
Slop.new do
|
|
|
|
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
|
|
|
|
banner %{Usage: pry [OPTIONS]
|
|
|
|
Start a Pry session.
|
2013-03-10 04:32:23 -04:00
|
|
|
See http://pryrepl.org/ for more information.
|
2013-01-30 08:10:40 -05:00
|
|
|
Copyright (c) 2013 John Mair (banisterfiend)
|
2011-12-10 10:09:49 -05:00
|
|
|
--
|
|
|
|
}
|
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|
|
2013-11-24 18:20:27 -05:00
|
|
|
exec_string << input << "\n"
|
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
|
|
|
|
Pry.color = false
|
|
|
|
end
|
2011-12-06 21:07:02 -05:00
|
|
|
|
2012-07-12 22:23:46 -04:00
|
|
|
on :f, "Suppress loading of ~/.pryrc and ./.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
|
|
|
|
|
2013-04-25 08:29:26 -04:00
|
|
|
on :i, "interactive", "Go interactive after execution" do
|
|
|
|
Pry.config.exit_interactive = true
|
|
|
|
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
|
|
|
|
Pry.config.prompt = Pry::SIMPLE_PROMPT
|
|
|
|
end
|
|
|
|
|
2013-12-12 17:24:05 -05:00
|
|
|
on "noprompt", "No prompt mode" do
|
2013-12-12 17:17:21 -05:00
|
|
|
Pry.config.prompt = Pry::NO_PROMPT
|
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
|
|
|
|
|
2013-01-30 08:10:40 -05:00
|
|
|
on :I=, "Add a path to the $LOAD_PATH", :as => Array, :delimiter => ":" do |load_path|
|
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
|
|
|
|
|
2013-02-03 03:04:53 -05:00
|
|
|
on "gem", "Shorthand for -I./lib -rgemname" do |load_path|
|
|
|
|
$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.",
|
2012-06-11 01:57:40 -04:00
|
|
|
:default => "Pry.toplevel_binding"
|
2011-12-10 10:09:49 -05:00
|
|
|
)
|
2014-01-11 08:42:25 -05:00
|
|
|
end.add_option_processor do |opts|
|
2012-06-17 06:50:18 -04:00
|
|
|
|
2012-06-21 06:10:28 -04:00
|
|
|
exit if opts.help?
|
|
|
|
|
2011-12-10 10:09:49 -05:00
|
|
|
# invoked via cli
|
|
|
|
Pry.cli = true
|
|
|
|
|
|
|
|
# create the actual context
|
2013-03-25 02:02:05 -04:00
|
|
|
if opts[:context]
|
|
|
|
Pry.initial_session_setup
|
|
|
|
context = Pry.binding_for(eval(opts[:context]))
|
|
|
|
else
|
2013-04-25 08:28:18 -04:00
|
|
|
context = Pry.toplevel_binding
|
2013-03-25 02:02:05 -04:00
|
|
|
end
|
2011-12-10 10:09:49 -05:00
|
|
|
|
2012-06-17 06:50:18 -04:00
|
|
|
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)
|
2013-04-25 08:29:26 -04:00
|
|
|
if Pry.config.exit_interactive
|
|
|
|
Pry.toplevel_binding.pry
|
|
|
|
end
|
2012-06-17 06:50:18 -04:00
|
|
|
exit
|
2011-12-10 10:09:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Start the session (running any code passed with -e, if there is any)
|
|
|
|
Pry.start(context, :input => StringIO.new(exec_string))
|
2011-12-06 21:07:02 -05:00
|
|
|
end
|