Sha256: f9d736ff25d1831cde8690df6912078020276d6c8e4535b3bbde40d5dc541608

Contents?: true

Size: 1.73 KB

Versions: 3

Compression:

Stored size: 1.73 KB

Contents

require 'forwardable'
require 'optparse'

require 'colored2'

require_relative 'parser'
require_relative 'parser_factory'
require_relative '../commands'
require_relative '../commands/search'
require_relative '../commands/install'
require_relative '../output'

module Arli
  module CLI
    class CommandFinder

      include Arli::Output

      attr_accessor :argv, :config, :command_name, :command

      def initialize(argv, config: Arli.config)
        self.config = config
        self.argv   = argv
      end

      def parse!
        self.command_name = detect_command
        parse_command_arguments!(command_name)
        unless Arli.config.help
          self.command = instantiate_command
          if self.command
            config.runtime.command.instance = command
            config.runtime.command.name     = command_name
          end
        end
        self
      end

      def parse_command_arguments!(cmd)
        parser = factory.command_parser(cmd)
        if parser
          factory.parse_argv(parser, argv)
        end
      end

      def instantiate_command
        self.command_name = detect_command unless command_name
        begin
          name          = command_name.to_s.capitalize.to_sym
          command_class = ::Arli::Commands.const_get(name)
          raise_invalid_arli_command!(command_name) unless command_class
          command_class.new(config: config) if command_class
        end
      end

      def detect_command
        return nil unless argv.first && argv.first !~ /^-.*$/
        cmd = argv.shift.to_sym
        if factory.valid_command?(cmd)
          cmd
        else
          raise_invalid_arli_command!(cmd)
        end
      end

      def factory
        Arli::CLI::ParserFactory
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
arli-0.7.0 lib/arli/cli/command_finder.rb
arli-0.6.2 lib/arli/cli/command_finder.rb
arli-0.6.1 lib/arli/cli/command_finder.rb