From 235c55d9b1ee6324bc17a7ec85eb8d1238946360 Mon Sep 17 00:00:00 2001 From: Conrad Irwin Date: Sat, 15 Oct 2011 23:20:54 -0700 Subject: [PATCH] Slightly deshonkify parse_options! API. --- lib/pry/default_commands/documentation.rb | 11 +++----- lib/pry/default_commands/introspection.rb | 8 ++---- lib/pry/helpers/options_helpers.rb | 31 +++++++++++++---------- 3 files changed, 23 insertions(+), 27 deletions(-) diff --git a/lib/pry/default_commands/documentation.rb b/lib/pry/default_commands/documentation.rb index d9fc3fda..add44890 100644 --- a/lib/pry/default_commands/documentation.rb +++ b/lib/pry/default_commands/documentation.rb @@ -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 diff --git a/lib/pry/default_commands/introspection.rb b/lib/pry/default_commands/introspection.rb index 401a8300..f81dc67d 100644 --- a/lib/pry/default_commands/introspection.rb +++ b/lib/pry/default_commands/introspection.rb @@ -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 diff --git a/lib/pry/helpers/options_helpers.rb b/lib/pry/helpers/options_helpers.rb index acd72f3f..4e7a2db1 100644 --- a/lib/pry/helpers/options_helpers.rb +++ b/lib/pry/helpers/options_helpers.rb @@ -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