Sha256: f637cd5d7267afeca0e8954f6a3f91957b359445be157eb3c56e2d5285332def

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

require 'mixlib/cli'
require 'spanx/helper/exit'

module Spanx
  class CLI
    include Mixlib::CLI
    include Spanx::Helper::Exit
    include Spanx::Helper::Subclassing

    attr_reader :args

    # the first element of ARGV should be a subcommand, which maps to
    # a class in spanx/cli/
    def run(args = ARGV)
      @args = args
      validate!
      Spanx::CLI.subclass_class(args.shift).new.run(args)
    end

    private

    def validate!
      error_exit_with_msg('No command given') if args.empty?
      @command = args.first
      if !@command.eql?('-h') && !@command.eql?('--help')
        error_exit_with_msg("No command found matching #{@command}") unless Spanx::CLI.subclasses.include?(@command)
      else
        help_exit
      end

    end

    def generate_config(argv)
      parse_options argv
      config.merge! Spanx::Config.new(config[:config_file])
      parse_options argv

      if config[:debug]
        STDOUT.sync = true
      end

      Spanx::Logger.enable if config[:debug]
    rescue OptionParser::InvalidOption => e
      error_exit_with_msg "Whoops, #{e.message}"
    end

  end
end

Dir.glob("#{File.expand_path('../cli', __FILE__)}/*.rb").each do |file|
  require file
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
spanx-0.3.0 lib/spanx/cli.rb