Class: OptionBinder
- Inherits:
-
Object
show all
- Extended by:
- Forwardable
- Includes:
- Default, Handler, OptionParser::Acceptables
- Defined in:
- lib/optbind.rb,
lib/optbind/mode.rb,
lib/optbind/type.rb
Defined Under Namespace
Modules: Arguable, Default, Handler, Switch
Classes: MissingArguments, TooManyArguments
Constant Summary
collapse
- BinaryInteger =
/\A[-+]?#{binary}\z/io
- OctalInteger =
/\A[-+]?#{octal}\z/io
- HexadecimalInteger =
/\A[-+]?#{hexadecimal}\z/io
- ShellWords =
NOTE: ShellWords defined in OptionParser does not handle missing argument, therefore it is redefined to work properly on switches with optional argument
Shellwords
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Handler
#included_in, #listed_as, #matched_by
Constructor Details
#initialize(parser: nil, target: nil, bind: nil) {|_self| ... } ⇒ OptionBinder
Returns a new instance of OptionBinder.
9
10
11
12
13
14
|
# File 'lib/optbind.rb', line 9
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.
7
8
9
|
# File 'lib/optbind.rb', line 7
def parser
@parser
end
|
#target ⇒ Object
Returns the value of attribute target.
7
8
9
|
# File 'lib/optbind.rb', line 7
def target
@target
end
|
Instance Method Details
#argument(*opts, &handler) ⇒ Object
Also known as:
arg
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/optbind.rb', line 94
def argument(*opts, &handler)
opts, handler, bound, variable, default = *several_variants(*opts, &handler)
o, h = *loot!(opts, &handler)
(@argument_parser ||= OptionParser.new).on(*(o + ["--#{(@argument_definitions || []).size}"])) do |r|
raise OptionParser::InvalidArgument if opts.include?(:REQUIRED) && (r.nil? || r.respond_to?(:empty?) && r.empty?)
handle! h, r, bound, variable, default
end
(@argument_definitions ||= []) << { opts: opts, handler: handler, bound: bound, variable: variable }
(@bound_variables_with_defaults ||= {})[variable] = default if bound
self
end
|
#assigned?(v) ⇒ Boolean
134
135
136
137
|
# File 'lib/optbind.rb', line 134
def assigned?(v)
return nil unless bound? v
(@assigned_variables_with_values || {}).key? v.to_sym
end
|
#assigned_variables ⇒ Object
119
120
121
122
|
# File 'lib/optbind.rb', line 119
def assigned_variables
return {} unless @assigned_variables_with_values
@assigned_variables_with_values.dup
end
|
#bound?(v) ⇒ Boolean
130
131
132
|
# File 'lib/optbind.rb', line 130
def bound?(v)
(@bound_variables_with_defaults || {}).key? v.to_sym
end
|
#bound_defaults ⇒ Object
110
111
112
|
# File 'lib/optbind.rb', line 110
def bound_defaults
@bound_variables_with_defaults ? @bound_variables_with_defaults.dup : {}
end
|
#bound_variables ⇒ Object
114
115
116
117
|
# File 'lib/optbind.rb', line 114
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
124
125
126
127
128
|
# File 'lib/optbind.rb', line 124
def default?(v)
v = v.to_sym
return nil unless bound? v
(@bound_variables_with_defaults[v] || {}) == @reader.call(v)
end
|
#option(*opts, &handler) ⇒ Object
Also known as:
opt
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/optbind.rb', line 78
def option(*opts, &handler)
opts, handler, bound, variable, default = *several_variants(*opts, &handler)
o, h = *loot!(opts, &handler)
@parser.on(*o) do |r|
raise OptionParser::InvalidArgument if opts.include?(:REQUIRED) && (r.nil? || r.respond_to?(:empty?) && r.empty?)
handle! h, r, bound, variable, default
end
(@option_definitions ||= []) << { opts: opts, handler: handler, bound: bound, variable: variable }
(@bound_variables_with_defaults ||= {})[variable] = default if bound
self
end
|
#order(*argv, &blk) ⇒ Object
4
5
6
|
# File 'lib/optbind/mode.rb', line 4
def order(*argv, &blk)
order! argv.dup.flatten, &blk
end
|
#order!(argv = parser.default_argv, &blk) ⇒ Object
8
9
10
|
# File 'lib/optbind/mode.rb', line 8
def order!(argv = parser.default_argv, &blk)
parse_args! @parser.order! argv, &blk
end
|
#parse(*argv) ⇒ Object
51
52
53
|
# File 'lib/optbind.rb', line 51
def parse(*argv)
parse! argv.dup.flatten
end
|
#parse!(argv = parser.default_argv) ⇒ Object
55
56
57
|
# File 'lib/optbind.rb', line 55
def parse!(argv = parser.default_argv)
parse_args! @parser.parse! argv
end
|
#permute(*argv) ⇒ Object
12
13
14
|
# File 'lib/optbind/mode.rb', line 12
def permute(*argv)
permute! argv.dup.flatten
end
|
#permute!(argv = parser.default_argv) ⇒ Object
16
17
18
|
# File 'lib/optbind/mode.rb', line 16
def permute!(argv = parser.default_argv)
parse_args! @parser.permute! argv
end
|
#usage(*args) ⇒ Object
Also known as:
use
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/optbind.rb', line 63
def usage(*args)
line = (args.flatten * ' ') << "\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
|