mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
updated cli.rb for new API and added tests for cli
This commit is contained in:
parent
d438a2f037
commit
6a799a7fcd
3 changed files with 213 additions and 107 deletions
2
bin/pry
2
bin/pry
|
@ -13,4 +13,4 @@ rescue LoadError
|
|||
end
|
||||
|
||||
# Process command line options and run Pry
|
||||
Pry::CLI.run
|
||||
Pry::CLI.parse_options
|
||||
|
|
240
lib/pry/cli.rb
240
lib/pry/cli.rb
|
@ -1,124 +1,152 @@
|
|||
|
||||
class Pry
|
||||
|
||||
# Manage the processing of command line options
|
||||
class CLI
|
||||
|
||||
NoOptionsError = Class.new(StandardError)
|
||||
|
||||
class << self
|
||||
|
||||
# @return [Proc] The Proc defining the valid command line options.
|
||||
attr_accessor :options
|
||||
|
||||
# @return [Array] The Procs that process the parsed options.
|
||||
attr_accessor :option_processors
|
||||
|
||||
# Add another set of CLI options
|
||||
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
|
||||
|
||||
# Bring in options defined in plugins
|
||||
def add_plugin_options
|
||||
Pry.plugins.values.each do |plugin|
|
||||
plugin.load_cli_options
|
||||
end
|
||||
|
||||
self
|
||||
end
|
||||
|
||||
# Add a block responsible for processing parsed options.
|
||||
def process_options(&block)
|
||||
self.option_processors ||= []
|
||||
option_processors << block
|
||||
|
||||
self
|
||||
end
|
||||
|
||||
# Clear `options` and `option_processors`
|
||||
def reset
|
||||
self.options = nil
|
||||
self.option_processors = nil
|
||||
end
|
||||
|
||||
def parse_options(args=ARGV)
|
||||
raise NoOptionsError, "No command line options defined! Use Pry::CLI.add_options to add command line options." if !options
|
||||
|
||||
opts = Slop.parse(args, :help => true, :multiple_switches => false, &options)
|
||||
option_processors.each { |processor| processor.call(opts) } if option_processors # option processors are optional
|
||||
|
||||
self
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
# The default Pry command line options (before plugin options are included)
|
||||
def self.default_options
|
||||
proc do
|
||||
banner %{Usage: pry [OPTIONS]
|
||||
reset
|
||||
end
|
||||
end
|
||||
|
||||
# Bring in options defined by plugins
|
||||
Pry::CLI.add_plugin_options
|
||||
|
||||
# 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)
|
||||
--
|
||||
}
|
||||
on :e, :exec, "A line of code to execute in context before the session starts", true
|
||||
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
|
||||
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.process_options do |opts|
|
||||
# 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
|
||||
|
||||
|
|
78
test/test_cli.rb
Normal file
78
test/test_cli.rb
Normal file
|
@ -0,0 +1,78 @@
|
|||
require 'helper'
|
||||
|
||||
describe Pry::Hooks do
|
||||
before do
|
||||
Pry::CLI.reset
|
||||
end
|
||||
|
||||
describe "parsing options" do
|
||||
it 'should raise if no options defined' do
|
||||
lambda { Pry::CLI.parse_options(["--nothing"]) }.should.raise Pry::CLI::NoOptionsError
|
||||
end
|
||||
end
|
||||
|
||||
describe "adding options" do
|
||||
it "should be able to add an option" do
|
||||
run = false
|
||||
|
||||
Pry::CLI.add_options do
|
||||
on :optiontest, "A test option" do
|
||||
run = true
|
||||
end
|
||||
end.parse_options(["--optiontest"])
|
||||
|
||||
run.should == true
|
||||
end
|
||||
|
||||
it "should be able to add multiple options" do
|
||||
run = false
|
||||
run2 = false
|
||||
|
||||
Pry::CLI.add_options do
|
||||
on :optiontest, "A test option" do
|
||||
run = true
|
||||
end
|
||||
end.add_options do
|
||||
on :optiontest2, "Another test option" do
|
||||
run2 = true
|
||||
end
|
||||
end.parse_options(["--optiontest", "--optiontest2"])
|
||||
|
||||
run.should == true
|
||||
run2.should == true
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "processing options" do
|
||||
it "should be able to process an option" do
|
||||
run = false
|
||||
|
||||
Pry::CLI.add_options do
|
||||
on :optiontest, "A test option"
|
||||
end.process_options do |opts|
|
||||
run = true if opts.present?(:optiontest)
|
||||
end.parse_options(["--optiontest"])
|
||||
|
||||
run.should == true
|
||||
end
|
||||
|
||||
it "should be able to process multiple options" do
|
||||
run = false
|
||||
run2 = false
|
||||
|
||||
Pry::CLI.add_options do
|
||||
on :optiontest, "A test option"
|
||||
on :optiontest2, "Another test option"
|
||||
end.process_options do |opts|
|
||||
run = true if opts.present?(:optiontest)
|
||||
end.process_options do |opts|
|
||||
run2 = true if opts.present?(:optiontest2)
|
||||
end.parse_options(["--optiontest", "--optiontest2"])
|
||||
|
||||
run.should == true
|
||||
run2.should == true
|
||||
end
|
||||
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue