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.
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/benry/cmdopt.rb', line 304 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.
328 329 330 |
# File 'lib/benry/cmdopt.rb', line 328 def callback @callback end |
#desc ⇒ Object Also known as: help
Returns the value of attribute desc.
328 329 330 |
# File 'lib/benry/cmdopt.rb', line 328 def desc @desc end |
#detail ⇒ Object
Returns the value of attribute detail.
328 329 330 |
# File 'lib/benry/cmdopt.rb', line 328 def detail @detail end |
#enum ⇒ Object (readonly)
Returns the value of attribute enum.
328 329 330 |
# File 'lib/benry/cmdopt.rb', line 328 def enum @enum end |
#hidden=(value) ⇒ Object (writeonly)
!!experimental!!
329 330 331 |
# File 'lib/benry/cmdopt.rb', line 329 def hidden=(value) @hidden = value end |
#important=(value) ⇒ Object (writeonly)
!!experimental!!
329 330 331 |
# File 'lib/benry/cmdopt.rb', line 329 def important=(value) @important = value end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
328 329 330 |
# File 'lib/benry/cmdopt.rb', line 328 def key @key end |
#long ⇒ Object (readonly)
Returns the value of attribute long.
328 329 330 |
# File 'lib/benry/cmdopt.rb', line 328 def long @long end |
#optdef ⇒ Object (readonly)
Returns the value of attribute optdef.
328 329 330 |
# File 'lib/benry/cmdopt.rb', line 328 def optdef @optdef end |
#param ⇒ Object (readonly)
Returns the value of attribute param.
328 329 330 |
# File 'lib/benry/cmdopt.rb', line 328 def param @param end |
#range ⇒ Object (readonly)
Returns the value of attribute range.
328 329 330 |
# File 'lib/benry/cmdopt.rb', line 328 def range @range end |
#rexp ⇒ Object (readonly) Also known as: pattern
Returns the value of attribute rexp.
328 329 330 |
# File 'lib/benry/cmdopt.rb', line 328 def rexp @rexp end |
#short ⇒ Object (readonly)
Returns the value of attribute short.
328 329 330 |
# File 'lib/benry/cmdopt.rb', line 328 def short @short end |
#tag ⇒ Object
Returns the value of attribute tag.
328 329 330 |
# File 'lib/benry/cmdopt.rb', line 328 def tag @tag end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
328 329 330 |
# File 'lib/benry/cmdopt.rb', line 328 def type @type end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
328 329 330 |
# File 'lib/benry/cmdopt.rb', line 328 def value @value end |
Instance Method Details
#arg_requireness ⇒ Object
340 341 342 343 344 345 346 347 |
# File 'lib/benry/cmdopt.rb', line 340 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
349 350 351 352 353 354 355 356 |
# File 'lib/benry/cmdopt.rb', line 349 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
358 359 360 361 362 |
# File 'lib/benry/cmdopt.rb', line 358 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
333 334 335 336 337 338 |
# File 'lib/benry/cmdopt.rb', line 333 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
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/benry/cmdopt.rb', line 364 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 |