class Gingerice::Command

Attributes

args[R]
args_parser[R]
options[R]

Public Class Methods

new(args) click to toggle source
# File lib/gingerice/command.rb, line 10
def initialize(args)
  @args = args
  @args << '-h' if @args.empty?

  @options     = Gingerice::Parser.default_options.merge({ :output => :simple })
  @args_parser = parse_args
end

Public Instance Methods

execute() click to toggle source
# File lib/gingerice/command.rb, line 18
def execute
  if options.has_key?(:show)

    case options[:show]
    when :help
      puts args_parser
    when :version
      puts "Gingerice: #{Gingerice::VERSION}"
    end

  else
    parser_opts = options.select { |k, _| Parser.default_options.keys.include?(k) }
    parser      = Parser.new(parser_opts)
    response    = parser.parse(args.last)

    if options[:output] === :verbose
      ap response
    else
      puts response
    end
  end
end

Protected Instance Methods

parse_args() click to toggle source
# File lib/gingerice/command.rb, line 42
def parse_args
  OptionParser.new do |opt|
    opt.banner = 'Usage: gingerice [options] "some texts"'

    opt.on("--api-endpoint API_ENDPOINT", "Set API endpoint") do |endpoint|
      options[:api_endpoint] = endpoint
    end

    opt.on("--api-version API_VERSION", "Set API version") do |version|
      options[:api_version] = version
    end

    opt.on("--api-key API_KEY", "Set API key") do |api_key|
      options[:api_key] = api_key
    end

    opt.on("--lang LANG", "Set language, currently support 'US' only") do |lang|
      options[:lang] = lang
    end

    opt.on("-v", "--verbose", "Verbose output (deprecated: use --output verbose, instead)") do
      options[:output] = :verbose
    end

    opt.on("-o", "--output OUTPUT", "Output type") do |output|
      case output
      when 'verbose'
        options[:output] = :verbose
      when 'count'
        options[:output] = :count
      end
    end

    opt.on("--version", "Show version") do
      options[:show] = :version
    end

    opt.on_tail("-h", "--help", "Show this message") do
      options[:show] = :help
    end

    opt.parse!(args)
  end
end