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
- #execute(command, non_options) ⇒ 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.
62 63 64 65 66 67 68 69 |
# File 'lib/transat/parser.rb', line 62 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
185 186 187 |
# File 'lib/transat/parser.rb', line 185 def self.parse_and_execute(args=ARGV, &block) self.new(&block).parse_and_execute(args) end |
Instance Method Details
#command(name, klass, options = {}) ⇒ Object
97 98 99 |
# File 'lib/transat/parser.rb', line 97 def command(name, klass, ={}) @commands[name.to_s] = .merge(:class => klass) end |
#execute(command, non_options) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/transat/parser.rb', line 125 def execute(command, ) found = false @commands.each do |command_name, | command_klass = [:class] aliases = [command_name] aliases += command_klass.aliases if command_klass.respond_to?(:aliases) return command_klass.new(, @received_options).run if aliases.include?(command) end raise UnknownCommand.new(command, self) end |
#option(name, options = {}) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/transat/parser.rb', line 71 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 @option_parser.on(*opt_args.compact) do |value| @received_options[name] = value end end |
#parse(args) ⇒ Object
118 119 120 121 122 123 |
# File 'lib/transat/parser.rb', line 118 def parse(args) = @option_parser.parse(args) command = .shift raise NoCommandGiven unless command return command, end |
#parse_and_execute(args = ARGV) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/transat/parser.rb', line 101 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
173 174 175 |
# File 'lib/transat/parser.rb', line 173 def program_name(value=nil) value ? @program_name = value : @program_name end |
#usage(command = nil) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/transat/parser.rb', line 137 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:" @option_parser.summarize() else << "usage: #{program_name.downcase} <SUBCOMMAND> [OPTIONS] [ARGS...]" << "Type '#{program_name.downcase} help <SUBCOMMAND>' for help on a specific subcommand." << "Type '#{program_name.downcase} version' to get this program's version." << "" << "Available subcommands 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 end .map {|line| line.chomp}.join("\n") end |
#version(value = nil) ⇒ Object
177 178 179 180 181 182 183 |
# File 'lib/transat/parser.rb', line 177 def version(value=nil) if value then @version = value.respond_to?(:join) ? value.join(".") : value else @version end end |