Sha256: 3a974d4e2da6b8c5a7f3dee3a36bd1d5f632362b7735346f06c41c4828839846

Contents?: true

Size: 1.24 KB

Versions: 4

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

require_relative "../i18n-js"
require_relative "cli/command"
require_relative "cli/ui"
require_relative "cli/init_command"
require_relative "cli/version_command"
require_relative "cli/export_command"
require_relative "cli/check_command"

module I18nJS
  class CLI
    attr_reader :ui

    def initialize(argv:, stdout:, stderr:, colored: stdout.tty?)
      @argv = argv.dup
      @ui = UI.new(stdout: stdout, stderr: stderr, colored: colored)
    end

    def call
      command_name = @argv.shift
      command = commands.find {|cmd| cmd.name == command_name }

      ui.fail_with(root_help) unless command

      command.call
    end

    private def command_classes
      [InitCommand, ExportCommand, VersionCommand, CheckCommand]
    end

    private def commands
      command_classes.map do |command_class|
        command_class.new(argv: @argv, ui: ui)
      end
    end

    private def root_help
      commands_list = commands
                      .map {|cmd| "- #{cmd.name}: #{cmd.description}" }
                      .join("\n")

      <<~TEXT
        Usage: i18n COMMAND FLAGS

        Commands:

        #{commands_list}

        Run `i18n COMMAND --help` for more information on specific commands.
      TEXT
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

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