From eb0b1f518b417978f7c488d7aef1605d8c1b6b3d Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Tue, 14 Jun 2011 15:13:36 +0200 Subject: [PATCH] feature #661: Add description and exit_with_code method --- src/cli/command_parser.rb | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/cli/command_parser.rb b/src/cli/command_parser.rb index 288f8ed459..03f75fbdb6 100755 --- a/src/cli/command_parser.rb +++ b/src/cli/command_parser.rb @@ -79,6 +79,10 @@ module CommandParser @version = str end + def description(str) + @description = str + end + def set(e, *args, &block) case e when :option @@ -88,6 +92,11 @@ module CommandParser end end + def exit_with_code(code, output=nil) + puts output if output + exit code + end + def command(name, desc, *args_format, &block) cmd = Hash.new cmd[:desc] = desc @@ -106,7 +115,7 @@ module CommandParser end } cmd[:proc] = block - @commands[name] = cmd + @commands[name.to_sym] = cmd end def script(*args_format, &block) @@ -167,6 +176,8 @@ module CommandParser def help puts @usage if @usage puts + puts @description if @description + puts print_options puts print_commands @@ -209,10 +220,9 @@ module CommandParser puts "== Commands" cmd_format5 = "#{' '*3}%s" - cmd_format10 = "#{' '*6}%s" + cmd_format10 = "#{' '*8}%s" @commands.each{ |key,value| - printf cmd_format5, "* #{key}" - puts + printf cmd_format5, "* #{key} " args_str=value[:args_format].collect{ |a| if a.include?(nil) @@ -221,7 +231,7 @@ module CommandParser "<#{a.join("|")}>" end }.join(' ') - printf cmd_format10, "arguments: #{args_str}" + printf "#{args_str}" puts value[:desc].split("\n").each { |l| @@ -244,7 +254,7 @@ module CommandParser puts "== Argument formats" cmd_format5 = "#{' '*3}%s" - cmd_format10 = "#{' '*6}%s" + cmd_format10 = "#{' '*8}%s" @formats.each{ |key,value| printf cmd_format5, "* #{key}" puts