From aa82a533dc1653cde40323bf73c4758d5d1940c3 Mon Sep 17 00:00:00 2001 From: Javi Fontan Date: Fri, 1 Feb 2013 12:19:08 +0100 Subject: [PATCH] Word wrap options description in CLI help --- src/cli/command_parser.rb | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/src/cli/command_parser.rb b/src/cli/command_parser.rb index e434958d83..a75362b9d3 100644 --- a/src/cli/command_parser.rb +++ b/src/cli/command_parser.rb @@ -635,14 +635,20 @@ module CommandParser end def print_option(o) - opt_format = "#{' '*5}%-25s %s" + opt_format = "#{' '*5}%-25s" str = "" str << o[:short].split(' ').first << ', ' if o[:short] str << o[:large] - printf opt_format, str, o[:description] - puts + params=sprintf(opt_format, str) + + first_line=80-params.length + + description=word_wrap(80-32, o[:description], first_line). + join(("\n"+" "*31)) + + puts "#{params} #{description}" end def print_commands @@ -707,6 +713,30 @@ module CommandParser } end + def word_wrap(size, text, first_size=nil) + output=[] + line="" + if first_size + line_size=first_size + else + line_size=size + end + + text.scan(/[^\s]+/) do |word| + if line.length+word.length+1<=line_size + line+=" #{word}" + else + output<