Class: Cmdopt::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/cmdopt.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSchema



18
19
20
21
# File 'lib/cmdopt.rb', line 18

def initialize
  @items = []
  @option2item = {}
end

Instance Attribute Details

#itemsObject (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(options, desc, validator=nil, handler=nil)
  attr = nil
  if options =~ / *\#(\w+)\z/
    attr, options = $1, $`
  end
  short, long, arg, required = _parse_options(options)
  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.options   = options
  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.options.length }.max + 1
    limit = 20
    width = limit if width > limit
  end
  fmt = "#{indent}%-#{width}s: %s\n"
  return items.collect {|it| fmt % [it.options, it.desc] }.join
end