Class: Cmdopt::Schema
- Inherits:
-
Object
- Object
- Cmdopt::Schema
- Defined in:
- lib/cmdopt.rb
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
Instance Method Summary collapse
- #add(options, desc, validator = nil, handler = nil) ⇒ Object
- #get(opt) ⇒ Object
- #help(width = nil, indent = " ") ⇒ Object
-
#initialize ⇒ Schema
constructor
A new instance of Schema.
Constructor Details
#initialize ⇒ Schema
18 19 20 21 |
# File 'lib/cmdopt.rb', line 18 def initialize @items = [] @option2item = {} end |
Instance Attribute Details
#items ⇒ Object (readonly)
Returns the value of attribute items.
23 24 25 |
# File 'lib/cmdopt.rb', line 23 def items @items end |
Instance Method Details
#add(options, desc, validator = nil, handler = nil) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/cmdopt.rb', line 29 def add(, desc, validator=nil, handler=nil) attr = nil if =~ / *\#(\w+)\z/ attr, = $1, $` end short, long, arg, required = () attr ||= long || short handler ||= proc {|val, opts| opts[attr] = val } item = SchemaItem.new item.short = short item.long = long item.arg = arg item.required = required item.attr = attr item.desc = desc item. = item.validator = validator item.handler = handler _register(item) return item end |
#get(opt) ⇒ Object
25 26 27 |
# File 'lib/cmdopt.rb', line 25 def get(opt) return @option2item[opt] end |
#help(width = nil, indent = " ") ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/cmdopt.rb', line 76 def help(width=nil, indent=" ") items = @items.select {|it| it.desc } if ! width width = items.collect {|it| it..length }.max + 1 limit = 20 width = limit if width > limit end fmt = "#{indent}%-#{width}s: %s\n" return items.collect {|it| fmt % [it., it.desc] }.join end |