#!/usr/bin/env ruby require "optparse" options = {} OptionParser.new do |opts| opts.banner = "Usage: youdao_fanyi [--key API_Key] [--keyfrom API_KeyFrom] STRING [STRING, ...]" opts.on('--key API_Key', "A 'http://fanyi.youdao.com/fanyiapi?path=data-mode' API key") do |key| options[:key] = key end opts.on('--keyfrom API_KeyFrom', "A 'http://fanyi.youdao.com/fanyiapi?path=data-mode' API keyfrom") do |keyfrom| options[:keyfrom] = keyfrom end opts.on("-v", "--version", "The current version of YoudaoFanyi") do puts "YoudaoFanyi v#{YoudaoFanyi::VERSION}" exit end opts.on("-h", "--help", "This help screen") do puts opts exit end opts.on("-m", "--more", "Display all results.") do options[:more] = true end end.parse! STDERR.puts "fanyi.youdao.com API key is required" unless options[:key] STDERR.puts "fanyi.youdao.com API keyfrom is required" unless options[:keyfrom] STDERR.puts "At least one string to translate is required" unless ARGV.size >= 1 $:.push File.expand_path("../../lib", __FILE__) require "youdao_fanyi" YoudaoFanyi.configure do |config| config.api_keys = [ { "keyfrom" => options[:keyfrom], "key" => options[:key] } ] end YoudaoFanyi::Translator.translate(*ARGV) do |connector| STDOUT.puts "=======================================" if options[:more] require "pp" pp connector.results else require "vkill_gems_methods/std" if connector.error_code == 0 STDOUT.puts VkillGemsMethods::STD::Display.new( :header => :translation, :text => connector.translation, :color => :green, :rjust_integer => 12 ).output STDOUT.puts VkillGemsMethods::STD::Display.new( :header => :phonetic, :text => (connector.results['basic']['phonetic'] rescue ""), :color => :green, :rjust_integer => 12 ).output STDOUT.puts VkillGemsMethods::STD::Display.new( :header => :explains, :text => (connector.results['basic']['explains'] rescue ""), :color => :green, :rjust_integer => 12 ).output else STDOUT.puts VkillGemsMethods::STD::Display.new( :header => :error_code, :text => connector.error_code, :color => :red, :rjust_integer => 12 ).output STDOUT.puts VkillGemsMethods::STD::Display.new( :header => :query, :text => connector.query, :color => :green, :rjust_integer => 12 ).output end end STDOUT.puts "=======================================" end