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

Slightly deshonkify parse_options! API.

This commit is contained in:
Conrad Irwin 2011-10-15 23:20:54 -07:00
parent cddadcb95f
commit 235c55d9b1
3 changed files with 23 additions and 27 deletions

View file

@ -8,9 +8,7 @@ class Pry
end
command "show-doc", "Show the comments above METH. Type `show-doc --help` for more info. Aliases: \?", :shellwords => false do |*args|
target = target()
opts = parse_options!(args, :method_object => true) do |opt|
opts, meth = parse_options!(args, :method_object) do |opt|
opt.banner unindent <<-USAGE
Usage: show-doc [OPTIONS] [METH]
Show the comments above method METH. Tries instance methods first and then methods by default.
@ -20,7 +18,6 @@ class Pry
opt.on :f, :flood, "Do not use a pager to view text longer than one screen."
end
meth = opts[:method_object]
raise Pry::CommandError, "No documentation found." if meth.doc.nil? || meth.doc.empty?
doc = process_comment_markup(meth.doc, meth.source_type)
@ -36,7 +33,7 @@ class Pry
command "stat", "View method information and set _file_ and _dir_ locals. Type `stat --help` for more info.", :shellwords => false do |*args|
target = target()
opts = parse_options!(args, :method_object => true) do |opt|
opts, meth = parse_options!(args, :method_object) do |opt|
opt.banner unindent <<-USAGE
Usage: stat [OPTIONS] [METH]
Show method information for method METH and set _file_ and _dir_ locals.
@ -44,7 +41,6 @@ class Pry
USAGE
end
meth = opts[:method_object]
output.puts unindent <<-EOS
Method Information:
--
@ -63,7 +59,7 @@ class Pry
target = target()
opts = parse_options!(args, :method_object => true) do |opt|
opts, meth = parse_options!(args, :method_object) do |opt|
opt.banner unindent <<-USAGE
Usage: gist-method [OPTIONS] [METH]
Gist the method (doc or source) to github.
@ -76,7 +72,6 @@ class Pry
opt.on :p, :private, "Create a private gist (default: true)", :default => true
end
meth = opts[:method_object]
type_map = { :ruby => "rb", :c => "c", :plain => "plain" }
if !opts.doc?
content = meth.source

View file

@ -6,7 +6,7 @@ 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", :shellwords => false do |*args|
opts = parse_options!(args, :method_object => true) do |opt|
opts, meth = parse_options!(args, :method_object) do |opt|
opt.banner unindent <<-USAGE
Usage: show-method [OPTIONS] [METH]
Show the source for method METH. Tries instance methods first and then methods by default.
@ -18,8 +18,6 @@ class Pry
opt.on :f, :flood, "Do not use a pager to view text longer than one screen."
end
meth = opts[:method_object]
raise CommandError, "Could not find method source" unless meth.source
output.puts make_header(meth)
@ -200,7 +198,7 @@ class Pry
command "edit-method", "Edit a method. Type `edit-method --help` for more info.", :shellwords => false do |*args|
target = target()
opts = parse_options!(args, :method_object => true) do |opt|
opts, meth = parse_options!(args, :method_object) do |opt|
opt.banner unindent <<-USAGE
Usage: edit-method [OPTIONS] [METH]
Edit the method METH in an editor.
@ -220,8 +218,6 @@ class Pry
raise CommandError, "No editor set!\nEnsure that #{text.bold("Pry.config.editor")} is set to your editor of choice."
end
meth = opts[:method_object]
if opts.p? || meth.dynamically_defined?
lines = meth.source.lines.to_a

View file

@ -5,27 +5,32 @@ class Pry
# 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.
# @param [Array] args The options are stripped out by Slop.
# @param [*Symbol] extras Extra features you want returned.
# @param [&Block] used to add custom arguments to Slop.
#
# @return Slop::Options
# @option [Extra] :method_object Returns a method object.
#
# @option [Boolean] :method_object
# Set to true if you want to get a method object from the user.
# @return Slop::Options iff you don't pass any extras.
# @return [Array] If you do pass extras, an array is returned where the first argument is the
# Slop::Options object, and the remainder are the extras you requested in order.
#
def parse_options!(args, predefined={}, &block)
Slop.parse!(args) do |opt|
add_method_object_options(opt) if predefined[:method_object]
def parse_options!(args, *extras, &block)
opts = Slop.parse!(args) do |opt|
extras.each{ |extra| send(:"add_#{extra}_options", opt) }
yield opt
opt.on :h, :help, "This message" do
output.puts opt
throw :command_done
end
end
end.tap do |opts|
process_method_object_options(args, opts) if predefined[:method_object]
if extras.empty?
opts
else
[opts] + extras.map{ |extra| send(:"process_#{extra}_options", args, opts) }
end
end
@ -45,8 +50,8 @@ class Pry
opts[:instance] = opts['instance-methods'] if opts.m?
# TODO: de-hack when we upgrade Slop: https://github.com/injekt/slop/pull/30
opts.options[:super].force_argument_value opts.options[:super].count if opts.super?
method_obj = get_method_or_raise(args.empty? ? nil : args.join(" "), @method_target, opts.to_hash(true))
opts.on(:method_object, :default => method_obj)
get_method_or_raise(args.empty? ? nil : args.join(" "), @method_target, opts.to_hash(true))
end
end
end