Class: Transat::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/transat/parser.rb

Class Method Summary collapse

Instance Method Summary collapse

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, options={})
  @commands[name.to_s] = options.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_options_summary(command, message=[])
  valid_options = (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|
      message << line
    end if valid_options.include?(opt.long.to_s)
  end
  message
end

#execute(command, non_options) ⇒ Object

Raises:



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, non_options)
  @commands.each do |command_name, options|
    command_klass = options[:class]
    
    aliases = [command_name]
    aliases += command_klass.aliases if command_klass.respond_to?(:aliases)
    
    valid_options = {}
    @received_options.each_pair do |name, value|
      valid_options[name] = value if options[:valid_options].include?(name)
    end if options[:valid_options]
    
    return command_klass.new(non_options, valid_options).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(message)
  @help = message
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, options={})
  options[:long] = name.to_s.gsub("_", "-") unless options[:long]
  @valid_options << name
  @received_options[name] = nil

  opt_args = []
  opt_args << "-#{options[:short]}" if options.has_key?(:short)
  opt_args << "--#{options[:long] || name}"
  opt_args << "=#{options[:param_name]}" if options.has_key?(:param_name)
  opt_args << options[:message]
  case options[: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: #{options[:type].inspect}"
  end

  if options.has_key?(:default)
    opt_args << "(default: #{options[:default]})"
    @received_options[name] = options[: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

Raises:



156
157
158
159
160
161
# File 'lib/transat/parser.rb', line 156

def parse(args)
  non_options = @option_parser.parse(args)
  command = non_options.shift
  raise NoCommandGiven unless command
  return command, non_options
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, non_options = parse(args)
    execute(command, non_options)
  rescue HelpNeeded
    $stderr.puts usage($!.command)
    exit 1
  rescue VersionNeeded
    puts "#{program_name} #{version}"
    exit 0
  rescue NoCommandGiven, UnknownOptions, UnknownCommand
    $stderr.puts "Error: #{$!.message}"
    $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)
  message = []

  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)
    message << help
    message << command_klass.detailed_help if command_klass.respond_to?(:detailed_help)
    message << ""
    message << "Valid options:"
    command_options_summary(@commands[command], message)
  else
    message << "usage: #{program_name.downcase} <command> [options] [args...]"
    message << "Type '#{program_name.downcase} help <command>' for help on a specific command."
    message << "Type '#{program_name.downcase} version' to get this program's version."
    message << ""
    message << "Available commands are:"
    @commands.sort.each do |command, options|
      command_klass = options[:class]
      if command_klass.respond_to?(:aliases) then
        message << "  #{command} (#{command_klass.aliases.join(", ")})"
      else
        message << "  #{command}"
      end
    end
    if @help
      message << ""
      message << @help
    end
  end

  message.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