Module: Rabbit::Console

Defined in:
lib/rabbit/console.rb

Constant Summary collapse

@@locale_dir_option_name =
"--locale-dir"

Class Method Summary collapse

Class Method Details



43
44
45
# File 'lib/rabbit/console.rb', line 43

def banner
  _("Usage: %s [options]") % File.basename($0, '.*')
end

.get_last_name(klass) ⇒ Object



129
130
131
# File 'lib/rabbit/console.rb', line 129

def get_last_name(klass)
  klass.name.split("::").last
end

.guess_default_loggerObject



133
134
135
136
137
138
139
# File 'lib/rabbit/console.rb', line 133

def guess_default_logger
  if Utils.support_console_output? or !Logger.const_defined?(:GUI)
    Logger::STDERR.new
  else
    Logger::GUI.new
  end
end

.output_info_and_exit(options, message) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/rabbit/console.rb', line 119

def output_info_and_exit(options, message)
  if options.logger.is_a?(Logger::STDERR) and
      options.default_logger == options.logger
    print(GLib.locale_from_utf8(message))
  else
    options.logger.info(message)
  end
  exit
end

.parse!(args, logger = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rabbit/console.rb', line 20

def parse!(args, logger=nil)
  bindtextdomain
  logger ||= guess_default_logger
  options = OpenStruct.new
  options.logger = logger
  options.default_logger = logger

  process_locale_options(args)

  opts = OptionParser.new(banner) do |opts|
    yield(opts, options)
    setup_common_options(opts, options)
  end

  begin
    opts.parse!(args)
  rescue
    logger.fatal($!.message)
  end

  [options, options.logger]
end

.process_locale_options(args) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/rabbit/console.rb', line 47

def process_locale_options(args)
  args.each_with_index do |arg, i|
    if arg == @@locale_dir_option_name
      bindtextdomain(args[i + 1])
    elsif /#{@@locale_dir_option_name}=/ =~ arg
      bindtextdomain($POSTMATCH)
    end
  end
end

.setup_common_options(opts, options) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/rabbit/console.rb', line 57

def setup_common_options(opts, options)
  opts.separator ""
  opts.separator _("Common options")

  setup_locale_options(opts, options)
  setup_logger_options(opts, options)
  setup_common_options_on_tail(opts, options)
end

.setup_common_options_on_tail(opts, options) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/rabbit/console.rb', line 109

def setup_common_options_on_tail(opts, options)
  opts.on_tail("--help", _("Show this message.")) do
    output_info_and_exit(options, opts.to_s)
  end

  opts.on_tail("--version", _("Show version.")) do
    output_info_and_exit(options, "#{VERSION}\n")
  end
end

.setup_locale_options(opts, options) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'lib/rabbit/console.rb', line 66

def setup_locale_options(opts, options)
  opts.on("--locale-dir=DIR",
          _("Specify locale dir as [DIR]."),
          _("(auto)")) do |d|
    bindtextdomain(d)
  end

  opts.separator ""
end

.setup_logger_options(opts, options) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rabbit/console.rb', line 76

def setup_logger_options(opts, options)
  logger_type_names = Rabbit::Logger.types.collect do |x|
    get_last_name(x).downcase
  end

  opts.on("--logger-type=TYPE",
          logger_type_names,
          _("Specify logger type as [TYPE]."),
          _("Select from [%s].") % logger_type_names.join(', '),
          "(#{get_last_name(options.logger.class)})") do |logger_type|
    logger_class = Rabbit::Logger.types.find do |t|
      get_last_name(t).downcase == logger_type.downcase
    end
    if logger_class.nil?
      options.logger = options.default_logger
      # logger.error("Unknown logger type: #{t}")
    else
      options.logger = logger_class.new
    end
  end

  level_names = Logger::Severity.names
  opts.on("--log-level=LEVEL",
          level_names,
          _("Specify log level as [LEVEL]."),
          _("Select from [%s].") % level_names.join(', '),
          "(#{Logger::Severity.name(options.logger.level)})") do |name|
    options.logger.level = Logger::Severity.level(name)
  end

  opts.separator ""
end