[前][次][番号順一覧][スレッド一覧]

ruby-changes:71618

From: Nobuyoshi <ko1@a...>
Date: Mon, 4 Apr 2022 15:06:00 +0900 (JST)
Subject: [ruby-changes:71618] de427c3ce0 (master): [ruby/optparse] Define `inspect` and `pretty_inspect`

https://git.ruby-lang.org/ruby.git/commit/?id=de427c3ce0

From de427c3ce0368ed075b09dac95f8e8a01fce8673 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
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(+)

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 https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L674
       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 https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L716
       def self.pattern
         Object
       end
+
+      def pretty_head           # :nodoc:
+        "NoArgument"
+      end
     end
 
     #
@@ -710,6 +737,10 @@ class OptionParser https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L737
         end
         conv_arg(*parse_arg(arg, &method(:raise)))
       end
+
+      def pretty_head           # :nodoc:
+        "Required"
+      end
     end
 
     #
@@ -727,6 +758,10 @@ class OptionParser https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L758
           conv_arg(arg)
         end
       end
+
+      def pretty_head           # :nodoc:
+        "Optional"
+      end
     end
 
     #
@@ -750,6 +785,10 @@ class OptionParser https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L785
         end
         val
       end
+
+      def pretty_head           # :nodoc:
+        "Placed"
+      end
     end
   end
 
@@ -781,6 +820,17 @@ class OptionParser https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L820
       @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 https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L1343
   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.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]