Class: Transat::Parser
- Inherits:
-
Object
- Object
- Transat::Parser
- Defined in:
- lib/transat/parser.rb
Class Method Summary collapse
Instance Method Summary collapse
- #command(name, klass, options = {}) ⇒ Object
- #command_options_summary(command, message = []) ⇒ Object
- #execute(command, non_options) ⇒ Object
- #help(message) ⇒ Object
-
#initialize(&block) ⇒ Parser
constructor
A new instance of Parser.
- #option(name, options = {}) ⇒ Object
- #parse(args) ⇒ Object
- #parse_and_execute(args = ARGV) ⇒ Object
- #program_name(value = nil) ⇒ Object
- #usage(command = nil) ⇒ Object
- #version(value = nil) ⇒ Object
Constructor Details
#initialize(&block) ⇒ Parser
Returns a new instance of Parser.
83 84 85 86 87 88 89 90 |
# File 'lib/transat/parser.rb', line 83 def initialize(&block) @valid_options, @received_options, @commands = [], {}, {} @option_parser = OptionParser.new command(:help, Transat::HelpCommand) command(:version, Transat::VersionCommand) instance_eval(&block) if block_given? end |
Class Method Details
.parse_and_execute(args = ARGV, &block) ⇒ Object
243 244 245 |
# File 'lib/transat/parser.rb', line 243 def self.parse_and_execute(args=ARGV, &block) self.new(&block).parse_and_execute(args) end |
Instance Method Details
#command(name, klass, options = {}) ⇒ Object
131 132 133 |
# File 'lib/transat/parser.rb', line 131 def command(name, klass, ={}) @commands[name.to_s] = .merge(:class => klass) end |
#command_options_summary(command, message = []) ⇒ Object
221 222 223 224 225 226 227 228 229 |
# File 'lib/transat/parser.rb', line 221 def (command, =[]) = (command[:valid_options] || []).collect { |opt| "--#{opt.to_s.tr('_', '-')}" } @option_parser.top.list.each do |opt| opt.summarize({}, {}, @option_parser.summary_width, @option_parser.summary_width - 1, @option_parser.summary_indent) do |line| << line end if .include?(opt.long.to_s) end end |
#execute(command, non_options) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/transat/parser.rb', line 163 def execute(command, ) @commands.each do |command_name, | command_klass = [:class] aliases = [command_name] aliases += command_klass.aliases if command_klass.respond_to?(:aliases) = {} @received_options.each_pair do |name, value| [name] = value if [:valid_options].include?(name) end if [:valid_options] return command_klass.new(, ).run if aliases.include?(command) end raise UnknownCommand.new(command, self) end |
#help(message) ⇒ Object
135 136 137 |
# File 'lib/transat/parser.rb', line 135 def help() @help = end |
#option(name, options = {}) ⇒ Object
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 |
# File 'lib/transat/parser.rb', line 92 def option(name, ={}) [:long] = name.to_s.gsub("_", "-") unless [:long] @valid_options << name @received_options[name] = nil opt_args = [] opt_args << "-#{[:short]}" if .has_key?(:short) opt_args << "--#{[:long] || name}" opt_args << "=#{[:param_name]}" if .has_key?(:param_name) opt_args << [:message] case [:type] when :int, :integer opt_args << Integer when :float opt_args << Float when nil # NOP else raise ArgumentError, "Option #{name} has a bad :type parameter: #{[:type].inspect}" end if .has_key?(:default) opt_args << "(default: #{[:default]})" @received_options[name] = [:default] end @option_parser.on(*opt_args.compact) do |value| @received_options[name] = value end @option_parser.on_tail('-h', '--help') do raise HelpNeeded, nil end @option_parser.on_tail('-v', '--version') do raise VersionNeeded end end |
#parse(args) ⇒ Object
156 157 158 159 160 161 |
# File 'lib/transat/parser.rb', line 156 def parse(args) = @option_parser.parse(args) command = .shift raise NoCommandGiven unless command return command, end |
#parse_and_execute(args = ARGV) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/transat/parser.rb', line 139 def parse_and_execute(args=ARGV) begin command, = parse(args) execute(command, ) rescue HelpNeeded $stderr.puts usage($!.command) exit 1 rescue VersionNeeded puts "#{program_name} #{version}" exit 0 rescue NoCommandGiven, UnknownOptions, UnknownCommand $stderr.puts "Error: #{$!.}" $stderr.puts usage($!.respond_to?(:command) ? $!.command : nil) exit 1 end end |
#program_name(value = nil) ⇒ Object
231 232 233 |
# File 'lib/transat/parser.rb', line 231 def program_name(value=nil) value ? @program_name = value : @program_name end |
#usage(command = nil) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/transat/parser.rb', line 181 def usage(command=nil) = [] if command then command_klass = @commands[command][:class] help = if command_klass.respond_to?(:aliases) then "#{command} (#{command_klass.aliases.join(", ")})" else "#{command}" end help = "#{help}: #{command_klass.help}" if command_klass.respond_to?(:help) << help << command_klass.detailed_help if command_klass.respond_to?(:detailed_help) << "" << "Valid options:" (@commands[command], ) else << "usage: #{program_name.downcase} <command> [options] [args...]" << "Type '#{program_name.downcase} help <command>' for help on a specific command." << "Type '#{program_name.downcase} version' to get this program's version." << "" << "Available commands are:" @commands.sort.each do |command, | command_klass = [:class] if command_klass.respond_to?(:aliases) then << " #{command} (#{command_klass.aliases.join(", ")})" else << " #{command}" end end if @help << "" << @help end end .map {|line| line.chomp}.join("\n") end |
#version(value = nil) ⇒ Object
235 236 237 238 239 240 241 |
# File 'lib/transat/parser.rb', line 235 def version(value=nil) if value then @version = value.respond_to?(:join) ? value.join(".") : value else @version end end |