Skip to content

Commit a3f0ec2

Browse files
committed
Define inspect and pretty_inspect
1 parent 482a426 commit a3f0ec2

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

lib/optparse.rb

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,29 @@ def compsys(sdone, ldone) # :nodoc:
674674
end
675675
end
676676

677+
def pretty_print_contents(q) # :nodoc:
678+
if @block
679+
q.text ":" + @block.source_location.join(":") + ":"
680+
first = false
681+
else
682+
first = true
683+
end
684+
[@short, @long].each do |list|
685+
list.each do |opt|
686+
if first
687+
q.text ":"
688+
first = false
689+
end
690+
q.breakable
691+
q.text opt
692+
end
693+
end
694+
end
695+
696+
def pretty_print(q) # :nodoc:
697+
q.object_group(self) {pretty_print_contents(q)}
698+
end
699+
677700
#
678701
# Switch that takes no arguments.
679702
#
@@ -693,6 +716,10 @@ def self.incompatible_argument_styles(*)
693716
def self.pattern
694717
Object
695718
end
719+
720+
def pretty_head # :nodoc:
721+
"NoArgument"
722+
end
696723
end
697724

698725
#
@@ -710,6 +737,10 @@ def parse(arg, argv)
710737
end
711738
conv_arg(*parse_arg(arg, &method(:raise)))
712739
end
740+
741+
def pretty_head # :nodoc:
742+
"Required"
743+
end
713744
end
714745

715746
#
@@ -727,6 +758,10 @@ def parse(arg, argv, &error)
727758
conv_arg(arg)
728759
end
729760
end
761+
762+
def pretty_head # :nodoc:
763+
"Optional"
764+
end
730765
end
731766

732767
#
@@ -750,6 +785,10 @@ def parse(arg, argv, &error)
750785
end
751786
val
752787
end
788+
789+
def pretty_head # :nodoc:
790+
"Placed"
791+
end
753792
end
754793
end
755794

@@ -781,6 +820,17 @@ def initialize
781820
@list = []
782821
end
783822

823+
def pretty_print(q) # :nodoc:
824+
q.group(1, "(", ")") do
825+
@list.each do |sw|
826+
next unless Switch === sw
827+
q.group(1, "(" + sw.pretty_head, ")") do
828+
sw.pretty_print_contents(q)
829+
end
830+
end
831+
end
832+
end
833+
784834
#
785835
# See OptionParser.accept.
786836
#
@@ -1293,6 +1343,29 @@ def summarize(to = [], width = @summary_width, max = width - 1, indent = @summar
12931343
def help; summarize("#{banner}".sub(/\n?\z/, "\n")) end
12941344
alias to_s help
12951345

1346+
def pretty_print(q) # :nodoc:
1347+
q.object_group(self) do
1348+
first = true
1349+
if @stack.size > 2
1350+
@stack.each_with_index do |s, i|
1351+
next if i < 2
1352+
next if s.list.empty?
1353+
if first
1354+
first = false
1355+
q.text ":"
1356+
end
1357+
q.breakable
1358+
s.pretty_print(q)
1359+
end
1360+
end
1361+
end
1362+
end
1363+
1364+
def inspect # :nodoc:
1365+
require 'pp'
1366+
pretty_print_inspect
1367+
end
1368+
12961369
#
12971370
# Returns option summary list.
12981371
#

0 commit comments

Comments
 (0)