@@ -10,28 +10,29 @@ def initialize(command_class, command_path: [])
1010 end
1111
1212 def format
13- lines = [ ]
14- lines << description_section if command_class . description . present?
15- lines << version_section if command_class . respond_to? ( :version ) && command_class . version
16- lines << usage_section
17- lines << subcommands_section if command_class . subcommands . any?
18- lines << options_section if command_class . option_definitions . any?
19- lines << arguments_section if command_class . argument_definitions . any?
20- lines . compact . join ( "\n \n " ) + "\n "
13+ [
14+ description_section ,
15+ version_section ,
16+ usage_section ,
17+ subcommands_section ,
18+ options_section ,
19+ arguments_section
20+ ] . compact . join ( "\n \n " ) + "\n "
2121 end
2222
2323 private
2424
2525 def description_section
26- command_class . description
26+ desc = command_class . description
27+ desc if desc . present?
2728 end
2829
2930 def version_section
30- "Version: #{ command_class . version . to_s . light_blue } "
31+ ver = command_class . respond_to? ( :version ) && command_class . version
32+ "Version: #{ ver . to_s . light_blue } " if ver
3133 end
3234
3335 def usage_section
34- parts = [ "Usage:" . light_cyan ]
3536 path = command_path . any? ? command_path . join ( " " ) : command_class . command_name . to_s
3637 usage = path . light_red
3738 usage += " [options]" . light_cyan if command_class . option_definitions . any?
@@ -40,13 +41,12 @@ def usage_section
4041 label = arg . required ? "<#{ arg . name } >" : "[#{ arg . name } ]"
4142 usage += " #{ label } " . light_yellow
4243 end
43- parts << usage
44- parts . join ( " " )
44+ "Usage:" . light_cyan + " " + usage
4545 end
4646
4747 def subcommands_section
4848 subs = command_class . subcommands
49- return nil if subs . empty?
49+ return if subs . empty?
5050
5151 max_width = subs . keys . map { |k | k . to_s . length } . max
5252 Util ::Formatting . reset_colors!
@@ -68,13 +68,13 @@ def subcommands_section
6868
6969 def options_section
7070 opts = command_class . option_definitions
71- return nil if opts . empty?
71+ return if opts . empty?
7272
73- max_width = opts . values . map { |o | option_signature ( o ) . length } . max
73+ max_width = opts . values . map { |o | o . signature . length } . max
7474
7575 lines = [ "Options:" . light_cyan ]
7676 opts . each_value do |opt |
77- sig = option_signature ( opt ) . ljust ( max_width + 2 )
77+ sig = opt . signature . ljust ( max_width + 2 )
7878 desc = opt . description || ""
7979 default_note = opt . default_value ? " (default: #{ opt . default_value } )" . light_black : ""
8080 lines << " #{ sig . light_green } #{ desc } #{ default_note } "
@@ -84,7 +84,7 @@ def options_section
8484
8585 def arguments_section
8686 args = command_class . argument_definitions
87- return nil if args . empty?
87+ return if args . empty?
8888
8989 max_width = args . map { |a | a . name . to_s . length } . max
9090
@@ -97,14 +97,5 @@ def arguments_section
9797 end
9898 lines . join ( "\n " )
9999 end
100-
101- def option_signature ( opt )
102- parts = [ ]
103- parts << "-#{ opt . short } ," if opt . short
104- long = "--#{ opt . name . to_s . tr ( '_' , '-' ) } "
105- long += "=VALUE" unless opt . boolean?
106- parts << long
107- parts . join ( " " )
108- end
109100 end
110101end
0 commit comments