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

[ruby/optparse] Define inspect and pretty_inspect

https://github.com/ruby/optparse/commit/a3f0ec21b1
This commit is contained in:
Nobuyoshi Nakada 2022-04-04 15:05:15 +09:00 committed by git
parent 4db75b6fe7
commit de427c3ce0

View file

@ -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.
#