summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2022-04-04 15:05:15 +0900
committergit <svn-admin@ruby-lang.org>2022-04-04 15:05:44 +0900
commitde427c3ce0368ed075b09dac95f8e8a01fce8673 (patch)
treed6c685d8adc2ef641eaf870ce17da66e34cd05ed /lib
parent4db75b6fe7453624bcb2286c4b191b49fc657f2e (diff)
[ruby/optparse] Define `inspect` and `pretty_inspect`
https://github.com/ruby/optparse/commit/a3f0ec21b1
Diffstat (limited to 'lib')
-rw-r--r--lib/optparse.rb73
1 files changed, 73 insertions, 0 deletions
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.
#