Sha256: e7e9c69d87823075db8711660a471c1c154e0a6b80163eaf7289fe6d53d199ef

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

require 'forwardable'
require 'optparse'
require 'colored2'

require_relative 'parser'
require_relative 'command_finder'
require_relative 'parser_factory'
require_relative '../commands'
require_relative '../output'


module Arli
  module CLI
    class App
      include Arli::Output

      attr_accessor :argv, :config, :command

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

      def start
        if argv.empty?
          factory.default_help
          return
        end

        parse_global_flags
        return if Arli.config.help

        finder = CommandFinder.new(argv, config: config)
        finder.parse!
        return if Arli.config.help

        if finder.command
          self.command = finder.command
          execute!
        else
          factory.default_help
        end
      rescue OptionParser::InvalidOption => e
        report_exception(e, 'Invalid flags or options')
      rescue Arli::Errors::InvalidCommandError => e
        report_exception(e, 'Unknown command')
      rescue Exception => e
        report_exception(e)
      end

      def parse_global_flags
        if argv.first && argv.first.start_with?('-')
          parser = factory.global_parser
          factory.parse_argv(parser, argv)
        end
      end

      def execute!
        if command
          header(command: command) if config.verbose
          command.run
        else
          factory.default_help
        end
      end

      def factory
        Arli::CLI::ParserFactory
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
arli-0.6.1 lib/arli/cli/app.rb