getoptlong 0.1.0

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