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
- #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.
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/benry/cmdopt.rb', line 298 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.
322 323 324 |
# File 'lib/benry/cmdopt.rb', line 322 def callback @callback end |
#desc ⇒ Object (readonly) Also known as: help
Returns the value of attribute desc.
322 323 324 |
# File 'lib/benry/cmdopt.rb', line 322 def desc @desc end |
#detail ⇒ Object (readonly)
Returns the value of attribute detail.
322 323 324 |
# File 'lib/benry/cmdopt.rb', line 322 def detail @detail end |
#enum ⇒ Object (readonly)
Returns the value of attribute enum.
322 323 324 |
# File 'lib/benry/cmdopt.rb', line 322 def enum @enum end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
322 323 324 |
# File 'lib/benry/cmdopt.rb', line 322 def key @key end |
#long ⇒ Object (readonly)
Returns the value of attribute long.
322 323 324 |
# File 'lib/benry/cmdopt.rb', line 322 def long @long end |
#optdef ⇒ Object (readonly)
Returns the value of attribute optdef.
322 323 324 |
# File 'lib/benry/cmdopt.rb', line 322 def optdef @optdef end |
#param ⇒ Object (readonly)
Returns the value of attribute param.
322 323 324 |
# File 'lib/benry/cmdopt.rb', line 322 def param @param end |
#range ⇒ Object (readonly)
Returns the value of attribute range.
322 323 324 |
# File 'lib/benry/cmdopt.rb', line 322 def range @range end |
#rexp ⇒ Object (readonly) Also known as: pattern
Returns the value of attribute rexp.
322 323 324 |
# File 'lib/benry/cmdopt.rb', line 322 def rexp @rexp end |
#short ⇒ Object (readonly)
Returns the value of attribute short.
322 323 324 |
# File 'lib/benry/cmdopt.rb', line 322 def short @short end |
#tag ⇒ Object (readonly)
Returns the value of attribute tag.
322 323 324 |
# File 'lib/benry/cmdopt.rb', line 322 def tag @tag end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
322 323 324 |
# File 'lib/benry/cmdopt.rb', line 322 def type @type end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
322 323 324 |
# File 'lib/benry/cmdopt.rb', line 322 def value @value end |
Instance Method Details
#arg_requireness ⇒ Object
333 334 335 336 337 338 339 340 |
# File 'lib/benry/cmdopt.rb', line 333 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
342 343 344 345 346 347 348 349 |
# File 'lib/benry/cmdopt.rb', line 342 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
351 352 353 354 355 |
# File 'lib/benry/cmdopt.rb', line 351 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
326 327 328 329 330 331 |
# File 'lib/benry/cmdopt.rb', line 326 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
357 358 359 360 361 362 363 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 |
# File 'lib/benry/cmdopt.rb', line 357 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 |