Class: Benry::CmdOpt::SchemaItem
- Inherits:
-
Object
- Object
- Benry::CmdOpt::SchemaItem
- Defined in:
- lib/benry/cmdopt.rb
Overview
avoid Struct
Instance Attribute Summary collapse
-
#callback ⇒ Object
readonly
Returns the value of attribute callback.
-
#desc ⇒ Object
(also: #help)
readonly
Returns the value of attribute desc.
-
#detail ⇒ Object
readonly
Returns the value of attribute detail.
-
#enum ⇒ Object
readonly
Returns the value of attribute enum.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#long ⇒ Object
readonly
Returns the value of attribute long.
-
#optdef ⇒ Object
readonly
Returns the value of attribute optdef.
-
#param ⇒ Object
readonly
Returns the value of attribute param.
-
#range ⇒ Object
readonly
Returns the value of attribute range.
-
#rexp ⇒ Object
(also: #pattern)
readonly
Returns the value of attribute rexp.
-
#short ⇒ Object
readonly
Returns the value of attribute short.
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #arg_requireness ⇒ Object
- #hidden? ⇒ Boolean
-
#initialize(key, optdef, desc, short, long, param, required, type: nil, rexp: nil, pattern: nil, enum: nil, range: nil, detail: nil, value: nil, tag: nil, &callback) ⇒ SchemaItem
constructor
A new instance of SchemaItem.
- #required? ⇒ Boolean
- #validate_and_convert(val, optdict) ⇒ Object
Constructor Details
#initialize(key, optdef, desc, short, long, param, required, type: nil, rexp: nil, pattern: nil, enum: nil, range: nil, detail: nil, value: nil, tag: nil, &callback) ⇒ SchemaItem
Returns a new instance of SchemaItem.
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/benry/cmdopt.rb', line 284 def initialize(key, optdef, desc, short, long, param, required, type: nil, rexp: nil, pattern: nil, enum: nil, range: nil, detail: nil, value: nil, tag: nil, &callback) rexp ||= pattern # for backward compatibility _init_validation(param, required, type, rexp, enum, range, value) @key = key unless nil == key @optdef = optdef unless nil == optdef @desc = desc unless nil == desc @short = short unless nil == short @long = long unless nil == long @param = param unless nil == param @required = required unless nil == required @type = type unless nil == type @rexp = rexp unless nil == rexp @enum = enum unless nil == enum @range = range unless nil == range @detail = detail unless nil == detail @value = value unless nil == value @tag = tag unless nil == tag @callback = callback unless nil == callback #; [!nn4cp] freezes enum object. @enum.freeze() if @enum end |
Instance Attribute Details
#callback ⇒ Object (readonly)
Returns the value of attribute callback.
306 307 308 |
# File 'lib/benry/cmdopt.rb', line 306 def callback @callback end |
#desc ⇒ Object (readonly) Also known as: help
Returns the value of attribute desc.
306 307 308 |
# File 'lib/benry/cmdopt.rb', line 306 def desc @desc end |
#detail ⇒ Object (readonly)
Returns the value of attribute detail.
306 307 308 |
# File 'lib/benry/cmdopt.rb', line 306 def detail @detail end |
#enum ⇒ Object (readonly)
Returns the value of attribute enum.
306 307 308 |
# File 'lib/benry/cmdopt.rb', line 306 def enum @enum end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
306 307 308 |
# File 'lib/benry/cmdopt.rb', line 306 def key @key end |
#long ⇒ Object (readonly)
Returns the value of attribute long.
306 307 308 |
# File 'lib/benry/cmdopt.rb', line 306 def long @long end |
#optdef ⇒ Object (readonly)
Returns the value of attribute optdef.
306 307 308 |
# File 'lib/benry/cmdopt.rb', line 306 def optdef @optdef end |
#param ⇒ Object (readonly)
Returns the value of attribute param.
306 307 308 |
# File 'lib/benry/cmdopt.rb', line 306 def param @param end |
#range ⇒ Object (readonly)
Returns the value of attribute range.
306 307 308 |
# File 'lib/benry/cmdopt.rb', line 306 def range @range end |
#rexp ⇒ Object (readonly) Also known as: pattern
Returns the value of attribute rexp.
306 307 308 |
# File 'lib/benry/cmdopt.rb', line 306 def rexp @rexp end |
#short ⇒ Object (readonly)
Returns the value of attribute short.
306 307 308 |
# File 'lib/benry/cmdopt.rb', line 306 def short @short end |
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
306 307 308 |
# File 'lib/benry/cmdopt.rb', line 306 def tag @tag end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
306 307 308 |
# File 'lib/benry/cmdopt.rb', line 306 def type @type end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
306 307 308 |
# File 'lib/benry/cmdopt.rb', line 306 def value @value end |
Instance Method Details
#arg_requireness ⇒ Object
317 318 319 320 321 322 323 324 |
# File 'lib/benry/cmdopt.rb', line 317 def arg_requireness() #; [!kmo28] returns :none if option takes no arguments. #; [!owpba] returns :optional if argument is optional. #; [!s8gxl] returns :required if argument is required. return :none if ! @param return :required if @required return :optional end |
#hidden? ⇒ Boolean
326 327 328 329 330 331 |
# File 'lib/benry/cmdopt.rb', line 326 def hidden?() #; [!h0uxs] returns true if desc is nil. #; [!su00g] returns true if key starts with '_'. #; [!28vzx] returns false if else. return @desc == nil || @key.to_s.start_with?('_') end |
#required? ⇒ Boolean
310 311 312 313 314 315 |
# File 'lib/benry/cmdopt.rb', line 310 def required?() #; [!svxny] returns nil if option takes no arguments. #; [!uwbgc] returns false if argument is optional. #; [!togcx] returns true if argument is required. return ! @param ? nil : !! @required end |
#validate_and_convert(val, optdict) ⇒ Object
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/benry/cmdopt.rb', line 333 def validate_and_convert(val, optdict) #; [!h0s0o] raises RuntimeError when value not matched to pattern. if @rexp && val != true val =~ @rexp or raise "Pattern unmatched." end #; [!j4fuz] calls type-specific callback when type specified. if @type && val != true proc_ = PARAM_TYPES[@type] val = proc_.call(val) end #; [!5jrdf] raises RuntimeError when value not in enum. if @enum && val != true @enum.include?(val) or raise "Expected one of #{@enum.join('/')}." end #; [!5falp] raise RuntimeError when value not in range. #; [!a0rej] supports endless range. if @range && val != true r = @range r.begin == nil || r.begin <= val or ( raise "Positive value (>= 0) expected." if r.begin == 0 raise "Positive value (>= 1) expected." if r.begin == 1 raise "Too small (min: #{r.begin.inspect})" ) r.end == nil || val <= r.end or raise "Too large (max: #{r.end.inspect})" end #; [!jn9z3] calls callback when callback specified. #; [!iqalh] calls callback with different number of args according to arity. if @callback n_args = @callback.arity val = n_args == 1 ? @callback.call(val) \ : @callback.call(optdict, @key, val) end #; [!eafem] returns default value (if specified) instead of true value. return @value if val == true && @value != nil #; [!x066l] returns new value. return val end |