Sha256: 2b176a75b756b6d4d23e63c4d653fc6b1231d2b3d89c1abb348a9daf6335831b

Contents?: true

Size: 1.46 KB

Versions: 4

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module I18nJS
  class CLI
    class Command
      attr_reader :ui, :argv

      def self.command_name(name)
        define_method(:name) { name }
      end

      def self.description(description)
        define_method(:description) { description }
      end

      def self.parse(&block)
        define_method(:parse) do
          OptionParser
            .new {|opts| instance_exec(opts, &block) }
            .parse!(argv)
        end
      end

      def self.command(&block)
        define_method(:command, &block)
      end

      def initialize(argv:, ui:)
        @argv = argv.dup
        @ui = ui
      end

      def call
        parse
        command
      end

      def options
        @options ||= {}
      end

      private def load_require_file!(require_file)
        require_without_warnings(require_file)
      rescue Exception => error # rubocop:disable Lint/RescueException
        ui.stderr_print("=> ERROR: couldn't load",
                        options[:require_file].inspect)
        ui.fail_with(
          "\n#{error_description(error)}\n#{error.backtrace.join("\n")}"
        )
      end

      private def error_description(error)
        [
          error.class.name,
          error.message
        ].reject(&:empty?).join(" => ")
      end

      private def require_without_warnings(path)
        old_verbose = $VERBOSE
        $VERBOSE = nil

        load path
      ensure
        $VERBOSE = old_verbose
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
i18n-js-4.0.1 lib/i18n-js/cli/command.rb
i18n-js-4.0.0 lib/i18n-js/cli/command.rb
i18n-js-4.0.0.alpha5 lib/i18n-js/cli/command.rb
i18n-js-4.0.0.alpha4 lib/i18n-js/cli/command.rb