Sha256: bba6cf8bfdb4654093ffdbc0bd0243fc1f5b36d3d7b4eb6a18a640a1efd01975

Contents?: true

Size: 1.04 KB

Versions: 8

Compression:

Stored size: 1.04 KB

Contents

require 'trollop'
require 'stastic/config'
require 'stastic/client'
require 'stastic/commands/base'
Dir["#{File.dirname(__FILE__)}/commands/*.rb"].each { |c| require c }

module Stastic
  module Command
    extend self

    class UnknownCommand < RuntimeError; end
    class InvalidOptions < RuntimeError; end


    def run(cmd, args)
      begin
        klass, method = parse_command(cmd)
        runner = klass.new(args)
        raise UnknownCommand unless runner.respond_to?(method)
        runner.send(method)
      rescue UnknownCommand
        puts "Invalid command `#{cmd}`. Run `stastic help` for available commands."
      rescue InvalidOptions
        puts "Invalid options for `#{cmd}`. Run `stastic help` for more information."
      end
    end

    private

      def parse_command(cmd)
        begin
          klass, method = cmd.split(':')
          return eval("Stastic::Command::#{klass.capitalize}"), method || :index
        rescue NameError, NoMethodError
          puts $!.inspect
          raise UnknownCommand
        end
      end

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
stastic-0.2.7 lib/stastic/command.rb
stastic-0.2.6 lib/stastic/command.rb
stastic-0.2.5 lib/stastic/command.rb
stastic-0.2.4 lib/stastic/command.rb
stastic-0.2.3 lib/stastic/command.rb
stastic-0.2.2 lib/stastic/command.rb
stastic-0.2.1 lib/stastic/command.rb
stastic-0.2.0 lib/stastic/command.rb