Sha256: e38c14493eff13e2136709de793248a51ae84b28df1c11a598710adc6b516d27

Contents?: true

Size: 1.71 KB

Versions: 2

Compression:

Stored size: 1.71 KB

Contents

# -*- encoding : utf-8 -*-

require 'optparse'

class Phrase::Tool::Options
  
  def initialize(args, command="")
    @command = command
    @data = {
      default: {
        version: false,
        help: false
      },
      init: {
        secret: "",
        default_locale: "en"
      },
      push: {
        tags: []
      },
      pull: {
      }
    }
    options.parse!(args)
  end
  
  def get(name)
    return @data.fetch(command_name).fetch(name.to_sym)
  rescue => e
    $stderr.puts "Invalid or missing option \"#{name}\" for command \"#{command_name}\""
  end
  
private
  
  def options
    case command_name
      when :init
        OptionParser.new do |opts|
          opts.on("--secret=YOUR_AUTH_TOKEN", String, "Your auth token") do |secret|
            @data[command_name][:secret] = secret
          end
          
          opts.on("--default-locale=en", String, "The default locale for your application") do |default_locale|
            @data[command_name][:default_locale] = default_locale
          end
        end
      when :push
        OptionParser.new do |opts|
          opts.on("--tags=foo,bar", Array, "List of tags for phrase push (separated by comma)") do |tags|
            @data[command_name][:tags] = tags
          end
        end
      else
        OptionParser.new do |opts|
          opts.on_tail("-v", "--version", "Show version number") do |version|
            @data[:default][:version] = true
          end
          
          opts.on_tail("-h", "--help", "Show help") do |help|
            @data[:default][:help] = true
          end
        end
    end
  end
  
  def command_name
    @command_name ||= (@command.present? and @data.has_key?(@command.to_sym)) ? @command.to_sym : :default
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
phrase-0.2.2 lib/phrase/tool/options.rb
phrase-0.2.1 lib/phrase/tool/options.rb