Module: Rabbit::Console
- Defined in:
- lib/rabbit/console.rb
Constant Summary collapse
- @@locale_dir_option_name =
"--locale-dir"
Class Method Summary collapse
- .banner ⇒ Object
- .get_last_name(klass) ⇒ Object
- .guess_default_logger ⇒ Object
- .output_info_and_exit(options, message) ⇒ Object
- .parse!(args, logger = nil) ⇒ Object
- .process_locale_options(args) ⇒ Object
- .setup_common_options(opts, options) ⇒ Object
- .setup_common_options_on_tail(opts, options) ⇒ Object
- .setup_locale_options(opts, options) ⇒ Object
- .setup_logger_options(opts, options) ⇒ Object
Class Method Details
.banner ⇒ Object
43 44 45 |
# File 'lib/rabbit/console.rb', line 43 def _("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_logger ⇒ Object
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(, ) if .logger.is_a?(Logger::STDERR) and .default_logger == .logger print(GLib.locale_from_utf8()) else .logger.info() 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 = OpenStruct.new .logger = logger .default_logger = logger (args) opts = OptionParser.new() do |opts| yield(opts, ) (opts, ) end begin opts.parse!(args) rescue logger.fatal($!.) end [, .logger] end |
.process_locale_options(args) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/rabbit/console.rb', line 47 def (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 (opts, ) opts.separator "" opts.separator _("Common options") (opts, ) (opts, ) (opts, ) 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 (opts, ) opts.on_tail("--help", _("Show this message.")) do output_info_and_exit(, opts.to_s) end opts.on_tail("--version", _("Show version.")) do output_info_and_exit(, "#{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 (opts, ) 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 (opts, ) 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(.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? .logger = .default_logger # logger.error("Unknown logger type: #{t}") else .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(.logger.level)})") do |name| .logger.level = Logger::Severity.level(name) end opts.separator "" end |