mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
Added Pry::CLI class for processing command line options, with plugin support.
Plugins can define their own command line options by having a lib/plugin_name/cli.rb file. If this file exists it is loaded immediately before command line options are processed. The contents of the file should be along the lines of: Pry::CLI.add_options do on "my-option", "My first option!" do puts "I just defined an option!" end end
This commit is contained in:
parent
91a249845e
commit
9142aa173f
5 changed files with 140 additions and 81 deletions
82
bin/pry
82
bin/pry
|
@ -12,83 +12,5 @@ rescue LoadError
|
|||
require 'pry'
|
||||
end
|
||||
|
||||
opts = Slop.parse(:help => true, :multiple_switches => false) do
|
||||
banner %{Usage: pry [OPTIONS]
|
||||
Start a Pry session.
|
||||
See: `https://github.com/pry` for more information.
|
||||
Copyright (c) 2011 John Mair (banisterfiend)
|
||||
--
|
||||
}
|
||||
|
||||
on :e, :exec, "A line of code to execute in context before the session starts", true
|
||||
|
||||
on "no-pager", "Disable pager for long output" do
|
||||
Pry.config.pager = false
|
||||
end
|
||||
|
||||
on "no-history", "Disable history loading" do
|
||||
Pry.config.history.should_load = false
|
||||
end
|
||||
|
||||
on "no-color", "Disable syntax highlighting for session" do
|
||||
Pry.color = false
|
||||
end
|
||||
|
||||
on :f, "Suppress loading of ~/.pryrc" do
|
||||
# load ~/.pryrc, if not suppressed with -f option
|
||||
Pry.config.should_load_rc = false
|
||||
end
|
||||
|
||||
on "no-plugins", "Suppress loading of plugins." do
|
||||
# suppress plugins if given --no-plugins optino
|
||||
Pry.config.plugins.enabled = false
|
||||
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
|
||||
end
|
||||
exit
|
||||
end
|
||||
|
||||
on "simple-prompt", "Enable simple prompt mode" do
|
||||
Pry.prompt = Pry::SIMPLE_PROMPT
|
||||
end
|
||||
|
||||
on :r, :require, "`require` a Ruby script at startup", true do |file|
|
||||
Pry.config.requires << file
|
||||
end
|
||||
|
||||
on :I, "Add a path to the $LOAD_PATH", true do |path|
|
||||
$LOAD_PATH << path
|
||||
end
|
||||
|
||||
on :v, :version, "Display the Pry version" do
|
||||
puts "Pry version #{Pry::VERSION} on Ruby #{RUBY_VERSION}"
|
||||
exit
|
||||
end
|
||||
|
||||
on(:c, :context,
|
||||
"Start the session in the specified context. Equivalent to `context.pry` in a session.",
|
||||
true,
|
||||
:default => "TOPLEVEL_BINDING"
|
||||
)
|
||||
end
|
||||
|
||||
# invoked via cli
|
||||
Pry.cli = true
|
||||
|
||||
# create the actual context
|
||||
context = Pry.binding_for(eval(opts[:context]))
|
||||
|
||||
if opts[:exec]
|
||||
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))
|
||||
|
||||
# Process command line options and run Pry
|
||||
Pry::CLI.run
|
||||
|
|
|
@ -192,3 +192,4 @@ require "pry/plugins"
|
|||
require "pry/core_extensions"
|
||||
require "pry/pry_class"
|
||||
require "pry/pry_instance"
|
||||
require "pry/cli"
|
||||
|
|
124
lib/pry/cli.rb
Normal file
124
lib/pry/cli.rb
Normal file
|
@ -0,0 +1,124 @@
|
|||
class Pry
|
||||
|
||||
# Manage the processing of command line options
|
||||
class CLI
|
||||
class << self
|
||||
|
||||
# @return [Proc] The Proc defining the valid command line options.
|
||||
attr_accessor :options
|
||||
end
|
||||
|
||||
# The default Pry command line options (before plugin options are included)
|
||||
def self.default_options
|
||||
proc do
|
||||
banner %{Usage: pry [OPTIONS]
|
||||
Start a Pry session.
|
||||
See: `https://github.com/pry` for more information.
|
||||
Copyright (c) 2011 John Mair (banisterfiend)
|
||||
--
|
||||
}
|
||||
on :e, :exec, "A line of code to execute in context before the session starts", true
|
||||
|
||||
on "no-pager", "Disable pager for long output" do
|
||||
Pry.config.pager = false
|
||||
end
|
||||
|
||||
on "no-history", "Disable history loading" do
|
||||
Pry.config.history.should_load = false
|
||||
end
|
||||
|
||||
on "no-color", "Disable syntax highlighting for session" do
|
||||
Pry.color = false
|
||||
end
|
||||
|
||||
on :f, "Suppress loading of ~/.pryrc" do
|
||||
# load ~/.pryrc, if not suppressed with -f option
|
||||
Pry.config.should_load_rc = false
|
||||
end
|
||||
|
||||
on "no-plugins", "Suppress loading of plugins." do
|
||||
# suppress plugins if given --no-plugins optino
|
||||
Pry.config.plugins.enabled = false
|
||||
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
|
||||
end
|
||||
exit
|
||||
end
|
||||
|
||||
on "simple-prompt", "Enable simple prompt mode" do
|
||||
Pry.config.prompt = Pry::SIMPLE_PROMPT
|
||||
end
|
||||
|
||||
on :r, :require, "`require` a Ruby script at startup", true do |file|
|
||||
Pry.config.requires << file
|
||||
end
|
||||
|
||||
on :I, "Add a path to the $LOAD_PATH", true do |path|
|
||||
$LOAD_PATH << path
|
||||
end
|
||||
|
||||
on :v, :version, "Display the Pry version" do
|
||||
puts "Pry version #{Pry::VERSION} on Ruby #{RUBY_VERSION}"
|
||||
exit
|
||||
end
|
||||
|
||||
on(:c, :context,
|
||||
"Start the session in the specified context. Equivalent to `context.pry` in a session.",
|
||||
true,
|
||||
:default => "TOPLEVEL_BINDING"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
# initialize the options block to the default
|
||||
self.options = default_options
|
||||
|
||||
# Bring in other options defined in a sister proc
|
||||
def self.add_options(&block)
|
||||
old_options = @options
|
||||
@options = proc do
|
||||
instance_exec(&old_options)
|
||||
instance_exec(&block)
|
||||
end
|
||||
end
|
||||
|
||||
# Bring in options defined in plugins
|
||||
def self.add_plugin_options
|
||||
Pry.plugins.values.each do |plugin|
|
||||
plugin.load_cli_options
|
||||
end
|
||||
end
|
||||
|
||||
# Process all options, including those from plugins.
|
||||
# @param [Array] args The array of options.
|
||||
def self.run(args=ARGV)
|
||||
add_plugin_options
|
||||
process_options(args)
|
||||
end
|
||||
|
||||
# Process the current options block.
|
||||
# @param [Array] args The array of options.
|
||||
def self.process_options(args)
|
||||
opts = Slop.parse(args, :help => true, :multiple_switches => false, &options)
|
||||
# invoked via cli
|
||||
Pry.cli = true
|
||||
|
||||
# create the actual context
|
||||
context = Pry.binding_for(eval(opts[:context]))
|
||||
|
||||
if opts[:exec]
|
||||
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))
|
||||
end
|
||||
end
|
||||
end
|
|
@ -105,11 +105,18 @@ class Pry
|
|||
code_type = meth.source_type
|
||||
end
|
||||
|
||||
# prevent Gist from exiting the session on error
|
||||
begin
|
||||
link = Gist.write([:extension => ".#{type_map[code_type]}",
|
||||
:input => content],
|
||||
!opts[:p])
|
||||
rescue SystemExit
|
||||
end
|
||||
|
||||
output.puts "Gist created at #{link}"
|
||||
if link
|
||||
Gist.copy(link)
|
||||
output.puts "Gist created at #{link} and added to clipboard."
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
|
|
@ -32,6 +32,11 @@ class Pry
|
|||
self.enabled = true
|
||||
end
|
||||
|
||||
# Load the Command line options defined by this plugin (if they exist)
|
||||
def load_cli_options
|
||||
cli_options_file = File.join(spec.full_gem_path, "lib/#{spec.name}/cli.rb")
|
||||
require cli_options_file if File.exists?(cli_options_file)
|
||||
end
|
||||
# Activate the plugin (require the gem - enables/loads the
|
||||
# plugin immediately at point of call, even if plugin is
|
||||
# disabled)
|
||||
|
|
Loading…
Add table
Reference in a new issue