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)
Returns the value of attribute desc.
-
#detail ⇒ Object
Returns the value of attribute detail.
-
#enum ⇒ Object
readonly
Returns the value of attribute enum.
-
#hidden ⇒ Object
writeonly
!!experimental!!.
-
#important ⇒ Object
writeonly
!!experimental!!.
-
#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
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
- #important? ⇒ Boolean
-
#initialize(key, optdef, desc, short, long, param, required, type: nil, rexp: nil, pattern: nil, enum: nil, range: nil, detail: nil, value: nil, hidden: nil, important: 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, hidden: nil, important: nil, tag: nil, &callback) ⇒ SchemaItem
Returns a new instance of SchemaItem.
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/benry/cmdopt.rb', line 325 def initialize(key, optdef, desc, short, long, param, required, type: nil, rexp: nil, pattern: nil, enum: nil, range: nil, detail: nil, value: nil, hidden: nil, important: 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 @hidden = hidden unless nil == hidden @important = important unless nil == important @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.
349 350 351 |
# File 'lib/benry/cmdopt.rb', line 349 def callback @callback end |
#desc ⇒ Object Also known as: help
Returns the value of attribute desc.
349 350 351 |
# File 'lib/benry/cmdopt.rb', line 349 def desc @desc end |
#detail ⇒ Object
Returns the value of attribute detail.
349 350 351 |
# File 'lib/benry/cmdopt.rb', line 349 def detail @detail end |
#enum ⇒ Object (readonly)
Returns the value of attribute enum.
349 350 351 |
# File 'lib/benry/cmdopt.rb', line 349 def enum @enum end |
#hidden=(value) ⇒ Object (writeonly)
!!experimental!!
350 351 352 |
# File 'lib/benry/cmdopt.rb', line 350 def hidden=(value) @hidden = value end |
#important=(value) ⇒ Object (writeonly)
!!experimental!!
350 351 352 |
# File 'lib/benry/cmdopt.rb', line 350 def important=(value) @important = value end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
349 350 351 |
# File 'lib/benry/cmdopt.rb', line 349 def key @key end |
#long ⇒ Object (readonly)
Returns the value of attribute long.
349 350 351 |
# File 'lib/benry/cmdopt.rb', line 349 def long @long end |
#optdef ⇒ Object (readonly)
Returns the value of attribute optdef.
349 350 351 |
# File 'lib/benry/cmdopt.rb', line 349 def optdef @optdef end |
#param ⇒ Object (readonly)
Returns the value of attribute param.
349 350 351 |
# File 'lib/benry/cmdopt.rb', line 349 def param @param end |
#range ⇒ Object (readonly)
Returns the value of attribute range.
349 350 351 |
# File 'lib/benry/cmdopt.rb', line 349 def range @range end |
#rexp ⇒ Object (readonly) Also known as: pattern
Returns the value of attribute rexp.
349 350 351 |
# File 'lib/benry/cmdopt.rb', line 349 def rexp @rexp end |
#short ⇒ Object (readonly)
Returns the value of attribute short.
349 350 351 |
# File 'lib/benry/cmdopt.rb', line 349 def short @short end |
#tag ⇒ Object
Returns the value of attribute tag.
349 350 351 |
# File 'lib/benry/cmdopt.rb', line 349 def tag @tag end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
349 350 351 |
# File 'lib/benry/cmdopt.rb', line 349 def type @type end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
349 350 351 |
# File 'lib/benry/cmdopt.rb', line 349 def value @value end |
Instance Method Details
#arg_requireness ⇒ Object
361 362 363 364 365 366 367 368 |
# File 'lib/benry/cmdopt.rb', line 361 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
370 371 372 373 374 375 376 377 |
# File 'lib/benry/cmdopt.rb', line 370 def hidden?() #; [!no6ov] returns true if @hidden is true. #; [!ej8ot] returns false if @hidden is false. return @hidden if @hidden != nil #; [!h0uxs] returns true if desc is nil. #; [!28vzx] returns false if else. return @desc == nil end |
#important? ⇒ Boolean
379 380 381 382 383 |
# File 'lib/benry/cmdopt.rb', line 379 def important?() #; [!ua8kt] returns true/false if `important:` kwarg passed to constructor. #; [!hz9sx] returns nil if `important:` kwarg not passed to constructor. return @important end |
#required? ⇒ Boolean
354 355 356 357 358 359 |
# File 'lib/benry/cmdopt.rb', line 354 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
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 |
# File 'lib/benry/cmdopt.rb', line 385 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 |