ivanvc-choice 0.1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ 0.1.3:
2
+ - Added args_of method to retrieve the arguments of an option
3
+
4
+ 0.1.2:
5
+ - Made validate directive accept block and validate against its boolean value.
6
+ - Created shorthand format for defining options directly with a hash.
7
+
8
+ 0.1.1:
9
+ - Fixed test_option.rb to be self sufficient.
10
+ - Fix so that long argument with equals sign in it will parse correctly [Justin Bailey]
11
+ - Added 'defaultize' deprecation warning. Too much magic can be harmful.
12
+ - Made Choice::Writer::puts, print, and printf public, can now possibly be used by other Choice classes.
13
+ - Changed UnknownArgument to UnknownOption (more descriptive)
14
+ - Added a 'valid' option as per bugtracker request for 'enum.' [Alexis Li]
15
+ - Change --long=[ARG] optional format to --long[=ARG] but keep around old format just in case.
16
+ - Added list format to options as per bug tracker suggestion in the format of --long=*LONG [Alexis Li]
17
+ - Added --long ARG format. Works with --long [ARG] and --long *ARG and --long [*ARG]
18
+ - Added :required option which insists an option is present.
19
+ - Added gamble.rb card game example.
20
+
21
+ 0.1.0:
22
+ - First release
data/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2006 Chris Wanstrath
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7
+ the Software, and to permit persons to whom the Software is furnished to do so,
8
+ subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,447 @@
1
+ defunkt's choice fork.
2
+
3
+ Fixed one test, and added gemspec, so it can be installed as a gem.
4
+
5
+ = Welcome to Choice
6
+
7
+ Choice is a small library for defining and parsing command line options. It
8
+ works awesomely with Highline[http://highline.rubyforge.org/] or other command
9
+ line interface libraries.
10
+
11
+ Choice was written by Chris Wanstrath as an exercise in test driving development
12
+ of a DSL. This project is still an infant: bugs are expected and tattling on them
13
+ is appreciated.
14
+
15
+ Installing is easy, with RubyGems. Give it a shot:
16
+ $ gem install choice
17
+
18
+ If you are lost, you can find Choice at http://choice.rubyforge.org or
19
+ http://rubyforge.org/projects/choice/. E-mail inquiries can be directed to
20
+ mailto:chris[at]ozmm[dot]org.
21
+
22
+ Of course, Choice is licensed under the MIT License, which you can find included
23
+ in the LICENSE file or by surfing your World Wide Web browser of choice towards
24
+ http://www.opensource.org/licenses/mit-license.php.
25
+
26
+ == Using Choice
27
+
28
+ An +examples+ directory is included with Choice, in which some contrived Ruby
29
+ programs utilizing the library have been placed. Here's a snippet:
30
+
31
+ === ftpd.rb
32
+
33
+ require 'choice'
34
+
35
+ PROGRAM_VERSION = 4
36
+
37
+ Choice.options do
38
+ header ''
39
+ header 'Specific options:'
40
+
41
+ option :host do
42
+ short '-h'
43
+ long '--host=HOST'
44
+ desc 'The hostname or ip of the host to bind to (default 127.0.0.1)'
45
+ default '127.0.0.1'
46
+ end
47
+
48
+ option :port do
49
+ short '-p'
50
+ long '--port=PORT'
51
+ desc 'The port to listen on (default 21)'
52
+ cast Integer
53
+ default 21
54
+ end
55
+
56
+ separator ''
57
+ separator 'Common options: '
58
+
59
+ option :help do
60
+ long '--help'
61
+ desc 'Show this message'
62
+ end
63
+
64
+ option :version do
65
+ short '-v'
66
+ long '--version'
67
+ desc 'Show version'
68
+ action do
69
+ puts "ftpd.rb FTP server v#{PROGRAM_VERSION}"
70
+ exit
71
+ end
72
+ end
73
+ end
74
+
75
+ puts 'port: ' + Choice[:port]
76
+
77
+ Notice the last line. For free, you will be given a <tt>Choice.choices</tt>
78
+ hash which contain, at runtime, the options found and their values.
79
+
80
+ <tt>Choice[:key]</tt> is a shortcut for <tt>Choice.choices[:key]</tt>.
81
+
82
+ Because we gave option <tt>:port</tt> a default of 21,
83
+ <tt>Choice[:port]</tt> should be 21 if we run ftpd.rb with no options.
84
+ Let's see.
85
+
86
+ $ ruby ftpd.rb
87
+ port: 21
88
+
89
+ Cool. On our system, port 21 is reserved. Let's use another port.
90
+
91
+ $ ruby ftpd.rb -p 2100
92
+ port: 2100
93
+
94
+ Alright. And, of course, there is the hard way of doing things.
95
+
96
+ $ ruby ftpd.rb --port=2100
97
+ port: 2100
98
+
99
+ That <tt>:version</tt> option looks pretty interesting, huh? I wonder what it
100
+ does...
101
+
102
+ $ ruby ftpd.rb -v
103
+ ftpd.rb FTP server v4
104
+
105
+ That's not all, though. We also get a <tt>--help</tt> option for free.
106
+
107
+ $ ruby ftpd.rb --help
108
+ Usage: ftpd.rb [-hpv]
109
+
110
+ Specific options:
111
+ -h, --host=HOST The hostname or ip of the host to bind to (default 127.0.0.1)
112
+ -p, --port=PORT The port to listen on (default 21)
113
+
114
+ Common options:
115
+ --help Show this message
116
+ -v, --version Show version
117
+
118
+
119
+ == The Choice.choices hash
120
+
121
+ For better or worse, the <tt>Choice.choices</tt> hash is a bit lazy. It does
122
+ not care how you access it. Using the above example, assume we have a
123
+ <tt>:port</tt> option and we replace the last line of our program with the
124
+ following three lines:
125
+
126
+ puts 'port: ' + Choice.choices[:port]
127
+ puts 'port: ' + Choice.choices['port']
128
+ puts 'port: ' + Choice.choices.port
129
+
130
+ Now, run it.
131
+
132
+ $ ftpd.rb -p 2100
133
+ port: 2100
134
+ port: 2100
135
+ port: 2100
136
+
137
+ Lazy, huh?
138
+
139
+ Keep in mind that your option's key in the <tt>Choice.choices</tt> hash is
140
+ defined by the first parameter passed to option statement. This is perfectly
141
+ legit, albeit somewhat confusing:
142
+
143
+ option :name do
144
+ short '-h'
145
+ long '--handle=NAME'
146
+ desc "Your handle."
147
+ end
148
+
149
+ You can access this option by using <tt>Choice.choices[:name]</tt>, not
150
+ <tt>:handle</tt>.
151
+
152
+ == Option options
153
+
154
+ Obviously, Choice revolves around the <tt>option</tt> statement, which receives
155
+ a block. Here are all the, er, options +option+ accepts. None of them are
156
+ required but +short+ or +long+ must be present for Choice to know what to do.
157
+
158
+ Options must be defined in the context of a <tt>Choice.options</tt> block, as
159
+ seen above. This context is assumed for the following explanations.
160
+
161
+ For the quick learners, here's the list:
162
+ * short
163
+ * long
164
+ * default
165
+ * desc
166
+ * cast
167
+ * valid (takes array)
168
+ * validate (takes regex)
169
+ * filter (takes a block)
170
+ * action (ditto)
171
+
172
+ You can define these within your option in any order which pleases you.
173
+
174
+ === short
175
+
176
+ Defines the short switch for an option. Expected to be a dash and a single
177
+ character.
178
+
179
+ short '-s'
180
+
181
+ === long
182
+
183
+ Defines the long switch for an option. Expected to be a double dash followed by
184
+ a string, an equal sign (or a space), and another string.
185
+
186
+ There are two variants: longs where a parameter is required and longs where a
187
+ parameter is optional, in which case the value will be +true+ if the option is
188
+ present.
189
+
190
+ *Optional*:
191
+ long '--debug=[LEVEL]'
192
+
193
+ Assuming our program defines Choices and ends with this line:
194
+ puts 'debug: ' + Choice.choices[:debug]
195
+
196
+ we can do this:
197
+
198
+ $ ruby ftpd.rb --debug
199
+ debug: true
200
+
201
+ $ ruby ftpd.rb --debug=1
202
+ debug: 1
203
+
204
+ $ ruby ftpd.rb --debug 1
205
+ debug: 1
206
+
207
+ *Required*:
208
+ long '--debug=LEVEL'
209
+
210
+ Assuming the same as above:
211
+
212
+ $ ruby ftpd.rb --debug 1
213
+ debug: 1
214
+
215
+ $ ruby ftpd.rb --debug
216
+ <help screen printed>
217
+
218
+ === long as array
219
+
220
+ Often you may wish to allow users the ability to pass in multiple arguments and have
221
+ them all combined into an array. You can accomplish this by defining a +long+ and
222
+ setting the caps-argument to *ARG. Like this:
223
+
224
+ long '--suit *SUITS'
225
+
226
+ <tt>Choice.choices.suits</tt> will now return an array. Here's an example of usage:
227
+
228
+ $ ruby --suit hearts clubs
229
+ suit: ['hearts', 'clubs']
230
+
231
+ Check out <tt>examples/gamble.rb</tt> for more information on this cool feature.
232
+
233
+ === default
234
+
235
+ You can define a default value for your option, if you'd like. If the option
236
+ is not present in the argument list, the default will be returned when trying
237
+ to access that element of the <tt>Choice.choices</tt> hash.
238
+
239
+ As with the above, assume our program prints <tt>Choice.choices[:debug]</tt>:
240
+
241
+ default 'info'
242
+
243
+ If we don't pass in <tt>--debug</tt>, the <tt>:debug</tt> element of our hash
244
+ will be 'info.'
245
+
246
+ $ ftpd.rb
247
+ debug: info
248
+
249
+ $ ftpd.rb --debug warn
250
+ debug: warn
251
+
252
+ === desc
253
+
254
+ The description of this option. Fairly straightforward, with one little trick:
255
+ multiple +desc+ statements in a single option will be considered new desc lines.
256
+ The desc lines will be printed in the order they are defined. Like this:
257
+
258
+ desc "Your hostname."
259
+ desc "(default 'localhost')"
260
+
261
+ A snippet from your <tt>--help</tt> might then look like this:
262
+
263
+ -h, --host=HOST Your hostname.
264
+ (default 127.0.0.1)
265
+
266
+
267
+ === cast
268
+
269
+ By default, all members of the <tt>Choice.choices</tt> hash are strings. If
270
+ you want something different, like an Integer for a port number, you can use
271
+ the +cast+ statement.
272
+
273
+ cast Integer
274
+
275
+ Currently support +cast+ options:
276
+
277
+ * Integer
278
+ * String
279
+ * Float
280
+ * Symbol
281
+
282
+ We'll probably add Date, Time, and DateTime in the future, if people want them.
283
+
284
+ === valid
285
+
286
+ Giving +valid+ an array creates a whitelist of acceptable arguments.
287
+
288
+ valid %w[clubs hearts spades diamonds]
289
+
290
+ If our option is passed anything other than one of the four card suits, the help
291
+ screen will be printed. It might be a good idea to include acceptable arguments in
292
+ your option's "desc" value.
293
+
294
+ $ ruby gamble.rb -s clubs
295
+ suit: clubs
296
+
297
+ $ ruby gamble.rb -s joker
298
+ <help screen printed>
299
+
300
+ === validate
301
+
302
+ The +validate+ statement accepts a regular expression which it will test
303
+ against the value passed. If the test fails, the <tt>--help</tt> screen will
304
+ be printed. I love ports, so let's stick with that example:
305
+
306
+ validate /^\d+$/
307
+
308
+ Of course, 2100 matches this:
309
+
310
+ $ ruby ftpd.rb -p 2100
311
+ port: 2100
312
+
313
+ I like dogs. I wish dogs could be ports. Alas, Choice knows better (once
314
+ I've told it so):
315
+
316
+ $ ruby ftpd.rb -p labradoodle
317
+ <help screen printed>
318
+
319
+ === filter
320
+
321
+ The +filter+ statement lets you play with a value before it goes into the
322
+ <tt>Choice.choices</tt> hash. If you use +cast+, this will occur post-casting.
323
+
324
+ In this program we're defining a :name option and saying we don't want any
325
+ crazy characters in it, then printing that element of the
326
+ <tt>Choice.choices</tt>+ hash:
327
+
328
+ filter do |value|
329
+ value = value.gsub(/[^\w]/, '')
330
+ end
331
+
332
+ Now:
333
+
334
+ $ ruby ftpd.rb --name=c.hr.is
335
+ name: chris
336
+
337
+ You can probably think of better uses.
338
+
339
+ === action
340
+
341
+ A block passed to the +action+ statement will be run if that particular option
342
+ is passed. See the <tt>--version</tt> example earlier.
343
+
344
+ === required options
345
+
346
+ You can specify an option as being required by passing :required => true to the
347
+ option definition. Choice will then print the help screen if this option is
348
+ not present. Please let your dear users know which options are required.
349
+
350
+ For example:
351
+
352
+ option :card, :required => true do
353
+ short '-c'
354
+ long '--card CARD'
355
+ desc "The card you wish to gamble on. Required. Only one, please."
356
+ end
357
+
358
+ Then:
359
+
360
+ $ ruby gamble.rb
361
+ <help screen, -c or --card wasn't passed>
362
+
363
+ == Other options
364
+
365
+ These statements are purely aesthetic, used to help make your <tt>--help</tt>
366
+ screen a little more digestible.
367
+
368
+ Passing an empty string to any of these options will print a newline.
369
+
370
+ === banner
371
+
372
+ The banner is the first line printed when your program is called with
373
+ <tt>--help</tt>. By default, it will be something like this, based on the
374
+ options defined:
375
+
376
+ Usage: ftpd.rb [-hpv]
377
+
378
+ You can pass any string to the +banner+ statement to override what prints. This
379
+ might be useful if you're into ascii art.
380
+
381
+ banner "Usage: ftpd.rb"
382
+
383
+ === header
384
+
385
+ The header is what shows up after the banner but before your option definitions
386
+ are printed. Each header call is a newline. Check out the example above.
387
+
388
+ header "ftp is a harsh and unforgiving protocol."
389
+
390
+ === separator
391
+
392
+ As in the example above, you can put separators between options to help display
393
+ the logical groupings of your options. Or whatever.
394
+
395
+ separator "----"
396
+
397
+ To get a blank line, rock an empty string:
398
+
399
+ separator ''
400
+
401
+ === footer
402
+
403
+ The footer is displayed after all your options are displayed. Nothing new
404
+ here, works like the other options above.
405
+
406
+ footer "That's all there is to it!"
407
+
408
+ == Shorthand
409
+
410
+ Now that you've gone through all the hard stuff, here's the easy stuff: Choice
411
+ options can be defined with a simple hash if you'd like. Here's an example,
412
+ from the tests:
413
+
414
+ Choice.options do
415
+ header "Tell me about yourself?"
416
+ header ""
417
+ options :band => { :short => "-b", :long => "--band=BAND", :cast => String, :desc => "Your favorite band.",
418
+ :validate => /\w+/ },
419
+ :animal => { :short => "-a", :long => "--animal=ANIMAL", :cast => String, :desc => "Your favorite animal." }
420
+
421
+ footer ""
422
+ footer "--help This message"
423
+ end
424
+
425
+ How's that tickle you? Real nice.
426
+
427
+ == It looks like poetry
428
+
429
+ That's it. Not much, I know. Maybe this will make handling your command
430
+ line options a bit easier. You can always use the option parser in the standard
431
+ Ruby library, but DSLs are just so cool. As one of my non-programmer friends
432
+ said of a Ruby DSL: "It looks like poetry."
433
+
434
+ == It's totally broken
435
+
436
+ Okay, I knew this would happen. Do me a favor, if you have time: run +rake+
437
+ from the Choice directory and send me the output (mailto:chris[at]ozmm[dot]org).
438
+ This'll run the unit tests. Also, if you would, send me a bit of information
439
+ on your platform. Choice was tested on OS X and RHEL with a 2.4 kernel but who
440
+ knows. Thanks a lot.
441
+
442
+ == Thanks to
443
+
444
+ For bug reports, patches, and ideas I'd be honored to thank the following:
445
+
446
+ - Justin Bailey
447
+ - Alexis Li