1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Share argument parsing code for methody commands

This commit is contained in:
Conrad Irwin 2011-10-15 00:03:41 -07:00
parent 5d6e1ce7b2
commit bf85c62c42
7 changed files with 85 additions and 114 deletions

View file

@ -10,45 +10,26 @@ class Pry
command "show-doc", "Show the comments above METH. Type `show-doc --help` for more info. Aliases: \?" do |*args|
target = target()
opts = Slop.parse!(args) do |opt|
opts = parse_options!(args, :method_object => true) do |opt|
opt.banner unindent <<-USAGE
Usage: show-doc [OPTIONS] [METH 1] [METH 2] [METH N]
Usage: show-doc [OPTIONS] [METH]
Show the comments above method METH. Tries instance methods first and then methods by default.
e.g show-doc hello_method
USAGE
opt.on :M, "instance-methods", "Operate on instance methods."
opt.on :m, :methods, "Operate on methods."
opt.on :c, :context, "Select object context to run under.", true do |context|
target = Pry.binding_for(target.eval(context))
end
opt.on :f, :flood, "Do not use a pager to view text longer than one screen."
opt.on :h, :help, "This message." do
output.puts opt
end
end
next if opts.help?
args = [nil] if args.empty?
args.each do |method_name|
begin
meth = get_method_or_raise(method_name, target, opts.to_hash(true))
rescue CommandError => e
puts "\nError: #{e.message}"
next
end
meth = opts[:method_object]
raise Pry::CommandError, "No documentation found." if meth.doc.nil? || meth.doc.empty?
next output.puts("No documentation found.") if meth.doc.nil? || meth.doc.empty?
doc = process_comment_markup(meth.doc, meth.source_type)
output.puts make_header(meth, doc)
output.puts "#{text.bold("visibility: ")} #{meth.visibility}"
output.puts "#{text.bold("signature: ")} #{meth.signature}"
output.puts
render_output(opts.flood?, false, doc)
doc
end
doc = process_comment_markup(meth.doc, meth.source_type)
output.puts make_header(meth, doc)
output.puts "#{text.bold("visibility: ")} #{meth.visibility}"
output.puts "#{text.bold("signature: ")} #{meth.signature}"
output.puts
render_output(opts.flood?, false, doc)
end
alias_command "?", "show-doc"
@ -56,27 +37,16 @@ class Pry
command "stat", "View method information and set _file_ and _dir_ locals. Type `stat --help` for more info." do |*args|
target = target()
opts = Slop.parse!(args) do |opt|
opts = parse_options!(args, :method_object => true) do |opt|
opt.banner unindent <<-USAGE
Usage: stat [OPTIONS] [METH]
Show method information for method METH and set _file_ and _dir_ locals.
e.g: stat hello_method
USAGE
opt.on :M, "instance-methods", "Operate on instance methods."
opt.on :m, :methods, "Operate on methods."
opt.on :c, :context, "Select object context to run under.", true do |context|
target = Pry.binding_for(target.eval(context))
end
opt.on :h, :help, "This message" do
output.puts opt
end
end
next if opts.help?
meth = get_method_or_raise(args.shift, target, opts.to_hash(true))
meth = opts[:method_object]
output.puts unindent <<-EOS
Method Information:
--
@ -95,7 +65,7 @@ class Pry
target = target()
opts = Slop.parse!(args) do |opt|
opts = parse_options!(args, :method_object => true) do |opt|
opt.banner unindent <<-USAGE
Usage: gist-method [OPTIONS] [METH]
Gist the method (doc or source) to github.
@ -104,18 +74,12 @@ class Pry
e.g: gist -d my_method
USAGE
opt.on :m, :method, "Gist a method's source."
opt.on :d, :doc, "Gist a method's documentation."
opt.on :p, :private, "Create a private gist (default: true)", :default => true
opt.on :h, :help, "This message" do
output.puts opt
end
end
next if opts.help?
meth = get_method_or_raise(args.shift, target, opts.to_hash(true))
meth = opts[:method_object]
type_map = { :ruby => "rb", :c => "c", :plain => "plain" }
if !opts.doc?
content = meth.source

View file

@ -6,58 +6,39 @@ class Pry
Introspection = Pry::CommandSet.new do
command "show-method", "Show the source for METH. Type `show-method --help` for more info. Aliases: $, show-source" do |*args|
target = target()
opts = Slop.parse!(args) do |opt|
opts = parse_options!(args, :method_object => true) do |opt|
opt.banner unindent <<-USAGE
Usage: show-method [OPTIONS] [METH 1] [METH 2] [METH N]
Usage: show-method [OPTIONS] [METH]
Show the source for method METH. Tries instance methods first and then methods by default.
e.g: show-method hello_method
USAGE
opt.on :l, "line-numbers", "Show line numbers."
opt.on :b, "base-one", "Show line numbers but start numbering at 1 (useful for `amend-line` and `play` commands)."
opt.on :M, "instance-methods", "Operate on instance methods."
opt.on :m, :methods, "Operate on methods."
opt.on :f, :flood, "Do not use a pager to view text longer than one screen."
opt.on :c, :context, "Select object context to run under.", true do |context|
target = Pry.binding_for(target.eval(context))
end
opt.on :h, :help, "This message." do
output.puts opt
end
end
next if opts.help?
opts[:instance] = opts['instance-methods'] if opts.m?
args = [nil] if args.empty?
args.each do |method_name|
begin
meth = get_method_or_raise(method_name, target, opts.to_hash(true))
rescue CommandError => e
puts "\nError: #{e.message}"
next
end
next unless meth.source
meth = opts[:method_object]
output.puts make_header(meth)
if Pry.color
code = CodeRay.scan(meth.source, meth.source_type).term
else
code = meth.source
end
raise CommandError, "Could not find method source" unless meth.source
start_line = false
if opts.b?
start_line = 1
elsif opts.l?
start_line = meth.source_line || 1
end
output.puts make_header(meth)
render_output(opts.flood?, start_line, code)
code
if Pry.color
code = CodeRay.scan(meth.source, meth.source_type).term
else
code = meth.source
end
start_line = false
if opts.b?
start_line = 1
elsif opts.l?
start_line = meth.source_line || 1
end
render_output(opts.flood?, start_line, code)
end
alias_command "show-source", "show-method"
@ -220,7 +201,7 @@ class Pry
command "edit-method", "Edit a method. Type `edit-method --help` for more info." do |*args|
target = target()
opts = Slop.parse!(args) do |opt|
opts = parse_options!(args, :method_object => true) do |opt|
opt.banner unindent <<-USAGE
Usage: edit-method [OPTIONS] [METH]
Edit the method METH in an editor.
@ -228,14 +209,9 @@ class Pry
e.g: edit-method hello_method
USAGE
opt.on :M, "instance-methods", "Operate on instance methods."
opt.on :m, :methods, "Operate on methods."
opt.on :n, "no-reload", "Do not automatically reload the method's file after editing."
opt.on "no-jump", "Do not fast forward editor to first line of method."
opt.on :p, :patch, "Instead of editing the method's file, try to edit in a tempfile and apply as a monkey patch."
opt.on :c, :context, "Select object context to run under.", true do |context|
target = Pry.binding_for(target.eval(context))
end
opt.on :h, :help, "This message." do
output.puts opt
end
@ -246,7 +222,7 @@ class Pry
raise CommandError, "No editor set!\nEnsure that #{text.bold("Pry.config.editor")} is set to your editor of choice."
end
meth = get_method_or_raise(args.shift, target, opts.to_hash(true))
meth = opts[:method_object]
if opts.p? || meth.dynamically_defined?
lines = meth.source.lines.to_a

View file

@ -1,3 +1,4 @@
require "pry/helpers/base_helpers"
require "pry/helpers/options_helpers"
require "pry/helpers/command_helpers"
require "pry/helpers/text"

View file

@ -2,6 +2,7 @@ class Pry
module Helpers
module CommandHelpers
include OptionsHelpers
module_function

View file

@ -0,0 +1,49 @@
class Pry
module Helpers
module OptionsHelpers
module_function
# Use Slop to parse the arguments given.
#
# @param [Array] mutable list of arguments
# @param [Hash] predefined option types
# @param [&Block] used to add custom arguments.
#
# @return Slop::Options
#
# @option [Boolean] :method_object
# Set to true if you want to get a method object from the user.
#
def parse_options!(args, predefined={}, &block)
Slop.parse!(args) do |opt|
add_method_object_options(opt) if predefined[:method_object]
yield opt
opt.on :h, :help, "This message" do
output.puts opt
end
end.tap do |opts|
process_method_object_options(args, opts) if predefined[:method_object]
end
end
# Add the method object options to an unused Slop instance.
def add_method_object_options(opt)
@method_target = target
opt.on :M, "instance-methods", "Operate on instance methods."
opt.on :m, :methods, "Operate on methods."
opt.on :c, :context, "Select object context to run under.", true do |context|
@method_target = Pry.binding_for(target.eval(context))
end
end
# Add the derived :method_object option to a used Slop instance.
def process_method_object_options(args, opts)
opts[:instance] = opts['instance-methods'] if opts.m?
method_obj = get_method_or_raise(args.empty? ? nil : args.join(" "), @method_target, opts.to_hash(true))
opts.on(:method_object, :default => method_obj)
end
end
end
end

View file

@ -10,16 +10,6 @@ describe "Pry::DefaultCommands::Documentation" do
str_output.string.should =~ /sample doc/
end
it 'should output multiple methods\' documentation' do
str_output = StringIO.new
redirect_pry_io(InputTester.new("show-doc sample_method another_sample_method", "exit-all"), str_output) do
pry
end
str_output.string.should =~ /sample doc/
str_output.string.should =~ /another sample doc/
end
it 'should output a method\'s documentation if inside method without needing to use method name' do
$str_output = StringIO.new

View file

@ -254,16 +254,6 @@ describe "Pry::DefaultCommands::Introspection" do
str_output.string.should =~ /def sample/
end
it 'should output multiple methods\' sources' do
str_output = StringIO.new
redirect_pry_io(InputTester.new("show-method sample_method another_sample_method", "exit-all"), str_output) do
pry
end
str_output.string.should =~ /def sample/
str_output.string.should =~ /def another_sample/
end
it 'should output a method\'s source with line numbers' do
str_output = StringIO.new
redirect_pry_io(InputTester.new("show-method -l sample_method", "exit-all"), str_output) do