rubysl-getoptlong 1.0.0 → 2.0.0
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.
- checksums.yaml +4 -4
- data/.travis.yml +3 -5
- data/lib/rubysl/getoptlong/getoptlong.rb +143 -152
- data/lib/rubysl/getoptlong/version.rb +2 -2
- data/rubysl-getoptlong.gemspec +0 -1
- metadata +3 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 33eae63a1731a9502d501689c0f1d1ab36116bbe
|
4
|
+
data.tar.gz: 9e5df99b50bb9312fd49263c8047d4cf3fcf5152
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1e240bf44aef8bff5cc3826b00787062bd7d8467045af1ccabc7b783b5ce6addcace72bbf4d0d18ceb2ba20615f2f13645e8e910297010de42354f54cc61403e
|
7
|
+
data.tar.gz: d0118f759d0cceb310b673c2fb553fa54a12b6282582270f32953b4e7ea547554b3fcdd9ce258c6961ebd947332669c65e8ab813ef192e3db6e8153614ebc396
|
data/.travis.yml
CHANGED
@@ -12,10 +12,10 @@
|
|
12
12
|
# found at http://www.sra.co.jp/people/m-kasahr/ruby/getoptlong/
|
13
13
|
|
14
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
|
15
|
+
# the GNU getopt_long() C library call. Note, however, that GetoptLong is a
|
16
16
|
# pure Ruby implementation.
|
17
17
|
#
|
18
|
-
# GetoptLong allows for POSIX-style options like <tt>--file</tt> as well
|
18
|
+
# GetoptLong allows for POSIX-style options like <tt>--file</tt> as well
|
19
19
|
# as single letter options like <tt>-f</tt>
|
20
20
|
#
|
21
21
|
# The empty option <tt>--</tt> (two minus symbols) is used to end option
|
@@ -24,41 +24,34 @@
|
|
24
24
|
#
|
25
25
|
# Here is a simple example of usage:
|
26
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
27
|
# require 'getoptlong'
|
47
|
-
#
|
48
|
-
#
|
28
|
+
#
|
49
29
|
# opts = GetoptLong.new(
|
50
30
|
# [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
|
51
31
|
# [ '--repeat', '-n', GetoptLong::REQUIRED_ARGUMENT ],
|
52
32
|
# [ '--name', GetoptLong::OPTIONAL_ARGUMENT ]
|
53
33
|
# )
|
54
|
-
#
|
34
|
+
#
|
55
35
|
# dir = nil
|
56
36
|
# name = nil
|
57
37
|
# repetitions = 1
|
58
38
|
# opts.each do |opt, arg|
|
59
39
|
# case opt
|
60
40
|
# when '--help'
|
61
|
-
#
|
41
|
+
# puts <<-EOF
|
42
|
+
# hello [OPTION] ... DIR
|
43
|
+
#
|
44
|
+
# -h, --help:
|
45
|
+
# show help
|
46
|
+
#
|
47
|
+
# --repeat x, -n x:
|
48
|
+
# repeat x times
|
49
|
+
#
|
50
|
+
# --name [name]:
|
51
|
+
# greet user by name, if name not supplied default is John
|
52
|
+
#
|
53
|
+
# DIR: The directory in which to issue the greeting.
|
54
|
+
# EOF
|
62
55
|
# when '--repeat'
|
63
56
|
# repetitions = arg.to_i
|
64
57
|
# when '--name'
|
@@ -69,14 +62,14 @@
|
|
69
62
|
# end
|
70
63
|
# end
|
71
64
|
# end
|
72
|
-
#
|
65
|
+
#
|
73
66
|
# if ARGV.length != 1
|
74
67
|
# puts "Missing dir argument (try --help)"
|
75
68
|
# exit 0
|
76
69
|
# end
|
77
|
-
#
|
70
|
+
#
|
78
71
|
# dir = ARGV.shift
|
79
|
-
#
|
72
|
+
#
|
80
73
|
# Dir.chdir(dir)
|
81
74
|
# for i in (1..repetitions)
|
82
75
|
# print "Hello"
|
@@ -111,7 +104,7 @@ class GetoptLong
|
|
111
104
|
# Error types.
|
112
105
|
#
|
113
106
|
class Error < StandardError; end
|
114
|
-
class
|
107
|
+
class AmbiguousOption < Error; end
|
115
108
|
class NeedlessArgument < Error; end
|
116
109
|
class MissingArgument < Error; end
|
117
110
|
class InvalidOption < Error; end
|
@@ -120,7 +113,7 @@ class GetoptLong
|
|
120
113
|
# Set up option processing.
|
121
114
|
#
|
122
115
|
# 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
|
116
|
+
# Each sub-array contains any number of String option names which carry
|
124
117
|
# the same meaning, and one of the following flags:
|
125
118
|
#
|
126
119
|
# GetoptLong::NO_ARGUMENT :: Option does not take an argument.
|
@@ -157,7 +150,7 @@ class GetoptLong
|
|
157
150
|
@argument_flags = Hash.new
|
158
151
|
|
159
152
|
#
|
160
|
-
# Whether error messages are output to $
|
153
|
+
# Whether error messages are output to $stderr.
|
161
154
|
#
|
162
155
|
@quiet = FALSE
|
163
156
|
|
@@ -200,23 +193,23 @@ class GetoptLong
|
|
200
193
|
# the processing of options as follows:
|
201
194
|
#
|
202
195
|
# <b>REQUIRE_ORDER</b> :
|
203
|
-
#
|
196
|
+
#
|
204
197
|
# Options are required to occur before non-options.
|
205
198
|
#
|
206
199
|
# Processing of options ends as soon as a word is encountered that has not
|
207
200
|
# been preceded by an appropriate option flag.
|
208
201
|
#
|
209
202
|
# 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
|
203
|
+
# parsing command line arguments of '-a one -b two' would result in
|
204
|
+
# 'one', '-b', 'two' being left in ARGV, and only ('-a', '') being
|
212
205
|
# processed as an option/arg pair.
|
213
206
|
#
|
214
207
|
# This is the default ordering, if the environment variable
|
215
208
|
# POSIXLY_CORRECT is set. (This is for compatibility with GNU getopt_long.)
|
216
209
|
#
|
217
210
|
# <b>PERMUTE</b> :
|
218
|
-
#
|
219
|
-
# Options can occur anywhere in the command line parsed. This is the
|
211
|
+
#
|
212
|
+
# Options can occur anywhere in the command line parsed. This is the
|
220
213
|
# default behavior.
|
221
214
|
#
|
222
215
|
# Every sequence of words which can be interpreted as an option (with or
|
@@ -233,7 +226,7 @@ class GetoptLong
|
|
233
226
|
#
|
234
227
|
# <b>RETURN_IN_ORDER</b> :
|
235
228
|
#
|
236
|
-
# All words on the command line are processed as options. Words not
|
229
|
+
# All words on the command line are processed as options. Words not
|
237
230
|
# preceded by a short or long option flag are passed as arguments
|
238
231
|
# with an option of '' (empty string).
|
239
232
|
#
|
@@ -248,7 +241,7 @@ class GetoptLong
|
|
248
241
|
if @status != STATUS_YET
|
249
242
|
set_error(ArgumentError, "argument error")
|
250
243
|
raise RuntimeError,
|
251
|
-
|
244
|
+
"invoke ordering=, but option processing has already started"
|
252
245
|
end
|
253
246
|
|
254
247
|
#
|
@@ -279,8 +272,8 @@ class GetoptLong
|
|
279
272
|
# The method is failed if option processing has already started.
|
280
273
|
#
|
281
274
|
if @status != STATUS_YET
|
282
|
-
raise RuntimeError,
|
283
|
-
|
275
|
+
raise RuntimeError,
|
276
|
+
"invoke set_options, but option processing has already started"
|
284
277
|
end
|
285
278
|
|
286
279
|
#
|
@@ -290,11 +283,8 @@ class GetoptLong
|
|
290
283
|
@argument_flags.clear
|
291
284
|
|
292
285
|
arguments.each do |arg|
|
293
|
-
#
|
294
|
-
# Each argument must be an Array.
|
295
|
-
#
|
296
286
|
if !arg.is_a?(Array)
|
297
|
-
|
287
|
+
raise ArgumentError, "the option list contains non-Array argument"
|
298
288
|
end
|
299
289
|
|
300
290
|
#
|
@@ -302,43 +292,44 @@ class GetoptLong
|
|
302
292
|
#
|
303
293
|
argument_flag = nil
|
304
294
|
arg.each do |i|
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
295
|
+
if ARGUMENT_FLAGS.include?(i)
|
296
|
+
if argument_flag != nil
|
297
|
+
raise ArgumentError, "too many argument-flags"
|
298
|
+
end
|
299
|
+
argument_flag = i
|
300
|
+
end
|
311
301
|
end
|
302
|
+
|
312
303
|
raise ArgumentError, "no argument-flag" if argument_flag == nil
|
313
304
|
|
314
305
|
canonical_name = nil
|
315
306
|
arg.each do |i|
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
307
|
+
#
|
308
|
+
# Check an option name.
|
309
|
+
#
|
310
|
+
next if i == argument_flag
|
311
|
+
begin
|
312
|
+
if !i.is_a?(String) || i !~ /^-([^-]|-.+)$/
|
313
|
+
raise ArgumentError, "an invalid option `#{i}'"
|
314
|
+
end
|
315
|
+
if (@canonical_names.include?(i))
|
316
|
+
raise ArgumentError, "option redefined `#{i}'"
|
317
|
+
end
|
318
|
+
rescue
|
319
|
+
@canonical_names.clear
|
320
|
+
@argument_flags.clear
|
321
|
+
raise
|
322
|
+
end
|
323
|
+
|
324
|
+
#
|
325
|
+
# Register the option (`i') to the `@canonical_names' and
|
326
|
+
# `@canonical_names' Hashes.
|
327
|
+
#
|
328
|
+
if canonical_name == nil
|
329
|
+
canonical_name = i
|
330
|
+
end
|
331
|
+
@canonical_names[i] = canonical_name
|
332
|
+
@argument_flags[i] = argument_flag
|
342
333
|
end
|
343
334
|
raise ArgumentError, "no option name" if canonical_name == nil
|
344
335
|
end
|
@@ -388,10 +379,10 @@ class GetoptLong
|
|
388
379
|
end
|
389
380
|
|
390
381
|
#
|
391
|
-
# Set an error (protected).
|
382
|
+
# Set an error (a protected method).
|
392
383
|
#
|
393
384
|
def set_error(type, message)
|
394
|
-
$
|
385
|
+
$stderr.print("#{$0}: #{message}\n") if !@quiet
|
395
386
|
|
396
387
|
@error = type
|
397
388
|
@error_message = message
|
@@ -456,17 +447,17 @@ class GetoptLong
|
|
456
447
|
return nil
|
457
448
|
elsif @ordering == PERMUTE
|
458
449
|
while 0 < ARGV.length && ARGV[0] !~ /^-./
|
459
|
-
|
450
|
+
@non_option_arguments.push(ARGV.shift)
|
460
451
|
end
|
461
452
|
if ARGV.length == 0
|
462
|
-
|
463
|
-
|
453
|
+
terminate
|
454
|
+
return nil
|
464
455
|
end
|
465
456
|
argument = ARGV.shift
|
466
|
-
elsif @ordering == REQUIRE_ORDER
|
457
|
+
elsif @ordering == REQUIRE_ORDER
|
467
458
|
if (ARGV[0] !~ /^-./)
|
468
|
-
|
469
|
-
|
459
|
+
terminate
|
460
|
+
return nil
|
470
461
|
end
|
471
462
|
argument = ARGV.shift
|
472
463
|
else
|
@@ -491,49 +482,49 @@ class GetoptLong
|
|
491
482
|
#
|
492
483
|
pattern = $1
|
493
484
|
if @canonical_names.include?(pattern)
|
494
|
-
|
485
|
+
option_name = pattern
|
495
486
|
else
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
487
|
+
#
|
488
|
+
# The option `option_name' is not registered in `@canonical_names'.
|
489
|
+
# It may be an abbreviated.
|
490
|
+
#
|
491
|
+
matches = []
|
492
|
+
@canonical_names.each_key do |key|
|
493
|
+
if key.index(pattern) == 0
|
494
|
+
option_name = key
|
495
|
+
matches << key
|
496
|
+
end
|
497
|
+
end
|
498
|
+
if 2 <= matches.length
|
499
|
+
set_error(AmbiguousOption, "option `#{argument}' is ambiguous between #{matches.join(', ')}")
|
500
|
+
elsif matches.length == 0
|
501
|
+
set_error(InvalidOption, "unrecognized option `#{argument}'")
|
502
|
+
end
|
512
503
|
end
|
513
504
|
|
514
505
|
#
|
515
506
|
# Check an argument to the option.
|
516
507
|
#
|
517
508
|
if @argument_flags[option_name] == REQUIRED_ARGUMENT
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
509
|
+
if argument =~ /=(.*)$/
|
510
|
+
option_argument = $1
|
511
|
+
elsif 0 < ARGV.length
|
512
|
+
option_argument = ARGV.shift
|
513
|
+
else
|
514
|
+
set_error(MissingArgument,
|
515
|
+
"option `#{argument}' requires an argument")
|
516
|
+
end
|
526
517
|
elsif @argument_flags[option_name] == OPTIONAL_ARGUMENT
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
518
|
+
if argument =~ /=(.*)$/
|
519
|
+
option_argument = $1
|
520
|
+
elsif 0 < ARGV.length && ARGV[0] !~ /^-./
|
521
|
+
option_argument = ARGV.shift
|
522
|
+
else
|
523
|
+
option_argument = ''
|
524
|
+
end
|
534
525
|
elsif argument =~ /=(.*)$/
|
535
|
-
|
536
|
-
|
526
|
+
set_error(NeedlessArgument,
|
527
|
+
"option `#{option_name}' doesn't allow an argument")
|
537
528
|
end
|
538
529
|
|
539
530
|
elsif argument =~ /^(-(.))(.*)/
|
@@ -545,40 +536,40 @@ class GetoptLong
|
|
545
536
|
option_name, ch, @rest_singles = $1, $2, $3
|
546
537
|
|
547
538
|
if @canonical_names.include?(option_name)
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
539
|
+
#
|
540
|
+
# The option `option_name' is found in `@canonical_names'.
|
541
|
+
# Check its argument.
|
542
|
+
#
|
543
|
+
if @argument_flags[option_name] == REQUIRED_ARGUMENT
|
544
|
+
if 0 < @rest_singles.length
|
545
|
+
option_argument = @rest_singles
|
546
|
+
@rest_singles = ''
|
547
|
+
elsif 0 < ARGV.length
|
548
|
+
option_argument = ARGV.shift
|
549
|
+
else
|
550
|
+
# 1003.2 specifies the format of this message.
|
551
|
+
set_error(MissingArgument, "option requires an argument -- #{ch}")
|
552
|
+
end
|
553
|
+
elsif @argument_flags[option_name] == OPTIONAL_ARGUMENT
|
554
|
+
if 0 < @rest_singles.length
|
555
|
+
option_argument = @rest_singles
|
556
|
+
@rest_singles = ''
|
557
|
+
elsif 0 < ARGV.length && ARGV[0] !~ /^-./
|
558
|
+
option_argument = ARGV.shift
|
559
|
+
else
|
560
|
+
option_argument = ''
|
561
|
+
end
|
562
|
+
end
|
572
563
|
else
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
564
|
+
#
|
565
|
+
# This is an invalid option.
|
566
|
+
# 1003.2 specifies the format of this message.
|
567
|
+
#
|
568
|
+
if ENV.include?('POSIXLY_CORRECT')
|
569
|
+
set_error(InvalidOption, "invalid option -- #{ch}")
|
570
|
+
else
|
571
|
+
set_error(InvalidOption, "invalid option -- #{ch}")
|
572
|
+
end
|
582
573
|
end
|
583
574
|
else
|
584
575
|
#
|
@@ -600,7 +591,7 @@ class GetoptLong
|
|
600
591
|
#
|
601
592
|
# The block is called repeatedly with two arguments:
|
602
593
|
# The first is the option name.
|
603
|
-
# The second is the argument which followed it (if any).
|
594
|
+
# The second is the argument which followed it (if any).
|
604
595
|
# Example: ('--opt', 'value')
|
605
596
|
#
|
606
597
|
# The option name is always converted to the first (preferred)
|
data/rubysl-getoptlong.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubysl-getoptlong
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Shirai
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-09-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,20 +52,6 @@ dependencies:
|
|
52
52
|
- - ~>
|
53
53
|
- !ruby/object:Gem::Version
|
54
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
55
|
description: Ruby standard library getoptlong.
|
70
56
|
email:
|
71
57
|
- brixen@gmail.com
|
@@ -133,3 +119,4 @@ test_files:
|
|
133
119
|
- spec/shared/get.rb
|
134
120
|
- spec/terminate_spec.rb
|
135
121
|
- spec/terminated_spec.rb
|
122
|
+
has_rdoc:
|