Class: Coral::Util::CLI::Parser
- Inherits:
-
Object
- Object
- Coral::Util::CLI::Parser
- Defined in:
- lib/coral_core/util/cli.rb
Overview
Parser
Instance Attribute Summary collapse
-
#arguments ⇒ Object
Returns the value of attribute arguments.
-
#options ⇒ Object
Returns the value of attribute options.
-
#parser ⇒ Object
Returns the value of attribute parser.
-
#processed ⇒ Object
Returns the value of attribute processed.
Class Method Summary collapse
Instance Method Summary collapse
-
#arg(name, default, allowed_values, message_id, config = {}, &block) ⇒ Object
—.
-
#arg_array(name, default, message_id, config = {}) ⇒ Object
—.
-
#arg_bool(name, default, message_id, config = {}) ⇒ Object
—.
-
#arg_float(name, default, message_id, config = {}) ⇒ Object
—.
-
#arg_int(name, default, message_id, config = {}) ⇒ Object
—.
-
#arg_str(name, default, message_id, config = {}) ⇒ Object
—.
-
#banner=(banner) ⇒ Object
—.
-
#help=(help) ⇒ Object
—.
-
#initialize(args, banner = '', help = '') {|_self| ... } ⇒ Parser
constructor
—.
-
#option(name, default, option_str, allowed_values, message_id, config = {}) ⇒ Object
—.
-
#option_array(name, default, option_str, message_id, config = {}) ⇒ Object
—.
-
#option_bool(name, default, option_str, message_id, config = {}) ⇒ Object
—.
-
#option_float(name, default, option_str, message_id, config = {}) ⇒ Object
—.
-
#option_int(name, default, option_str, message_id, config = {}) ⇒ Object
—.
-
#option_str(name, default, option_str, message_id, config = {}) ⇒ Object
—.
-
#parse_command(args) ⇒ Object
—.
Constructor Details
#initialize(args, banner = '', help = '') {|_self| ... } ⇒ Parser
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/coral_core/util/cli.rb', line 27 def initialize(args, = '', help = '') @parser = OptionParser.new self. = {} self.arguments = {} self.processed = false @arg_settings = [] self. = self.help = help yield(self) if block_given? parse_command(args) end |
Instance Attribute Details
#arguments ⇒ Object
Returns the value of attribute arguments.
22 23 24 |
# File 'lib/coral_core/util/cli.rb', line 22 def arguments @arguments end |
#options ⇒ Object
Returns the value of attribute options.
21 22 23 |
# File 'lib/coral_core/util/cli.rb', line 21 def @options end |
#parser ⇒ Object
Returns the value of attribute parser.
20 21 22 |
# File 'lib/coral_core/util/cli.rb', line 20 def parser @parser end |
#processed ⇒ Object
Returns the value of attribute processed.
23 24 25 |
# File 'lib/coral_core/util/cli.rb', line 23 def processed @processed end |
Class Method Details
.split(args, banner, separator = '') ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/coral_core/util/cli.rb', line 47 def self.split(args, , separator = '') main_args = nil sub_command = nil sub_args = [] args.each_index do |index| if !args[index].start_with?('-') main_args = args[0, index] sub_command = args[index] sub_args = args[index + 1, args.length - index + 1] break end end main_args = args.dup if main_args.nil? results = [ Parser.new(main_args, , separator) ] if sub_command results << [ sub_command, sub_args ] end return results.flatten end |
Instance Method Details
#arg(name, default, allowed_values, message_id, config = {}, &block) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/coral_core/util/cli.rb', line 183 def arg(name, default, allowed_values, , config = {}, &block) config = Config.ensure(config) name = name.to_sym = name.to_s + '_message' = CLI.(, arguments[name]) settings = { :name => name, :default => config.get(name, default), :message => config.get(.to_sym, ) } settings[:allowed] = allowed_values if allowed_values settings[:block] = block if block @arg_settings << settings end |
#arg_array(name, default, message_id, config = {}) ⇒ Object
285 286 287 288 289 |
# File 'lib/coral_core/util/cli.rb', line 285 def arg_array(name, default, , config = {}) arg(name, default, Array, , config) do |value| block_given? ? yield(value) : value end end |
#arg_bool(name, default, message_id, config = {}) ⇒ Object
216 217 218 219 220 221 222 223 224 225 |
# File 'lib/coral_core/util/cli.rb', line 216 def arg_bool(name, default, , config = {}) arg(name, default, nil, , config) do |value| value = Util::Data.value(value) if value == true || value == false block_given? ? yield(value) : value else nil end end end |
#arg_float(name, default, message_id, config = {}) ⇒ Object
253 254 255 256 257 |
# File 'lib/coral_core/util/cli.rb', line 253 def arg_float(name, default, , config = {}) arg(name, default, Float, , config) do |value| block_given? ? yield(value) : value end end |
#arg_int(name, default, message_id, config = {}) ⇒ Object
237 238 239 240 241 |
# File 'lib/coral_core/util/cli.rb', line 237 def arg_int(name, default, , config = {}) arg(name, default, Integer, , config) do |value| block_given? ? yield(value) : value end end |
#arg_str(name, default, message_id, config = {}) ⇒ Object
269 270 271 272 273 |
# File 'lib/coral_core/util/cli.rb', line 269 def arg_str(name, default, , config = {}) arg(name, default, nil, , config) do |value| block_given? ? yield(value) : value end end |
#banner=(banner) ⇒ Object
73 74 75 |
# File 'lib/coral_core/util/cli.rb', line 73 def parser. = end |
#help=(help) ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/coral_core/util/cli.rb', line 79 def help=help if help.is_a?(Array) help.each do |line| parser.separator line end else parser.separator help end end |
#option(name, default, option_str, allowed_values, message_id, config = {}) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/coral_core/util/cli.rb', line 158 def option(name, default, option_str, allowed_values, , config = {}) config = Config.ensure(config) name = name.to_sym [name] = config.get(name, default) = name.to_s + '_message' = CLI.(, [name]) option_str = Util::Data.array(option_str) if allowed_values parser.on(*option_str, allowed_values, config.get(.to_sym, )) do |value| value = yield(value) if block_given? [name] = value unless value.nil? end else parser.on(*option_str, config.get(.to_sym, )) do |value| value = yield(value) if block_given? [name] = value unless value.nil? end end end |
#option_array(name, default, option_str, message_id, config = {}) ⇒ Object
277 278 279 280 281 |
# File 'lib/coral_core/util/cli.rb', line 277 def option_array(name, default, option_str, , config = {}) option(name, default, option_str, Array, , config) do |value| block_given? ? yield(value) : value end end |
#option_bool(name, default, option_str, message_id, config = {}) ⇒ Object
203 204 205 206 207 208 209 210 211 212 |
# File 'lib/coral_core/util/cli.rb', line 203 def option_bool(name, default, option_str, , config = {}) option(name, default, option_str, nil, , config) do |value| value = Util::Data.value(value) if value == true || value == false block_given? ? yield(value) : value else nil end end end |
#option_float(name, default, option_str, message_id, config = {}) ⇒ Object
245 246 247 248 249 |
# File 'lib/coral_core/util/cli.rb', line 245 def option_float(name, default, option_str, , config = {}) option(name, default, option_str, Float, , config) do |value| block_given? ? yield(value) : value end end |
#option_int(name, default, option_str, message_id, config = {}) ⇒ Object
229 230 231 232 233 |
# File 'lib/coral_core/util/cli.rb', line 229 def option_int(name, default, option_str, , config = {}) option(name, default, option_str, Integer, , config) do |value| block_given? ? yield(value) : value end end |
#option_str(name, default, option_str, message_id, config = {}) ⇒ Object
261 262 263 264 265 |
# File 'lib/coral_core/util/cli.rb', line 261 def option_str(name, default, option_str, , config = {}) option(name, default, option_str, nil, , config) do |value| block_given? ? yield(value) : value end end |
#parse_command(args) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/coral_core/util/cli.rb', line 91 def parse_command(args) args = args.dup error = false self.processed = false parser.on_tail('-h', '--help', CLI.('coral.core.util.cli.options.help')) do puts parser.help.chomp [:help] = true return end parser.parse!(args) @arg_settings.each_with_index do |settings, index| if index >= args.length value = nil else value = Util::Data.value(args[index]) end if !value.nil? && settings.has_key?(:allowed) allowed = settings[:allowed] case allowed when Class unless value.is_a?(allowed) Coral.ui.error(CLI.(settings[:message])) error = true end when Array unless allowed.include(value) Coral.ui.error(CLI.(settings[:message])) error = true end end end if value.nil? if settings.has_key?(:default) value = settings[:default] else error = true end end if !value.nil? && settings.has_key?(:block) value = settings[:block].call(value) error = true if value.nil? end break if error self.arguments[settings[:name]] = value end if error Coral.ui.error(CLI.('coral.util.cli.parse.error')) puts parser.help.chomp else self.processed = true end rescue OptionParser::InvalidOption raise Errors::CLIInvalidOptions, :help => parser.help.chomp end |