class Sanzang::Command::SanzangCmd

This class provides a frontend for all Sanzang operations and subcommands.

Attributes

name[R]

Name of the command

Public Class Methods

new() click to toggle source

Create a new instance of the sanzang command

# File lib/sanzang/command/sanzang_cmd.rb, line 28
def initialize
  @name = "sanzang"
  @commands = [
    ["batch", Sanzang::Command::Batch],
    ["reflow", Sanzang::Command::Reflow],
    ["translate", Sanzang::Command::Translate]
  ]
end

Public Instance Methods

platform_info() click to toggle source

A string giving a listing of platform information

# File lib/sanzang/command/sanzang_cmd.rb, line 74
def platform_info
  require 'parallel'

  info = "host_arch = #{Sanzang::Platform.machine_arch}\n"
  info << "host_os = #{Sanzang::Platform.os_name}\n"
  info << "host_processors = #{Sanzang::Platform.processor_count}\n"
  info << "ruby_encoding_ext = #{Encoding.default_external}\n"
  info << "ruby_encoding_int = #{Encoding.default_internal or 'none'}\n"
  info << "ruby_multiproc = #{Sanzang::Platform.unix_processes?}\n"
  info << "ruby_platform = #{RUBY_PLATFORM}\n"
  info << "ruby_version = #{RUBY_VERSION}p#{RUBY_PATCHLEVEL}\n"
  info << "sanzang_encoding = #{Sanzang::Platform.data_encoding}\n"
  info << "sanzang_parallel = #{Parallel::VERSION}\n"
  info << "sanzang_version = #{Sanzang::VERSION}\n"
end
run(args) click to toggle source

Run the sanzang command with the given arguments. If the first argument is the name of a sanzang subcommand or the beginning of a subcommand, then that subcommand is executed. The sanzang command also accepts several options such as showing usage and platform information.

# File lib/sanzang/command/sanzang_cmd.rb, line 42
def run(args)
  parser = option_parser

  if args.length < 1
    $stderr.puts parser
    return 1
  end

  @commands.each do |key,cmd|
    if key.start_with?(args[0])
      return cmd.new.run(args[1..-1])
    end
  end

  parser.parse!(args)

  $stderr.puts parser
  return 1
rescue SystemExit => err
  return err.status
rescue Interrupt
  puts
  return 0
rescue Errno::EPIPE
  return 0
rescue Exception => err
  $stderr.puts "#{@name}: #{err.inspect}"
  return 1
end
version_info() click to toggle source

This is a string giving a brief one-line summary of version information

# File lib/sanzang/command/sanzang_cmd.rb, line 92
    def version_info
      "sanzang #{Sanzang::VERSION} ruby-#{RUBY_VERSION} #{RUBY_PLATFORM} " \
        + "(#{Sanzang::Platform.data_encoding})"
#     "sanzang #{Sanzang::VERSION} #{Sanzang::Platform.data_encoding}"
    end

Private Instance Methods

option_parser() click to toggle source

An OptionParser object for parsing command options and parameters

# File lib/sanzang/command/sanzang_cmd.rb, line 106
def option_parser
  OptionParser.new do |op|
    op.banner = "Usage: #{@name} [options]\n"
    op.banner << "Usage: #{@name} <command> [options] [args]\n"

    op.banner << "\nUse \"-h\" or \"--help\" with sanzang commands for "
    op.banner << "usage information.\n"

    op.banner << "\nSanzang commands:\n"
    op.banner << "    batch       translate many files in parallel\n"    
    op.banner << "    reflow      format CJK text for translation\n"
    op.banner << "    translate   standard single text translation\n"

    op.banner << "\nOptions:\n"
    op.on("-h", "--help", "show this help message and exit") do |v|
      puts op
      exit 0
    end
    op.on("-P", "--platform", "show platform information and exit") do |v|
      puts platform_info
      exit 0
    end
    op.on("-V", "--version", "show version number and exit") do |v|
      puts version_info
      exit 0
    end
  end
end