Method: CommandLine#usage
- Defined in:
- lib/cmdline.rb
#usage(mes = "") ⇒ Object
definition
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/cmdline.rb', line 81 def usage mes="" raise mes unless @exit_on_usage # put together usage information for flags. max = 0 #max flag length for formatting has_required=false =false us = @definition.map {|arg_def| if arg_def[FLAG_1] == :command flag=:command elsif arg_def[FLAG_1] == :args flag=:args else # create a usage string of the form: # -f/-flag <value>* must start with a number # for each definition. (commands and args are treated seperately) flag = "#{arg_def[FLAG_1]}" flag << "/#{arg_def[FLAG_2]}" if arg_def[FLAG_2] flag << " <#{arg_def[DISP_NAME]}>" if arg_def[DISP_NAME] flag << "*" if arg_def[REQUIRED] has_required = true if arg_def[REQUIRED] = true max = max > flag.length ? max : flag.length end [flag, arg_def[DOC]] } usage = "usage: #{$0}" if command=@definition.retrieve_flag(:command) fmt = command[REQUIRED] ? " %s" : " [%s]" usage << sprintf(fmt,command[DISP_NAME]) end if usage << (has_required ? " options" : " [options]") end args_doc=false if command=@definition.retrieve_flag(:args) fmt = command[REQUIRED] ? " %s" : " [%s]" usage << sprintf(fmt,command[DISP_NAME]) args_doc = "#{command[DISP_NAME]}: #{command[DOC]}" end usage << "\n" us.each { |arg_def| if arg_def[0].kind_of? String usage << sprintf(" %-#{max}s %s\n", arg_def[0], arg_def[1]) elsif arg_def[0] == :command # command usage << arg_def[1] if arg_def[1] end } @output_io.puts usage @output_io.puts "* required flags" if has_required @output_io.puts "\n#{args_doc}" if args_doc @output_io.puts @output_io.puts mes exit end |