rubysl-getoptlong 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3536cc041a64d512b90faa69b462239d59a7d76c
4
+ data.tar.gz: 83ff9342026f92d5c9a5fc2ea5af625a1f6fc2b6
5
+ SHA512:
6
+ metadata.gz: a479f266722fef5c6415da317ac4bd1c2dec4eea7c5d5862bf58c370652b1326ff7d613b611ca18f88b8d3321e73548a1a2854018cf322c28bc6257891b8a099
7
+ data.tar.gz: c6a81a360957af20d366886f6691bcf784164e0aec3f8264c48f7848279b3d74156aa2e8461e982ac5da0f80b02ccce380dc34c1ff0d5210e315fc176c97c2b9
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ before_install:
3
+ - gem update --system
4
+ - gem --version
5
+ - gem install rubysl-bundler
6
+ script: bundle exec mspec spec
7
+ rvm:
8
+ - rbx-nightly-18mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rubysl-getoptlong.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ Copyright (c) 2013, Brian Shirai
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ 1. Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+ 2. Redistributions in binary form must reproduce the above copyright notice,
10
+ this list of conditions and the following disclaimer in the documentation
11
+ and/or other materials provided with the distribution.
12
+ 3. Neither the name of the library nor the names of its contributors may be
13
+ used to endorse or promote products derived from this software without
14
+ specific prior written permission.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY DIRECT,
20
+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23
+ OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
25
+ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,29 @@
1
+ # Rubysl::Getoptlong
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'rubysl-getoptlong'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install rubysl-getoptlong
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1 @@
1
+ require "rubysl/getoptlong"
@@ -0,0 +1,2 @@
1
+ require "rubysl/getoptlong/getoptlong"
2
+ require "rubysl/getoptlong/version"
@@ -0,0 +1,621 @@
1
+ #
2
+ # GetoptLong for Ruby
3
+ #
4
+ # Copyright (C) 1998, 1999, 2000 Motoyuki Kasahara.
5
+ #
6
+ # You may redistribute and/or modify this library under the same license
7
+ # terms as Ruby.
8
+ #
9
+ # See GetoptLong for documentation.
10
+ #
11
+ # Additional documents and the latest version of `getoptlong.rb' can be
12
+ # found at http://www.sra.co.jp/people/m-kasahr/ruby/getoptlong/
13
+
14
+ # The GetoptLong class allows you to parse command line options similarly to
15
+ # the GNU getopt_long() C library call. Note, however, that GetoptLong is a
16
+ # pure Ruby implementation.
17
+ #
18
+ # GetoptLong allows for POSIX-style options like <tt>--file</tt> as well
19
+ # as single letter options like <tt>-f</tt>
20
+ #
21
+ # The empty option <tt>--</tt> (two minus symbols) is used to end option
22
+ # processing. This can be particularly important if options have optional
23
+ # arguments.
24
+ #
25
+ # Here is a simple example of usage:
26
+ #
27
+ # # == Synopsis
28
+ # #
29
+ # # hello: greets user, demonstrates command line parsing
30
+ # #
31
+ # # == Usage
32
+ # #
33
+ # # hello [OPTION] ... DIR
34
+ # #
35
+ # # -h, --help:
36
+ # # show help
37
+ # #
38
+ # # --repeat x, -n x:
39
+ # # repeat x times
40
+ # #
41
+ # # --name [name]:
42
+ # # greet user by name, if name not supplied default is John
43
+ # #
44
+ # # DIR: The directory in which to issue the greeting.
45
+ #
46
+ # require 'getoptlong'
47
+ # require 'rdoc/usage'
48
+ #
49
+ # opts = GetoptLong.new(
50
+ # [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
51
+ # [ '--repeat', '-n', GetoptLong::REQUIRED_ARGUMENT ],
52
+ # [ '--name', GetoptLong::OPTIONAL_ARGUMENT ]
53
+ # )
54
+ #
55
+ # dir = nil
56
+ # name = nil
57
+ # repetitions = 1
58
+ # opts.each do |opt, arg|
59
+ # case opt
60
+ # when '--help'
61
+ # RDoc::usage
62
+ # when '--repeat'
63
+ # repetitions = arg.to_i
64
+ # when '--name'
65
+ # if arg == ''
66
+ # name = 'John'
67
+ # else
68
+ # name = arg
69
+ # end
70
+ # end
71
+ # end
72
+ #
73
+ # if ARGV.length != 1
74
+ # puts "Missing dir argument (try --help)"
75
+ # exit 0
76
+ # end
77
+ #
78
+ # dir = ARGV.shift
79
+ #
80
+ # Dir.chdir(dir)
81
+ # for i in (1..repetitions)
82
+ # print "Hello"
83
+ # if name
84
+ # print ", #{name}"
85
+ # end
86
+ # puts
87
+ # end
88
+ #
89
+ # Example command line:
90
+ #
91
+ # hello -n 6 --name -- /tmp
92
+ #
93
+ class GetoptLong
94
+ #
95
+ # Orderings.
96
+ #
97
+ ORDERINGS = [REQUIRE_ORDER = 0, PERMUTE = 1, RETURN_IN_ORDER = 2]
98
+
99
+ #
100
+ # Argument flags.
101
+ #
102
+ ARGUMENT_FLAGS = [NO_ARGUMENT = 0, REQUIRED_ARGUMENT = 1,
103
+ OPTIONAL_ARGUMENT = 2]
104
+
105
+ #
106
+ # Status codes.
107
+ #
108
+ STATUS_YET, STATUS_STARTED, STATUS_TERMINATED = 0, 1, 2
109
+
110
+ #
111
+ # Error types.
112
+ #
113
+ class Error < StandardError; end
114
+ class AmbigousOption < Error; end
115
+ class NeedlessArgument < Error; end
116
+ class MissingArgument < Error; end
117
+ class InvalidOption < Error; end
118
+
119
+ #
120
+ # Set up option processing.
121
+ #
122
+ # The options to support are passed to new() as an array of arrays.
123
+ # Each sub-array contains any number of String option names which carry
124
+ # the same meaning, and one of the following flags:
125
+ #
126
+ # GetoptLong::NO_ARGUMENT :: Option does not take an argument.
127
+ #
128
+ # GetoptLong::REQUIRED_ARGUMENT :: Option always takes an argument.
129
+ #
130
+ # GetoptLong::OPTIONAL_ARGUMENT :: Option may or may not take an argument.
131
+ #
132
+ # The first option name is considered to be the preferred (canonical) name.
133
+ # Other than that, the elements of each sub-array can be in any order.
134
+ #
135
+ def initialize(*arguments)
136
+ #
137
+ # Current ordering.
138
+ #
139
+ if ENV.include?('POSIXLY_CORRECT')
140
+ @ordering = REQUIRE_ORDER
141
+ else
142
+ @ordering = PERMUTE
143
+ end
144
+
145
+ #
146
+ # Hash table of option names.
147
+ # Keys of the table are option names, and their values are canonical
148
+ # names of the options.
149
+ #
150
+ @canonical_names = Hash.new
151
+
152
+ #
153
+ # Hash table of argument flags.
154
+ # Keys of the table are option names, and their values are argument
155
+ # flags of the options.
156
+ #
157
+ @argument_flags = Hash.new
158
+
159
+ #
160
+ # Whether error messages are output to $deferr.
161
+ #
162
+ @quiet = FALSE
163
+
164
+ #
165
+ # Status code.
166
+ #
167
+ @status = STATUS_YET
168
+
169
+ #
170
+ # Error code.
171
+ #
172
+ @error = nil
173
+
174
+ #
175
+ # Error message.
176
+ #
177
+ @error_message = nil
178
+
179
+ #
180
+ # Rest of catenated short options.
181
+ #
182
+ @rest_singles = ''
183
+
184
+ #
185
+ # List of non-option-arguments.
186
+ # Append them to ARGV when option processing is terminated.
187
+ #
188
+ @non_option_arguments = Array.new
189
+
190
+ if 0 < arguments.length
191
+ set_options(*arguments)
192
+ end
193
+ end
194
+
195
+ #
196
+ # Set the handling of the ordering of options and arguments.
197
+ # A RuntimeError is raised if option processing has already started.
198
+ #
199
+ # The supplied value must be a member of GetoptLong::ORDERINGS. It alters
200
+ # the processing of options as follows:
201
+ #
202
+ # <b>REQUIRE_ORDER</b> :
203
+ #
204
+ # Options are required to occur before non-options.
205
+ #
206
+ # Processing of options ends as soon as a word is encountered that has not
207
+ # been preceded by an appropriate option flag.
208
+ #
209
+ # For example, if -a and -b are options which do not take arguments,
210
+ # parsing command line arguments of '-a one -b two' would result in
211
+ # 'one', '-b', 'two' being left in ARGV, and only ('-a', '') being
212
+ # processed as an option/arg pair.
213
+ #
214
+ # This is the default ordering, if the environment variable
215
+ # POSIXLY_CORRECT is set. (This is for compatibility with GNU getopt_long.)
216
+ #
217
+ # <b>PERMUTE</b> :
218
+ #
219
+ # Options can occur anywhere in the command line parsed. This is the
220
+ # default behavior.
221
+ #
222
+ # Every sequence of words which can be interpreted as an option (with or
223
+ # without argument) is treated as an option; non-option words are skipped.
224
+ #
225
+ # For example, if -a does not require an argument and -b optionally takes
226
+ # an argument, parsing '-a one -b two three' would result in ('-a','') and
227
+ # ('-b', 'two') being processed as option/arg pairs, and 'one','three'
228
+ # being left in ARGV.
229
+ #
230
+ # If the ordering is set to PERMUTE but the environment variable
231
+ # POSIXLY_CORRECT is set, REQUIRE_ORDER is used instead. This is for
232
+ # compatibility with GNU getopt_long.
233
+ #
234
+ # <b>RETURN_IN_ORDER</b> :
235
+ #
236
+ # All words on the command line are processed as options. Words not
237
+ # preceded by a short or long option flag are passed as arguments
238
+ # with an option of '' (empty string).
239
+ #
240
+ # For example, if -a requires an argument but -b does not, a command line
241
+ # of '-a one -b two three' would result in option/arg pairs of ('-a', 'one')
242
+ # ('-b', ''), ('', 'two'), ('', 'three') being processed.
243
+ #
244
+ def ordering=(ordering)
245
+ #
246
+ # The method is failed if option processing has already started.
247
+ #
248
+ if @status != STATUS_YET
249
+ set_error(ArgumentError, "argument error")
250
+ raise RuntimeError,
251
+ "invoke ordering=, but option processing has already started"
252
+ end
253
+
254
+ #
255
+ # Check ordering.
256
+ #
257
+ if !ORDERINGS.include?(ordering)
258
+ raise ArgumentError, "invalid ordering `#{ordering}'"
259
+ end
260
+ if ordering == PERMUTE && ENV.include?('POSIXLY_CORRECT')
261
+ @ordering = REQUIRE_ORDER
262
+ else
263
+ @ordering = ordering
264
+ end
265
+ end
266
+
267
+ #
268
+ # Return ordering.
269
+ #
270
+ attr_reader :ordering
271
+
272
+ #
273
+ # Set options. Takes the same argument as GetoptLong.new.
274
+ #
275
+ # Raises a RuntimeError if option processing has already started.
276
+ #
277
+ def set_options(*arguments)
278
+ #
279
+ # The method is failed if option processing has already started.
280
+ #
281
+ if @status != STATUS_YET
282
+ raise RuntimeError,
283
+ "invoke set_options, but option processing has already started"
284
+ end
285
+
286
+ #
287
+ # Clear tables of option names and argument flags.
288
+ #
289
+ @canonical_names.clear
290
+ @argument_flags.clear
291
+
292
+ arguments.each do |arg|
293
+ #
294
+ # Each argument must be an Array.
295
+ #
296
+ if !arg.is_a?(Array)
297
+ raise ArgumentError, "the option list contains non-Array argument"
298
+ end
299
+
300
+ #
301
+ # Find an argument flag and it set to `argument_flag'.
302
+ #
303
+ argument_flag = nil
304
+ arg.each do |i|
305
+ if ARGUMENT_FLAGS.include?(i)
306
+ if argument_flag != nil
307
+ raise ArgumentError, "too many argument-flags"
308
+ end
309
+ argument_flag = i
310
+ end
311
+ end
312
+ raise ArgumentError, "no argument-flag" if argument_flag == nil
313
+
314
+ canonical_name = nil
315
+ arg.each do |i|
316
+ #
317
+ # Check an option name.
318
+ #
319
+ next if i == argument_flag
320
+ begin
321
+ if !i.is_a?(String) || i !~ /^-([^-]|-.+)$/
322
+ raise ArgumentError, "an invalid option `#{i}'"
323
+ end
324
+ if (@canonical_names.include?(i))
325
+ raise ArgumentError, "option redefined `#{i}'"
326
+ end
327
+ rescue
328
+ @canonical_names.clear
329
+ @argument_flags.clear
330
+ raise
331
+ end
332
+
333
+ #
334
+ # Register the option (`i') to the `@canonical_names' and
335
+ # `@canonical_names' Hashes.
336
+ #
337
+ if canonical_name == nil
338
+ canonical_name = i
339
+ end
340
+ @canonical_names[i] = canonical_name
341
+ @argument_flags[i] = argument_flag
342
+ end
343
+ raise ArgumentError, "no option name" if canonical_name == nil
344
+ end
345
+ return self
346
+ end
347
+
348
+ #
349
+ # Set/Unset `quiet' mode.
350
+ #
351
+ attr_writer :quiet
352
+
353
+ #
354
+ # Return the flag of `quiet' mode.
355
+ #
356
+ attr_reader :quiet
357
+
358
+ #
359
+ # `quiet?' is an alias of `quiet'.
360
+ #
361
+ alias quiet? quiet
362
+
363
+ #
364
+ # Explicitly terminate option processing.
365
+ #
366
+ def terminate
367
+ return nil if @status == STATUS_TERMINATED
368
+ raise RuntimeError, "an error has occured" if @error != nil
369
+
370
+ @status = STATUS_TERMINATED
371
+ @non_option_arguments.reverse_each do |argument|
372
+ ARGV.unshift(argument)
373
+ end
374
+
375
+ @canonical_names = nil
376
+ @argument_flags = nil
377
+ @rest_singles = nil
378
+ @non_option_arguments = nil
379
+
380
+ return self
381
+ end
382
+
383
+ #
384
+ # Returns true if option processing has terminated, false otherwise.
385
+ #
386
+ def terminated?
387
+ return @status == STATUS_TERMINATED
388
+ end
389
+
390
+ #
391
+ # Set an error (protected).
392
+ #
393
+ def set_error(type, message)
394
+ $deferr.print("#{$0}: #{message}\n") if !@quiet
395
+
396
+ @error = type
397
+ @error_message = message
398
+ @canonical_names = nil
399
+ @argument_flags = nil
400
+ @rest_singles = nil
401
+ @non_option_arguments = nil
402
+
403
+ raise type, message
404
+ end
405
+ protected :set_error
406
+
407
+ #
408
+ # Examine whether an option processing is failed.
409
+ #
410
+ attr_reader :error
411
+
412
+ #
413
+ # `error?' is an alias of `error'.
414
+ #
415
+ alias error? error
416
+
417
+ # Return the appropriate error message in POSIX-defined format.
418
+ # If no error has occurred, returns nil.
419
+ #
420
+ def error_message
421
+ return @error_message
422
+ end
423
+
424
+ #
425
+ # Get next option name and its argument, as an Array of two elements.
426
+ #
427
+ # The option name is always converted to the first (preferred)
428
+ # name given in the original options to GetoptLong.new.
429
+ #
430
+ # Example: ['--option', 'value']
431
+ #
432
+ # Returns nil if the processing is complete (as determined by
433
+ # STATUS_TERMINATED).
434
+ #
435
+ def get
436
+ option_name, option_argument = nil, ''
437
+
438
+ #
439
+ # Check status.
440
+ #
441
+ return nil if @error != nil
442
+ case @status
443
+ when STATUS_YET
444
+ @status = STATUS_STARTED
445
+ when STATUS_TERMINATED
446
+ return nil
447
+ end
448
+
449
+ #
450
+ # Get next option argument.
451
+ #
452
+ if 0 < @rest_singles.length
453
+ argument = '-' + @rest_singles
454
+ elsif (ARGV.length == 0)
455
+ terminate
456
+ return nil
457
+ elsif @ordering == PERMUTE
458
+ while 0 < ARGV.length && ARGV[0] !~ /^-./
459
+ @non_option_arguments.push(ARGV.shift)
460
+ end
461
+ if ARGV.length == 0
462
+ terminate
463
+ return nil
464
+ end
465
+ argument = ARGV.shift
466
+ elsif @ordering == REQUIRE_ORDER
467
+ if (ARGV[0] !~ /^-./)
468
+ terminate
469
+ return nil
470
+ end
471
+ argument = ARGV.shift
472
+ else
473
+ argument = ARGV.shift
474
+ end
475
+
476
+ #
477
+ # Check the special argument `--'.
478
+ # `--' indicates the end of the option list.
479
+ #
480
+ if argument == '--' && @rest_singles.length == 0
481
+ terminate
482
+ return nil
483
+ end
484
+
485
+ #
486
+ # Check for long and short options.
487
+ #
488
+ if argument =~ /^(--[^=]+)/ && @rest_singles.length == 0
489
+ #
490
+ # This is a long style option, which start with `--'.
491
+ #
492
+ pattern = $1
493
+ if @canonical_names.include?(pattern)
494
+ option_name = pattern
495
+ else
496
+ #
497
+ # The option `option_name' is not registered in `@canonical_names'.
498
+ # It may be an abbreviated.
499
+ #
500
+ match_count = 0
501
+ @canonical_names.each_key do |key|
502
+ if key.index(pattern) == 0
503
+ option_name = key
504
+ match_count += 1
505
+ end
506
+ end
507
+ if 2 <= match_count
508
+ set_error(AmbigousOption, "option `#{argument}' is ambiguous")
509
+ elsif match_count == 0
510
+ set_error(InvalidOption, "unrecognized option `#{argument}'")
511
+ end
512
+ end
513
+
514
+ #
515
+ # Check an argument to the option.
516
+ #
517
+ if @argument_flags[option_name] == REQUIRED_ARGUMENT
518
+ if argument =~ /=(.*)$/
519
+ option_argument = $1
520
+ elsif 0 < ARGV.length
521
+ option_argument = ARGV.shift
522
+ else
523
+ set_error(MissingArgument,
524
+ "option `#{argument}' requires an argument")
525
+ end
526
+ elsif @argument_flags[option_name] == OPTIONAL_ARGUMENT
527
+ if argument =~ /=(.*)$/
528
+ option_argument = $1
529
+ elsif 0 < ARGV.length && ARGV[0] !~ /^-./
530
+ option_argument = ARGV.shift
531
+ else
532
+ option_argument = ''
533
+ end
534
+ elsif argument =~ /=(.*)$/
535
+ set_error(NeedlessArgument,
536
+ "option `#{option_name}' doesn't allow an argument")
537
+ end
538
+
539
+ elsif argument =~ /^(-(.))(.*)/
540
+ #
541
+ # This is a short style option, which start with `-' (not `--').
542
+ # Short options may be catenated (e.g. `-l -g' is equivalent to
543
+ # `-lg').
544
+ #
545
+ option_name, ch, @rest_singles = $1, $2, $3
546
+
547
+ if @canonical_names.include?(option_name)
548
+ #
549
+ # The option `option_name' is found in `@canonical_names'.
550
+ # Check its argument.
551
+ #
552
+ if @argument_flags[option_name] == REQUIRED_ARGUMENT
553
+ if 0 < @rest_singles.length
554
+ option_argument = @rest_singles
555
+ @rest_singles = ''
556
+ elsif 0 < ARGV.length
557
+ option_argument = ARGV.shift
558
+ else
559
+ # 1003.2 specifies the format of this message.
560
+ set_error(MissingArgument, "option requires an argument -- #{ch}")
561
+ end
562
+ elsif @argument_flags[option_name] == OPTIONAL_ARGUMENT
563
+ if 0 < @rest_singles.length
564
+ option_argument = @rest_singles
565
+ @rest_singles = ''
566
+ elsif 0 < ARGV.length && ARGV[0] !~ /^-./
567
+ option_argument = ARGV.shift
568
+ else
569
+ option_argument = ''
570
+ end
571
+ end
572
+ else
573
+ #
574
+ # This is an invalid option.
575
+ # 1003.2 specifies the format of this message.
576
+ #
577
+ if ENV.include?('POSIXLY_CORRECT')
578
+ set_error(InvalidOption, "illegal option -- #{ch}")
579
+ else
580
+ set_error(InvalidOption, "invalid option -- #{ch}")
581
+ end
582
+ end
583
+ else
584
+ #
585
+ # This is a non-option argument.
586
+ # Only RETURN_IN_ORDER falled into here.
587
+ #
588
+ return '', argument
589
+ end
590
+
591
+ return @canonical_names[option_name], option_argument
592
+ end
593
+
594
+ #
595
+ # `get_option' is an alias of `get'.
596
+ #
597
+ alias get_option get
598
+
599
+ # Iterator version of `get'.
600
+ #
601
+ # The block is called repeatedly with two arguments:
602
+ # The first is the option name.
603
+ # The second is the argument which followed it (if any).
604
+ # Example: ('--opt', 'value')
605
+ #
606
+ # The option name is always converted to the first (preferred)
607
+ # name given in the original options to GetoptLong.new.
608
+ #
609
+ def each
610
+ loop do
611
+ option_name, option_argument = get_option
612
+ break if option_name == nil
613
+ yield option_name, option_argument
614
+ end
615
+ end
616
+
617
+ #
618
+ # `each_option' is an alias of `each'.
619
+ #
620
+ alias each_option each
621
+ end
@@ -0,0 +1,5 @@
1
+ module RubySL
2
+ module GetoptLong
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ require './lib/rubysl/getoptlong/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "rubysl-getoptlong"
6
+ spec.version = RubySL::GetoptLong::VERSION
7
+ spec.authors = ["Brian Shirai"]
8
+ spec.email = ["brixen@gmail.com"]
9
+ spec.description = %q{Ruby standard library getoptlong.}
10
+ spec.summary = %q{Ruby standard library getoptlong.}
11
+ spec.homepage = "https://github.com/rubysl/rubysl-getoptlong"
12
+ spec.license = "BSD"
13
+
14
+ spec.files = `git ls-files`.split($/)
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.3"
20
+ spec.add_development_dependency "rake", "~> 10.0"
21
+ spec.add_development_dependency "mspec", "~> 1.5"
22
+ spec.add_development_dependency "rubysl-prettyprint", "~> 1.0"
23
+ end
@@ -0,0 +1,6 @@
1
+ require 'getoptlong'
2
+ require File.expand_path('../shared/each', __FILE__)
3
+
4
+ describe "GetoptLong#each_option" do
5
+ it_behaves_like(:getoptlong_each, :each_option)
6
+ end
@@ -0,0 +1,6 @@
1
+ require 'getoptlong'
2
+ require File.expand_path('../shared/each', __FILE__)
3
+
4
+ describe "GetoptLong#each" do
5
+ it_behaves_like(:getoptlong_each, :each)
6
+ end
@@ -0,0 +1,31 @@
1
+ require 'getoptlong'
2
+
3
+ describe "GetoptLong#error_message" do
4
+ before :each do
5
+ @stderr = $stderr
6
+ @argv = ARGV
7
+ end
8
+
9
+ after :each do
10
+ $stderr = @stderr
11
+ ARGV = @argv
12
+ end
13
+
14
+ it "returns nil if no error occurred" do
15
+ opts = GetoptLong.new
16
+ opts.error_message.should == nil
17
+ end
18
+
19
+ it "returns the error message of the last error that occurred" do
20
+ begin
21
+ ARGV = []
22
+
23
+ opts = GetoptLong.new
24
+ opts.quiet = true
25
+ opts.get
26
+ opts.ordering = GetoptLong::PERMUTE
27
+ rescue ArgumentError
28
+ opts.error_message.should == "argument error"
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,6 @@
1
+ require 'getoptlong'
2
+ require File.expand_path('../shared/get', __FILE__)
3
+
4
+ describe "GetoptLong#get_option" do
5
+ it_behaves_like(:getoptlong_get, :get_option)
6
+ end
@@ -0,0 +1,6 @@
1
+ require 'getoptlong'
2
+ require File.expand_path('../shared/get', __FILE__)
3
+
4
+ describe "GetoptLong#get" do
5
+ it_behaves_like(:getoptlong_get, :get)
6
+ end
@@ -0,0 +1,27 @@
1
+ require 'getoptlong'
2
+
3
+ describe "GetoptLong#initialize" do
4
+ it "sets ordering to REQUIRE_ORDER if ENV['POSIXLY_CORRECT'] is set" do
5
+ begin
6
+ old_env_value = ENV["POSIXLY_CORRECT"]
7
+ ENV["POSIXLY_CORRECT"] = ""
8
+
9
+ opt = GetoptLong.new
10
+ opt.ordering.should == GetoptLong::REQUIRE_ORDER
11
+ ensure
12
+ ENV["POSIXLY_CORRECT"] = old_env_value
13
+ end
14
+ end
15
+
16
+ it "sets ordering to PERMUTE if ENV['POSIXLY_CORRECT'] is not set" do
17
+ begin
18
+ old_env_value = ENV["POSIXLY_CORRECT"]
19
+ ENV["POSIXLY_CORRECT"] = nil
20
+
21
+ opt = GetoptLong.new
22
+ opt.ordering.should == GetoptLong::PERMUTE
23
+ ensure
24
+ ENV["POSIXLY_CORRECT"] = old_env_value
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,42 @@
1
+ require 'getoptlong'
2
+
3
+ describe "GetoptLong#ordering=" do
4
+ it "raises an ArgumentError if called after processing has started" do
5
+ begin
6
+ old_argv = ARGV
7
+ ARGV = [ "--size", "10k", "--verbose" ]
8
+
9
+ opts = GetoptLong.new([ '--size', GetoptLong::REQUIRED_ARGUMENT ],
10
+ [ '--verbose', GetoptLong::NO_ARGUMENT ])
11
+ opts.quiet = true
12
+ opts.get
13
+
14
+ lambda {
15
+ opts.ordering = GetoptLong::PERMUTE
16
+ }.should raise_error(ArgumentError)
17
+ ensure
18
+ ARGV = old_argv
19
+ end
20
+ end
21
+
22
+ it "raises an ArgumentError if given an invalid value" do
23
+ opts = GetoptLong.new
24
+
25
+ lambda {
26
+ opts.ordering = 12345
27
+ }.should raise_error(ArgumentError)
28
+ end
29
+
30
+ it "does not allow changing ordering to PERMUTE if ENV['POSIXLY_CORRECT'] is set" do
31
+ begin
32
+ old_env_value = ENV['POSIXLY_CORRECT']
33
+ ENV['POSIXLY_CORRECT'] = ""
34
+
35
+ opts = GetoptLong.new
36
+ opts.ordering = GetoptLong::PERMUTE
37
+ opts.ordering.should == GetoptLong::REQUIRE_ORDER
38
+ ensure
39
+ ENV['POSIXLY_CORRECT'] = old_env_value
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,91 @@
1
+ require 'getoptlong'
2
+
3
+ describe "GetoptLong#set_options" do
4
+ before :each do
5
+ @argv = ARGV
6
+ ARGV = []
7
+ @opts = GetoptLong.new
8
+ end
9
+
10
+ after :each do
11
+ ARGV = @argv
12
+ end
13
+
14
+ it "allows setting command line options" do
15
+ ARGV = ["--size", "10k", "-v", "arg1", "arg2"]
16
+
17
+ @opts.set_options(
18
+ ["--size", GetoptLong::REQUIRED_ARGUMENT],
19
+ ["--verbose", "-v", GetoptLong::NO_ARGUMENT]
20
+ )
21
+
22
+ @opts.get.should == ["--size", "10k"]
23
+ @opts.get.should == ["--verbose", ""]
24
+ @opts.get.should == nil
25
+ end
26
+
27
+ it "discards previously defined command line options" do
28
+ ARGV = ["--size", "10k", "-v", "arg1", "arg2"]
29
+
30
+ @opts.set_options(
31
+ ["--size", GetoptLong::REQUIRED_ARGUMENT],
32
+ ["--verbose", "-v", GetoptLong::NO_ARGUMENT]
33
+ )
34
+
35
+ @opts.set_options(
36
+ ["-s", "--size", GetoptLong::REQUIRED_ARGUMENT],
37
+ ["-v", GetoptLong::NO_ARGUMENT]
38
+ )
39
+
40
+ @opts.get.should == ["-s", "10k"]
41
+ @opts.get.should == ["-v", ""]
42
+ @opts.get.should == nil
43
+ end
44
+
45
+ it "raises an ArgumentError if too many argument flags where given" do
46
+ lambda {
47
+ @opts.set_options(["--size", GetoptLong::NO_ARGUMENT, GetoptLong::REQUIRED_ARGUMENT])
48
+ }.should raise_error(ArgumentError)
49
+ end
50
+
51
+ it "raises a RuntimeError if processing has already started" do
52
+ @opts.get
53
+ lambda {
54
+ @opts.set_options()
55
+ }.should raise_error(RuntimeError)
56
+ end
57
+
58
+ it "raises an ArgumentError if no argument flag was given" do
59
+ lambda {
60
+ @opts.set_options(["--size"])
61
+ }.should raise_error(ArgumentError)
62
+ end
63
+
64
+ it "raises an ArgumentError if one of the given arguments is not an Array" do
65
+ lambda {
66
+ @opts.set_options(
67
+ ["--size", GetoptLong::REQUIRED_ARGUMENT],
68
+ "test")
69
+ }.should raise_error(ArgumentError)
70
+ end
71
+
72
+ it "raises an ArgumentError if the same option is given twice" do
73
+ lambda {
74
+ @opts.set_options(
75
+ ["--size", GetoptLong::NO_ARGUMENT],
76
+ ["--size", GetoptLong::OPTIONAL_ARGUMENT])
77
+ }.should raise_error(ArgumentError)
78
+
79
+ lambda {
80
+ @opts.set_options(
81
+ ["--size", GetoptLong::NO_ARGUMENT],
82
+ ["-s", "--size", GetoptLong::OPTIONAL_ARGUMENT])
83
+ }.should raise_error(ArgumentError)
84
+ end
85
+
86
+ it "raises an ArgumentError if the given option is invalid" do
87
+ lambda {
88
+ @opts.set_options(["-size", GetoptLong::NO_ARGUMENT])
89
+ }.should raise_error(ArgumentError)
90
+ end
91
+ end
@@ -0,0 +1,23 @@
1
+ describe :getoptlong_each, :shared => true do
2
+ before(:each) do
3
+ @opts = GetoptLong.new(
4
+ [ '--size', '-s', GetoptLong::REQUIRED_ARGUMENT ],
5
+ [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ],
6
+ [ '--query', '-q', GetoptLong::NO_ARGUMENT ],
7
+ [ '--check', '--valid', '-c', GetoptLong::NO_ARGUMENT ]
8
+ )
9
+ end
10
+
11
+ it "passes each argument/value pair to the block" do
12
+ begin
13
+ old_argv = ARGV
14
+ ARGV = [ "--size", "10k", "-v", "-q", "a.txt", "b.txt" ]
15
+
16
+ pairs = []
17
+ @opts.send(@method) { |arg, val| pairs << [ arg, val ] }
18
+ pairs.should == [ [ "--size", "10k" ], [ "--verbose", "" ], [ "--query", ""] ]
19
+ ensure
20
+ ARGV = old_argv
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,75 @@
1
+ describe :getoptlong_get, :shared => true do
2
+ before(:each) do
3
+ @opts = GetoptLong.new(
4
+ [ '--size', '-s', GetoptLong::REQUIRED_ARGUMENT ],
5
+ [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ],
6
+ [ '--query', '-q', GetoptLong::NO_ARGUMENT ],
7
+ [ '--check', '--valid', '-c', GetoptLong::NO_ARGUMENT ]
8
+ )
9
+ @opts.quiet = true # silence using $deferr
10
+ end
11
+
12
+ it "returns the next option name and its argument as an Array" do
13
+ begin
14
+ old_argv = ARGV
15
+ ARGV = [ "--size", "10k", "-v", "-q", "a.txt", "b.txt" ]
16
+
17
+ @opts.send(@method).should == [ "--size", "10k" ]
18
+ @opts.send(@method).should == [ "--verbose", "" ]
19
+ @opts.send(@method).should == [ "--query", ""]
20
+ @opts.send(@method).should == nil
21
+ ensure
22
+ ARGV = old_argv
23
+ end
24
+ end
25
+
26
+ it "shifts ARGV on each call" do
27
+ begin
28
+ old_argv = ARGV
29
+ ARGV = [ "--size", "10k", "-v", "-q", "a.txt", "b.txt" ]
30
+
31
+ @opts.send(@method)
32
+ ARGV.should == [ "-v", "-q", "a.txt", "b.txt" ]
33
+
34
+ @opts.send(@method)
35
+ ARGV.should == [ "-q", "a.txt", "b.txt" ]
36
+
37
+ @opts.send(@method)
38
+ ARGV.should == [ "a.txt", "b.txt" ]
39
+
40
+ @opts.send(@method)
41
+ ARGV.should == [ "a.txt", "b.txt" ]
42
+ ensure
43
+ ARGV = old_argv
44
+ end
45
+ end
46
+
47
+ it "terminates processing when encountering '--'" do
48
+ begin
49
+ old_argv = ARGV
50
+ ARGV = [ "--size", "10k", "--", "-v", "-q", "a.txt", "b.txt" ]
51
+
52
+ @opts.send(@method)
53
+ ARGV.should == ["--", "-v", "-q", "a.txt", "b.txt"]
54
+
55
+ @opts.send(@method)
56
+ ARGV.should == ["-v", "-q", "a.txt", "b.txt"]
57
+
58
+ @opts.send(@method)
59
+ ARGV.should == ["-v", "-q", "a.txt", "b.txt"]
60
+ ensure
61
+ ARGV = old_argv
62
+ end
63
+ end
64
+
65
+ it "raises a if an argument was required, but none given" do
66
+ begin
67
+ old_argv = ARGV
68
+ ARGV = [ "--size" ]
69
+
70
+ lambda { @opts.send(@method) }.should raise_error(GetoptLong::MissingArgument)
71
+ ensure
72
+ ARGV = old_argv
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,34 @@
1
+ require 'getoptlong'
2
+
3
+ describe "GetoptLong#terminate" do
4
+ before(:each) do
5
+ @opts = GetoptLong.new(
6
+ [ '--size', '-s', GetoptLong::REQUIRED_ARGUMENT ],
7
+ [ '--verbose', '-v', GetoptLong::NO_ARGUMENT ],
8
+ [ '--query', '-q', GetoptLong::NO_ARGUMENT ],
9
+ [ '--check', '--valid', '-c', GetoptLong::NO_ARGUMENT ]
10
+ )
11
+ end
12
+
13
+ it "terminates option proccessing" do
14
+ begin
15
+ old_argv = ARGV
16
+ ARGV = [ "--size", "10k", "-v", "-q", "a.txt", "b.txt" ]
17
+
18
+ @opts.get.should == [ "--size", "10k" ]
19
+ @opts.terminate
20
+ @opts.get.should == nil
21
+ ensure
22
+ ARGV = old_argv
23
+ end
24
+ end
25
+
26
+ it "returns self when option processsing is terminated" do
27
+ @opts.terminate.should == @opts
28
+ end
29
+
30
+ it "returns nil when option processing was already terminated" do
31
+ @opts.terminate
32
+ @opts.terminate.should == nil
33
+ end
34
+ end
@@ -0,0 +1,20 @@
1
+ require 'getoptlong'
2
+
3
+ describe "GetoptLong#terminated?" do
4
+ it "returns true if option processing has terminated" do
5
+ begin
6
+ old_argv_value = ARGV
7
+ ARGV = [ "--size", "10k" ]
8
+ opts = GetoptLong.new(["--size", GetoptLong::REQUIRED_ARGUMENT])
9
+ opts.terminated?.should == false
10
+
11
+ opts.get.should == ["--size", "10k"]
12
+ opts.terminated?.should == false
13
+
14
+ opts.get.should == nil
15
+ opts.terminated?.should == true
16
+ ensure
17
+ ARGV = old_argv_value
18
+ end
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubysl-getoptlong
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Brian Shirai
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubysl-prettyprint
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.0'
69
+ description: Ruby standard library getoptlong.
70
+ email:
71
+ - brixen@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .travis.yml
78
+ - Gemfile
79
+ - LICENSE
80
+ - README.md
81
+ - Rakefile
82
+ - lib/getoptlong.rb
83
+ - lib/rubysl/getoptlong.rb
84
+ - lib/rubysl/getoptlong/getoptlong.rb
85
+ - lib/rubysl/getoptlong/version.rb
86
+ - rubysl-getoptlong.gemspec
87
+ - spec/each_option_spec.rb
88
+ - spec/each_spec.rb
89
+ - spec/error_message_spec.rb
90
+ - spec/get_option_spec.rb
91
+ - spec/get_spec.rb
92
+ - spec/initialize_spec.rb
93
+ - spec/ordering_spec.rb
94
+ - spec/set_options_spec.rb
95
+ - spec/shared/each.rb
96
+ - spec/shared/get.rb
97
+ - spec/terminate_spec.rb
98
+ - spec/terminated_spec.rb
99
+ homepage: https://github.com/rubysl/rubysl-getoptlong
100
+ licenses:
101
+ - BSD
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - '>='
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.0.7
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: Ruby standard library getoptlong.
123
+ test_files:
124
+ - spec/each_option_spec.rb
125
+ - spec/each_spec.rb
126
+ - spec/error_message_spec.rb
127
+ - spec/get_option_spec.rb
128
+ - spec/get_spec.rb
129
+ - spec/initialize_spec.rb
130
+ - spec/ordering_spec.rb
131
+ - spec/set_options_spec.rb
132
+ - spec/shared/each.rb
133
+ - spec/shared/get.rb
134
+ - spec/terminate_spec.rb
135
+ - spec/terminated_spec.rb