-
Notifications
You must be signed in to change notification settings - Fork 3
/
README
186 lines (128 loc) · 3.68 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
NAME
options.rb
DESCRIPTION
options.rb simplifies the common idiom of dealing with keyword options in
ruby functions. it also deals correctly with symbol vs string keywords and
prevents many subtle programming errors that can arise from doing so
incorrectly. options.rb doesn't hack ruby's core with one exception: the
method Array#options.
SYNOPSIS
require 'options'
def method(*args, &block)
args, options = Options.parse(args)
a = args.shift
b = args.shift
force = options.getopt(:force, default = false)
verbose = options.getopt([:verbose, :VERBOSE])
foo, bar = options.getopt(:foo, :bar)
end
INSTALL
gem install options
HISTORY
2.1.0:
- 1.9 compatibility
- Validation of passed options
SAMPLES
<========< samples/a.rb >========>
~ > cat samples/a.rb
require 'options'
# options.rb makes it super easy to deal with keyword options in a safe and
# easy way.
#
def method(*args)
args, options = Options.parse(args)
force = options.getopt(:force, default=false)
p force
end
method(:foo, :bar, :force => true)
method('force' => true)
~ > ruby samples/a.rb
true
true
<========< samples/b.rb >========>
~ > cat samples/b.rb
require 'options'
# options.rb avoids common mistakes made handling keyword arguments
#
def broken(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
if options[:force]
puts 'forcing'
else
puts 'broken'
end
end
def nonbroken(*args)
args, options = Options.parse(args)
if options.getopt(:force)
puts 'nonbroken'
end
end
broken('force' => true)
nonbroken('force' => true)
def fubar(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
verbose = options[:verbose] || true
if verbose
if options[:verbose]
puts 'verbosely'
else
puts 'fubar'
end
end
end
def nonfubar(*args)
args, options = Options.parse(args)
verbose = options.getopt(:verbose)
if verbose
puts 'verbosely'
else
puts 'nonfubar'
end
end
fubar(:verbose => false)
nonfubar(:verbose => false)
~ > ruby samples/b.rb
broken
nonbroken
fubar
nonfubar
<========< samples/c.rb >========>
~ > cat samples/c.rb
require 'options'
# options.rb hacks ruby core in exactly one way - the method Array#options
#
def method(*args)
options = args.options
p :args => args
p :options => options
end
method(:a, :b, :k => :v)
def method2(*args)
options = args.options.pop
p :args => args
p :options => options
end
method2(:a, :b, :k => :v)
~ > ruby samples/c.rb
{:args=>[:a, :b, {:k=>:v}]}
{:options=>{:k=>:v}}
{:args=>[:a, :b]}
{:options=>{:k=>:v}}
<========< samples/d.rb >========>
~ > cat samples/d.rb
require 'options'
# options.rb makes it easy to provide good error messages when people
# misuse a method.
#
def method(*args)
args, options = Options.parse(args)
options.validate(:force)
force = options.getopt(:force, default=false)
p force
end
method(:foo, :bar, :misspelled_option => true)
~ > ruby samples/d.rb
./lib/options.rb:180:in `validate': Unrecognized options: misspelled_option (ArgumentError)
from samples/d.rb:9:in `method'
from samples/d.rb:15