Class: I18n::Tasks::CLI

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/i18n/tasks/cli.rb

Defined Under Namespace

Classes: ExecutionError

Constant Summary

Constants included from Logging

Logging::MUTEX, Logging::PROGRAM_NAME

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

log_error, log_stderr, log_verbose, log_warn, program_name, warn_deprecated

Constructor Details

#initializeCLI

Returns a new instance of CLI.



14
# File 'lib/i18n/tasks/cli.rb', line 14

def initialize; end

Class Method Details

.start(argv) ⇒ Object



10
11
12
# File 'lib/i18n/tasks/cli.rb', line 10

def self.start(argv)
  new.start(argv)
end

Instance Method Details

#commandsObject



59
60
61
62
63
# File 'lib/i18n/tasks/cli.rb', line 59

def commands
  # load base task to initialize plugins
  base_task
  @commands ||= ::I18n::Tasks::Commands.cmds.transform_keys { |k| k.to_s.tr('_', '-') }
end

#contextObject



55
56
57
# File 'lib/i18n/tasks/cli.rb', line 55

def context
  @context ||= ::I18n::Tasks::Commands.new(base_task)
end

#run(argv) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/i18n/tasks/cli.rb', line 36

def run(argv)
  argv.each_with_index do |arg, i|
    next unless ['--config', '-c'].include?(arg)

    _, config_file = argv.slice!(i, 2)
    if File.exist?(config_file)
      @config_file = config_file
      break
    else
      error "Config file doesn't exist: #{config_file}", 128
    end
  end

  I18n.with_locale(base_task(config_file: @config_file).internal_locale) do
    name, *options = parse!(argv.dup)
    context.run(name, *options)
  end
end

#start(argv) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/i18n/tasks/cli.rb', line 16

def start(argv)
  auto_output_coloring do
    exit 1 if run(argv) == :exit1
  rescue OptionParser::ParseError => e
    error e.message, 64
  rescue I18n::Tasks::CommandError => e
    begin
      error e.message, 78
    ensure
      log_verbose e.backtrace * "\n"
    end
  rescue Errno::EPIPE
    # ignore Errno::EPIPE which is throw when pipe breaks, e.g.:
    # i18n-tasks missing | head
    exit 1
  end
rescue ExecutionError => e
  exit e.exit_code
end