alexeyzab/twitter_images

View on GitHub
lib/twitter_images/cli.rb

Summary

Maintainability
A
0 mins
Test Coverage
module TwitterImages
  class CLI
    attr_reader :configuration, :options

    def initialize(argv)
      @argv = argv
      @configuration = Configuration.new
      @options = {}
    end

    def run
      parse_command_line_options
      configuration.prepare(@options)
    end

    private

    def parse_command_line_options
      global_options.parse!(@argv)
      @options[:path] = @argv[0]
      @options[:term] = @argv[1..-2].join
      @options[:amount] = @argv[-1]
    end

    def global_options
      OptionParser.new do |opts|
        opts.banner = "usage: twitter_images [-v | --version] [-h | --help] [options] [path] [search terms] [amount]"

        opts.on("-v", "--version", "Display the version and exit") do
          puts "Version: #{TwitterImages::VERSION}"
          exit
        end

        opts.on("-h", "--help", "Display this help message and exit") do
          puts opts
          exit
        end

        opts.on("-a", "--authorize", "Run this first to authorize the app with Twitter") do
          Authorizer.new.authorize
          exit
        end
      end
    end
  end
end