Fix error handling and option parsing.

This commit is contained in:
Sebastian Celis 2012-02-10 18:49:52 -06:00
parent ee629a0aaf
commit b5ab3249a9
4 changed files with 29 additions and 45 deletions

View file

@ -1,3 +1,8 @@
#!/usr/bin/env ruby
require 'twine'
Twine::Runner.run(ARGV)
begin
Twine::Runner.run(ARGV)
rescue Twine::Error => e
STDERR.puts e.message
abort
end

View file

@ -12,7 +12,7 @@ module Twine
end
def parse_args
parser = OptionParser.new(@args) do |opts|
parser = OptionParser.new do |opts|
opts.banner = 'Usage: twine COMMAND STRINGS_FILE [INPUT_OR_OUTPUT_PATH] [--lang LANG1,LANG2...] [--tag TAG1,TAG2,TAG3...] [--format FORMAT]'
opts.separator ''
opts.separator 'The purpose of this script is to convert back and forth between multiple data formats, allowing us to treat our strings (and translations) as data stored in a text file. We can then use the data file to create drops for the localization team, consume similar drops returned by the localization team, generate reports on the strings, as well as create iOS and Android string files to ship with our products.'
@ -49,7 +49,7 @@ module Twine
end
end
if !found_format
puts "Invalid format: #{format}"
STDERR.puts "Invalid format: #{format}"
end
@options[:format] = lformat
end
@ -77,7 +77,7 @@ module Twine
opts.separator '> twine consume-loc-drop strings.txt LocDrop5.zip'
opts.separator '> twine generate-report strings.txt'
end
parser.parse!
parser.parse! @args
if @args.length == 0
puts parser.help
@ -87,13 +87,11 @@ module Twine
@options[:command] = @args[0]
if !VALID_COMMANDS.include? @options[:command]
puts "Invalid command: #{@options[:command]}"
exit
raise Twine::Error.new "Invalid command: #{@options[:command]}"
end
if @args.length == 1
puts 'You must specify your strings file.'
exit
raise Twine::Error.new 'You must specify your strings file.'
end
@options[:strings_file] = @args[1]
@ -103,68 +101,54 @@ module Twine
if @args.length == 3
@options[:output_path] = @args[2]
elsif @args.length > 3
puts "Unknown argument: #{@args[3]}"
exit
raise Twine::Error.new "Unknown argument: #{@args[3]}"
else
puts 'Not enough arguments.'
exit
raise Twine::Error.new 'Not enough arguments.'
end
if @options[:languages] and @options[:languages].length > 1
puts 'Please only specify a single language for the generate-string-file command.'
exit
raise Twine::Error.new 'Please only specify a single language for the generate-string-file command.'
end
when 'generate-all-string-files'
if ARGV.length == 3
@options[:output_path] = @args[2]
elsif @args.length > 3
puts "Unknown argument: #{@args[3]}"
exit
raise Twine::Error.new "Unknown argument: #{@args[3]}"
else
puts 'Not enough arguments.'
exit
raise Twine::Error.new 'Not enough arguments.'
end
when 'consume-string-file'
if @args.length == 3
@options[:input_path] = @args[2]
elsif @args.length > 3
puts "Unknown argument: #{@args[3]}"
exit
raise Twine::Error.new "Unknown argument: #{@args[3]}"
else
puts 'Not enough arguments.'
exit
raise Twine::Error.new 'Not enough arguments.'
end
if @options[:languages] and @options[:languages].length > 1
puts 'Please only specify a single language for the consume-string-file command.'
exit
raise Twine::Error.new 'Please only specify a single language for the consume-string-file command.'
end
when 'generate-loc-drop'
if @args.length == 3
@options[:output_path] = @args[2]
elsif @args.length > 3
puts "Unknown argument: #{@args[3]}"
exit
raise Twine::Error.new "Unknown argument: #{@args[3]}"
else
puts 'Not enough arguments.'
exit
raise Twine::Error.new 'Not enough arguments.'
end
if !@options[:format]
puts 'You must specify a format.'
exit
raise Twine::Error.new 'You must specify a format.'
end
when 'consume-loc-drop'
if @args.length == 3
@options[:input_path] = @args[2]
elsif @args.length > 3
puts "Unknown argument: #{@args[3]}"
exit
raise Twine::Error.new "Unknown argument: #{@args[3]}"
else
puts 'Not enough arguments.'
exit
raise Twine::Error.new 'Not enough arguments.'
end
when 'generate-report'
if @args.length > 2
puts "Unknown argument: #{@args[2]}"
exit
raise Twine::Error.new "Unknown argument: #{@args[2]}"
end
end
end

View file

@ -17,7 +17,7 @@ module Twine
if @strings.strings_map.include?(key)
@strings.strings_map[key].translations[lang] = value
elsif @options[:consume_all]
puts "Adding new string '#{key}' to strings data file."
STDERR.puts "Adding new string '#{key}' to strings data file."
arr = @strings.sections.select { |s| s.name == 'Uncategorized' }
current_section = arr ? arr[0] : nil
if !current_section
@ -29,7 +29,7 @@ module Twine
@strings.strings_map[key] = current_row
@strings.strings_map[key].translations[lang] = value
else
puts "Warning: '#{key}' not found in strings data file."
STDERR.puts "Warning: '#{key}' not found in strings data file."
end
if !@strings.language_codes.include?(lang)
@strings.add_language_code(lang)

View file

@ -16,13 +16,8 @@ module Twine
def run
# Parse all CLI arguments.
CLI::parse_args(@args, @options)
begin
read_strings_data
execute_command
rescue Twine::Error => e
puts e.message
end
read_strings_data
execute_command
end
def read_strings_data