2011-12-10 10:09:49 -05:00
|
|
|
|
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
|
|
|
|
|
2011-12-10 10:09:49 -05:00
|
|
|
# @return [Array] The Procs that process the parsed options.
|
|
|
|
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.
|
|
|
|
def process_options(&block)
|
|
|
|
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
|
|
|
|
2012-01-29 03:46:12 -05:00
|
|
|
def parse_options(args=ARGV.dup)
|
2011-12-10 10:09:49 -05:00
|
|
|
raise NoOptionsError, "No command line options defined! Use Pry::CLI.add_options to add command line options." if !options
|
2011-12-06 21:07:02 -05:00
|
|
|
|
2012-06-17 06:50:18 -04:00
|
|
|
self.input_args = args
|
|
|
|
|
2011-12-10 10:09:49 -05:00
|
|
|
opts = Slop.parse(args, :help => true, :multiple_switches => false, &options)
|
|
|
|
option_processors.each { |processor| processor.call(opts) } if option_processors # option processors are optional
|
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
|
|
|
|
2011-12-10 10:09:49 -05:00
|
|
|
# Bring in options defined by plugins
|
|
|
|
Pry::CLI.add_plugin_options
|
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.
|
|
|
|
See: `https://github.com/pry` for more information.
|
|
|
|
Copyright (c) 2011 John Mair (banisterfiend)
|
|
|
|
--
|
|
|
|
}
|
2012-05-31 02:38:21 -04:00
|
|
|
on :e, :exec, "A line of code to execute in context before the session starts", :argument => true
|
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
|
|
|
|
2011-12-10 10:09:49 -05:00
|
|
|
on :f, "Suppress loading of ~/.pryrc" do
|
|
|
|
# load ~/.pryrc, if not suppressed with -f option
|
|
|
|
Pry.config.should_load_rc = false
|
|
|
|
end
|
2011-12-06 21:07:02 -05:00
|
|
|
|
2011-12-10 10:09:49 -05:00
|
|
|
on "no-plugins", "Suppress loading of plugins." do
|
|
|
|
# suppress plugins if given --no-plugins optino
|
2012-01-29 20:19:10 -05:00
|
|
|
Pry.config.should_load_plugins = false
|
2011-12-10 10:09:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
on "installed-plugins", "List installed plugins." do
|
|
|
|
puts "Installed Plugins:"
|
|
|
|
puts "--"
|
|
|
|
Pry.locate_plugins.each do |plugin|
|
|
|
|
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
|
|
|
|
|
2012-05-31 02:38:21 -04:00
|
|
|
on :r, :require, "`require` a Ruby script at startup", :argument => true do |file|
|
2011-12-10 10:09:49 -05:00
|
|
|
Pry.config.requires << file
|
|
|
|
end
|
|
|
|
|
2012-05-31 02:38:21 -04:00
|
|
|
on :I, "Add a path to the $LOAD_PATH", :argument => true do |path|
|
2011-12-10 10:09:49 -05:00
|
|
|
$LOAD_PATH << path
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
on(:c, :context,
|
|
|
|
"Start the session in the specified context. Equivalent to `context.pry` in a session.",
|
2012-05-31 02:38:21 -04:00
|
|
|
:argument => true,
|
2012-06-11 01:57:40 -04:00
|
|
|
:default => "Pry.toplevel_binding"
|
2011-12-10 10:09:49 -05:00
|
|
|
)
|
|
|
|
end.process_options do |opts|
|
2012-06-17 06:50:18 -04:00
|
|
|
|
2011-12-10 10:09:49 -05:00
|
|
|
# invoked via cli
|
|
|
|
Pry.cli = true
|
|
|
|
|
|
|
|
# create the actual context
|
|
|
|
context = Pry.binding_for(eval(opts[:context]))
|
|
|
|
|
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)
|
|
|
|
exit
|
|
|
|
elsif opts[:exec]
|
2011-12-10 10:09:49 -05:00
|
|
|
exec_string = opts[:exec] + "\n"
|
|
|
|
else
|
|
|
|
exec_string = ""
|
|
|
|
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
|
2011-12-10 10:09:49 -05:00
|
|
|
|