Class: OptionBinder
- Inherits:
-
Object
show all
- Extended by:
- Forwardable
- Defined in:
- lib/optbind.rb
Defined Under Namespace
Modules: Arguable, Switch
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(parser: nil, target: nil, bind: nil) {|_self| ... } ⇒ OptionBinder
Returns a new instance of OptionBinder.
7
8
9
10
11
12
|
# File 'lib/optbind.rb', line 7
def initialize(parser: nil, target: nil, bind: nil)
target, bind = TOPLEVEL_BINDING, :to_local_variables if target == nil && bind == nil
@parser = resolve_parser(parser)
@target, @reader, @writer = target, *resolve_binding(target, bind)
yield self if block_given?
end
|
Instance Attribute Details
#parser ⇒ Object
Returns the value of attribute parser.
5
6
7
|
# File 'lib/optbind.rb', line 5
def parser
@parser
end
|
#target ⇒ Object
Returns the value of attribute target.
5
6
7
|
# File 'lib/optbind.rb', line 5
def target
@target
end
|
Instance Method Details
#argument(*opts, &handler) ⇒ Object
Also known as:
arg
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/optbind.rb', line 86
def argument(*opts, &handler)
opts, handler, bound, variable, default = *several_variants(*opts, &handler)
opts.each do |opt|
(opts << :MULTIPLE) and break if opt.to_s =~ /<\S+>\.{3}/
end
(@argument_definitions ||= []) << { opts: opts, handler: handler, bound: bound, variable: variable }
(@bound_variables_with_defaults ||= {})[variable] = default if bound
self
end
|
#bound_defaults ⇒ Object
100
101
102
|
# File 'lib/optbind.rb', line 100
def bound_defaults
@bound_variables_with_defaults ? @bound_variables_with_defaults.dup : {}
end
|
#bound_variables ⇒ Object
104
105
106
107
|
# File 'lib/optbind.rb', line 104
def bound_variables
return {} unless @bound_variables_with_defaults
Hash[@bound_variables_with_defaults.keys.map { |v| [v, @reader.call(v)] }]
end
|
#default?(v) ⇒ Boolean
109
110
111
112
113
|
# File 'lib/optbind.rb', line 109
def default?(v)
v = v.to_sym
return nil unless (@bound_variables_with_defaults || {}).has_key? v
@bound_variables_with_defaults[v] == @reader.call(v)
end
|
#option(*opts, &handler) ⇒ Object
Also known as:
opt
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/optbind.rb', line 68
def option(*opts, &handler)
opts, handler, bound, variable, default = *several_variants(*opts, &handler)
@parser.on(*opts) do |r|
if opts.include? :REQUIRED
a = opts.select { |o| o =~ /\A-/ }.sort_by { |o| o.length }[-1]
@parser.abort "missing argument: #{a}=" if !r || (r.respond_to?(:empty?) && r.empty?)
end
(handler || -> (x) { x }).call(r == nil ? default : r).tap { |x| @writer.call variable, x if bound }
end
(@bound_variables_with_defaults ||= {})[variable] = default if bound
self
end
|
#usage(*args) ⇒ Object
Also known as:
use
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/optbind.rb', line 53
def usage(*args)
line = (args * ' ') << "\n"
if @parser.banner =~ /\Ausage:.+\n\n/i
@parser.banner = "usage: #{program} " << line
@parser.separator "\n"
else
@parser.banner << " or: #{program} " << line
end
self
end
|