From de427c3ce0368ed075b09dac95f8e8a01fce8673 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Mon, 4 Apr 2022 15:05:15 +0900 Subject: [ruby/optparse] Define `inspect` and `pretty_inspect` https://github.com/ruby/optparse/commit/a3f0ec21b1 --- lib/optparse.rb | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) (limited to 'lib') diff --git a/lib/optparse.rb b/lib/optparse.rb index a61eff30c4..ec37bde3bd 100644 --- a/lib/optparse.rb +++ b/lib/optparse.rb @@ -674,6 +674,29 @@ class OptionParser end end + def pretty_print_contents(q) # :nodoc: + if @block + q.text ":" + @block.source_location.join(":") + ":" + first = false + else + first = true + end + [@short, @long].each do |list| + list.each do |opt| + if first + q.text ":" + first = false + end + q.breakable + q.text opt + end + end + end + + def pretty_print(q) # :nodoc: + q.object_group(self) {pretty_print_contents(q)} + end + # # Switch that takes no arguments. # @@ -693,6 +716,10 @@ class OptionParser def self.pattern Object end + + def pretty_head # :nodoc: + "NoArgument" + end end # @@ -710,6 +737,10 @@ class OptionParser end conv_arg(*parse_arg(arg, &method(:raise))) end + + def pretty_head # :nodoc: + "Required" + end end # @@ -727,6 +758,10 @@ class OptionParser conv_arg(arg) end end + + def pretty_head # :nodoc: + "Optional" + end end # @@ -750,6 +785,10 @@ class OptionParser end val end + + def pretty_head # :nodoc: + "Placed" + end end end @@ -781,6 +820,17 @@ class OptionParser @list = [] end + def pretty_print(q) # :nodoc: + q.group(1, "(", ")") do + @list.each do |sw| + next unless Switch === sw + q.group(1, "(" + sw.pretty_head, ")") do + sw.pretty_print_contents(q) + end + end + end + end + # # See OptionParser.accept. # @@ -1293,6 +1343,29 @@ XXX def help; summarize("#{banner}".sub(/\n?\z/, "\n")) end alias to_s help + def pretty_print(q) # :nodoc: + q.object_group(self) do + first = true + if @stack.size > 2 + @stack.each_with_index do |s, i| + next if i < 2 + next if s.list.empty? + if first + first = false + q.text ":" + end + q.breakable + s.pretty_print(q) + end + end + end + end + + def inspect # :nodoc: + require 'pp' + pretty_print_inspect + end + # # Returns option summary list. # -- cgit v1.2.3