Sha256: 106cc74a257a29a9ea6c1906c9db4cfbf910904bfbb909de77dc613f8b4b5253

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 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
          factory.global_parser.print_version_copyright
          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) unless config.quiet
          command.run
        else
          factory.default_help
        end
      end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
arli-0.7.0 lib/arli/cli/app.rb
arli-0.6.2 lib/arli/cli/app.rb