Sha256: cb2f8f8cb4fceab4a7473ce6b0701f063f3e60f67380329155a95da146cbf18e

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 KB

Contents

module MDQT

  class CLI

    class Base

      def self.run(args, options)

        #args = options.stdin ? absorb_piped_args(args) : args

        check_requirements(args, options)
        introduce(args, options)

        self.new(args, options).run
      end

      def self.check_requirements(args, options)
        abort "Error: No MDQ service URL has been specified." unless options.service
      end

      def self.introduce(args, options)
        if options.verbose
          STDERR.puts "Using #{options.service}"
          STDERR.puts "Caching is #{options.service ? 'on' : 'off'}"
          STDERR.puts
        end
      end

      def self.absorb_piped_args(args)
        piped = piped? ? parse_stdin : []
        args + piped
      end

      def self.piped?
        !STDIN.tty? && !STDIN.closed?
      end

      def self.parse_stdin
        STDIN.gsub("\s", "\n").each_line.collect {|l| l.strip }
      end

      def initialize(args, options)
        @args = args
        @options = options
      end

      def args=(new_args)
        @args = new_args
      end

      def args
        @args
      end

      def options=(new_options)
        @options = new_options
      end

      def options
        @options
      end

      def output(response)
        if response.ok?
          STDERR.puts response.message if options.verbose
          trailer = response.data[-1] == "\n" ? "" : "\n"
          response.data + trailer
        else
          STDERR.puts response.message
        end

      end

      def run

        abort "No action has been defined for this command!"

      end

    end

  end
#
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mdqt-0.2.1 lib/mdqt/cli/base.rb
mdqt-0.2.0 lib/mdqt/cli/base.rb