Sha256: 5d11e9c9aefa6a9e97c158e1313fb2a16b71ade2803c50d3c094dcd658f37c6b

Contents?: true

Size: 1.29 KB

Versions: 6

Compression:

Stored size: 1.29 KB

Contents

require "optparse"
require "oauth/cli/base_command"
require "oauth/cli/help_command"
require "oauth/cli/query_command"
require "oauth/cli/authorize_command"
require "oauth/cli/sign_command"
require "oauth/cli/version_command"
require "active_support/core_ext/string/inflections"

module OAuth
  class CLI
    def self.puts_red(string)
      puts "\033[0;91m#{string}\033[0m"
    end

    ALIASES = {
      "h" => "help",
      "v" => "version",
      "q" => "query",
      "a" => "authorize",
      "s" => "sign"
    }.freeze

    def initialize(stdout, stdin, stderr, command, arguments)
      klass = get_command_class(parse_command(command))
      @command = klass.new(stdout, stdin, stderr, arguments)
      @help_command = HelpCommand.new(stdout, stdin, stderr, [])
    end

    def run
      @command.run
    end

    private

    def get_command_class(command)
      Object.const_get("OAuth::CLI::#{command.camelize}Command")
    end

    def parse_command(command)
      case command = command.to_s.downcase
      when "--version", "-v"
        "version"
      when "--help", "-h", nil, ""
        "help"
      when *ALIASES.keys
        ALIASES[command]
      when *ALIASES.values
        command
      else
        OAuth::CLI.puts_red "Command '#{command}' not found"
        "help"
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
oauth-0.5.14 lib/oauth/cli.rb
oauth-0.5.13 lib/oauth/cli.rb
oauth-0.5.12 lib/oauth/cli.rb
oauth-0.5.11 lib/oauth/cli.rb
oauth-0.5.10 lib/oauth/cli.rb
oauth-0.5.9 lib/oauth/cli.rb