trad-getopt 1.1.0 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Rakefile +8 -12
- data/test.rb +62 -16
- data/trad-getopt.rb +13 -63
- data/trad-getopt.txt +1 -1
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 38d82a3905311e9231690786b724538d2701406265862291124d9b16cbd7e980
|
4
|
+
data.tar.gz: 7a4e1c88210f75a9363296ff6a62139ef8ca0c6c0c58faeb11d6d1602a264388
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 37acd030e56c0040730efa270001cf71eee98efe293164700a12658ac00988f6042af636f0294c052907b2f9f5d917b324283e33d5187a1065b2ab2f1d9f69b7
|
7
|
+
data.tar.gz: db1ecf6e8daac1c477def3d23f4a519de128da70fd53c720194a1dec7c05a380cc4c0c5a4b0474f440e66a1481283879935806c42a3945e92d59d0572be04bf8
|
data/Rakefile
CHANGED
@@ -1,12 +1,13 @@
|
|
1
|
-
require
|
1
|
+
require "rake/testtask"
|
2
2
|
require "rubygems/package_task"
|
3
|
+
require "rake/clean"
|
3
4
|
|
4
|
-
|
5
|
+
require_relative "trad-getopt.rb" # Getopt::VERSION
|
5
6
|
|
6
7
|
task :default => :gem
|
7
8
|
|
8
9
|
task :gem
|
9
|
-
spec = Gem::Specification.new {
|
10
|
+
spec = Gem::Specification.new {|s|
|
10
11
|
s.name = "trad-getopt"
|
11
12
|
s.version = Getopt::VERSION
|
12
13
|
s.author = "NOZAWA Hiromasa"
|
@@ -22,17 +23,12 @@ spec = Gem::Specification.new { |s|
|
|
22
23
|
]
|
23
24
|
s.require_path = "."
|
24
25
|
}
|
25
|
-
|
26
|
+
|
27
|
+
Gem::PackageTask.new(spec) {|pkg|
|
26
28
|
pkg.need_tar_gz = true
|
27
|
-
pkg.need_tar_bz2 = true
|
28
29
|
pkg.need_zip = true
|
29
30
|
}
|
30
31
|
|
31
|
-
|
32
|
-
|
33
|
-
# t.test_files = FileList["test.rb"]
|
34
|
-
# }
|
35
|
-
desc "do unit test"
|
36
|
-
task :test {
|
37
|
-
ruby "-w", "test.rb"
|
32
|
+
Rake::TestTask.new {|t|
|
33
|
+
t.test_files = FileList[ "test.rb" ]
|
38
34
|
}
|
data/test.rb
CHANGED
@@ -1,29 +1,40 @@
|
|
1
1
|
require "test/unit"
|
2
|
-
|
2
|
+
require_relative "trad-getopt.rb"
|
3
3
|
|
4
4
|
class Test_getopt < Test::Unit::TestCase
|
5
5
|
|
6
|
+
myname = File.basename $0
|
7
|
+
# "test.rb"
|
8
|
+
# "rake_test_loader.rb" via rake test
|
9
|
+
|
10
|
+
# empty
|
11
|
+
|
6
12
|
test "empty argv" do
|
7
13
|
av = []
|
8
14
|
assert_equal nil, getopt(av, "a")
|
9
15
|
assert_equal [], av
|
10
16
|
end
|
17
|
+
|
11
18
|
test "empty opts" do
|
12
19
|
av = [ "a" ]
|
13
20
|
assert_equal nil, getopt(av, "")
|
14
21
|
assert_equal [ "a" ], av
|
15
22
|
end
|
23
|
+
|
16
24
|
test "nil opts" do
|
17
25
|
av = [ "a" ]
|
18
26
|
assert_equal nil, getopt(av, nil)
|
19
27
|
assert_equal [ "a" ], av
|
20
28
|
end
|
21
29
|
|
30
|
+
# non-option
|
31
|
+
|
22
32
|
test "stop at non option" do
|
23
33
|
av = ["foo"]
|
24
34
|
assert_equal nil, getopt(av, "-a")
|
25
35
|
assert_equal ["foo"], av
|
26
36
|
end
|
37
|
+
|
27
38
|
test "unknown error" do
|
28
39
|
av = [ "-x" ]
|
29
40
|
$stderr = StringIO.new
|
@@ -31,10 +42,12 @@ class Test_getopt < Test::Unit::TestCase
|
|
31
42
|
msg = $stderr.string
|
32
43
|
$stderr = STDERR
|
33
44
|
assert_equal [:unknown_option, "x"], got
|
34
|
-
assert_equal "
|
45
|
+
assert_equal "#{myname}: unknown option -x\n", msg
|
35
46
|
assert_equal [], av
|
36
47
|
end
|
37
48
|
|
49
|
+
# short
|
50
|
+
|
38
51
|
test "short noarg" do
|
39
52
|
av = [ "-a", "foo" ]
|
40
53
|
assert_equal ["a"], getopt(av, "a")
|
@@ -46,6 +59,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
46
59
|
assert_equal ["a", "foo"], getopt(av, "a:")
|
47
60
|
assert_equal [], av
|
48
61
|
end
|
62
|
+
|
49
63
|
test "short reqarg. error" do
|
50
64
|
av = [ "-a" ]
|
51
65
|
$stderr = StringIO.new
|
@@ -53,7 +67,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
53
67
|
msg = $stderr.string
|
54
68
|
$stderr = STDERR
|
55
69
|
assert_equal [:argument_required, "a"], got
|
56
|
-
assert_equal "
|
70
|
+
assert_equal "#{myname}: option requires an argument -a\n", msg
|
57
71
|
assert_equal [], av
|
58
72
|
end
|
59
73
|
|
@@ -62,16 +76,19 @@ class Test_getopt < Test::Unit::TestCase
|
|
62
76
|
assert_equal ["a", "foo"], getopt(av, "a::")
|
63
77
|
assert_equal [], av
|
64
78
|
end
|
79
|
+
|
65
80
|
test "short optarg, no arg" do
|
66
81
|
av = [ "-a", "foo" ]
|
67
82
|
assert_equal [ "a", nil ], getopt(av, "a::")
|
68
83
|
assert_equal [ "foo" ], av
|
69
84
|
end
|
85
|
+
|
70
86
|
test "short optarg, no arg, at tail" do
|
71
87
|
av = [ "-a" ]
|
72
88
|
assert_equal [ "a", nil ], getopt(av, "a::")
|
73
89
|
assert_equal [], av
|
74
90
|
end
|
91
|
+
|
75
92
|
test "short optarg, disabled" do
|
76
93
|
av = [ "-a" ]
|
77
94
|
$stderr = StringIO.new
|
@@ -80,7 +97,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
80
97
|
assert_equal [ :argument_required, "a"], got
|
81
98
|
end
|
82
99
|
|
83
|
-
|
100
|
+
# concatenation
|
84
101
|
|
85
102
|
test "concat, noarg + noarg" do
|
86
103
|
av = [ "-ab" ]
|
@@ -88,6 +105,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
88
105
|
assert_equal ["a"], getopt(av, opts)
|
89
106
|
assert_equal ["b"], getopt(av, opts)
|
90
107
|
end
|
108
|
+
|
91
109
|
test "concat, noarg + reqarg + arg" do
|
92
110
|
av = [ "-abfoo" ]
|
93
111
|
opts = "ab:"
|
@@ -95,13 +113,14 @@ class Test_getopt < Test::Unit::TestCase
|
|
95
113
|
assert_equal ["b", "foo"], getopt(av, opts)
|
96
114
|
end
|
97
115
|
|
98
|
-
|
116
|
+
# special chars
|
99
117
|
|
100
118
|
test "single `-'" do
|
101
119
|
av = [ "-" ]
|
102
120
|
assert_equal nil, getopt(av, "a")
|
103
121
|
assert_equal ["-"], av
|
104
122
|
end
|
123
|
+
|
105
124
|
test "stop by `--'" do
|
106
125
|
av = [ "--" ]
|
107
126
|
assert_equal nil, getopt(av, "a")
|
@@ -120,6 +139,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
120
139
|
assert_equal ["-"], getopt(av, opts)
|
121
140
|
assert_equal [], av
|
122
141
|
end
|
142
|
+
|
123
143
|
test "hyphen in concat, not as option" do
|
124
144
|
av = [ "-a-" ]
|
125
145
|
opts = "a"
|
@@ -129,7 +149,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
129
149
|
msg = $stderr.string
|
130
150
|
$stderr = STDERR
|
131
151
|
assert_equal [:unknown_option, "-"], got
|
132
|
-
assert_equal "
|
152
|
+
assert_equal "#{myname}: unknown option --\n", msg
|
133
153
|
assert_equal [], av
|
134
154
|
end
|
135
155
|
|
@@ -137,6 +157,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
137
157
|
av = [ "-:" ]
|
138
158
|
assert_equal [":"], getopt(av, ":a")
|
139
159
|
end
|
160
|
+
|
140
161
|
test "colon not as option" do
|
141
162
|
av = [ "-:" ]
|
142
163
|
$stderr = StringIO.new
|
@@ -144,11 +165,11 @@ class Test_getopt < Test::Unit::TestCase
|
|
144
165
|
msg = $stderr.string
|
145
166
|
$stderr = STDERR
|
146
167
|
assert_equal [:unknown_option, ":"], got
|
147
|
-
assert_equal "
|
168
|
+
assert_equal "#{myname}: unknown option -:\n", msg
|
148
169
|
assert_equal [], av
|
149
170
|
end
|
150
171
|
|
151
|
-
|
172
|
+
# keywords
|
152
173
|
|
153
174
|
test "program_name" do
|
154
175
|
av = [ "-x" ]
|
@@ -156,7 +177,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
156
177
|
getopt(av, "a", error_message:true, program_name:"foo")
|
157
178
|
msg = $stderr.string
|
158
179
|
$stderr = STDERR
|
159
|
-
assert_equal "foo: unknown option -
|
180
|
+
assert_equal "foo: unknown option -x\n", msg
|
160
181
|
end
|
161
182
|
|
162
183
|
test "error_message" do
|
@@ -178,6 +199,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
178
199
|
assert_equal "unknown option", ex.message
|
179
200
|
end
|
180
201
|
end
|
202
|
+
|
181
203
|
test "use_exception, Getopt::ArgumentRequiredError" do
|
182
204
|
av = [ "-a" ]
|
183
205
|
begin
|
@@ -189,6 +211,8 @@ class Test_getopt < Test::Unit::TestCase
|
|
189
211
|
end
|
190
212
|
end
|
191
213
|
|
214
|
+
# permute
|
215
|
+
|
192
216
|
test "permute" do
|
193
217
|
av = [ "foo", "-a", "bar" ]
|
194
218
|
opts = "a"
|
@@ -196,6 +220,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
196
220
|
assert_equal nil, getopt(av, opts, permute:true)
|
197
221
|
assert_equal ["foo", "bar"], av
|
198
222
|
end
|
223
|
+
|
199
224
|
test "permute, reqarg" do
|
200
225
|
av = [ "foo", "-a", "bar", "baz" ]
|
201
226
|
opts = "a:"
|
@@ -211,6 +236,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
211
236
|
assert_equal [:argument_required, "a"], getopt(av, opts, allow_empty_optarg:false)
|
212
237
|
$stderr = STDERR
|
213
238
|
end
|
239
|
+
|
214
240
|
test "allow_empty_optarg. long" do
|
215
241
|
av = [ "--foo=" ]
|
216
242
|
longopts = { "foo" => :required_argument }
|
@@ -218,6 +244,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
218
244
|
assert_equal [:argument_required, "foo"], getopt(av, "", longopts, allow_empty_optarg:false)
|
219
245
|
$stderr = STDERR
|
220
246
|
end
|
247
|
+
|
221
248
|
test "allow_empty_optarg. long 2" do
|
222
249
|
av = [ "--foo", "" ]
|
223
250
|
longopts = { "foo" => :required_argument }
|
@@ -225,6 +252,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
225
252
|
assert_equal [:argument_required, "foo"], getopt(av, "", longopts, allow_empty_optarg:false)
|
226
253
|
$stderr = STDERR
|
227
254
|
end
|
255
|
+
|
228
256
|
test "allow_empty_optarg. optional long" do
|
229
257
|
av = [ "--foo=" ]
|
230
258
|
longopts = { "foo" => :optional_argument }
|
@@ -233,7 +261,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
233
261
|
$stderr = STDERR
|
234
262
|
end
|
235
263
|
|
236
|
-
|
264
|
+
# short context
|
237
265
|
|
238
266
|
test "short context" do
|
239
267
|
av = [ "-a-" ]
|
@@ -243,6 +271,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
243
271
|
assert_equal [ :unknown_option, "-" ], getopt(av, opts)
|
244
272
|
$stderr = STDERR
|
245
273
|
end
|
274
|
+
|
246
275
|
test "short context, not long option" do
|
247
276
|
av = [ "-a-foo" ]
|
248
277
|
opts = "afo-"
|
@@ -253,6 +282,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
253
282
|
assert_equal [ "-" ], getopt(av, opts, lopts)
|
254
283
|
assert_equal [ "f" ], getopt(av, opts, lopts)
|
255
284
|
end
|
285
|
+
|
256
286
|
test "short context, not long option, error" do
|
257
287
|
av = [ "-a-foo" ]
|
258
288
|
opts = "a-"
|
@@ -266,7 +296,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
266
296
|
$stderr = STDERR
|
267
297
|
end
|
268
298
|
|
269
|
-
|
299
|
+
# long
|
270
300
|
|
271
301
|
test "long option" do
|
272
302
|
av = [ "--foo" ]
|
@@ -300,6 +330,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
300
330
|
assert_equal [ "--foo" ], av
|
301
331
|
end
|
302
332
|
end
|
333
|
+
|
303
334
|
test "wrong long option type, permute" do
|
304
335
|
av = [ "a", "--foo", "b" ]
|
305
336
|
lopts = {
|
@@ -323,7 +354,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
323
354
|
msg = $stderr.string
|
324
355
|
$stderr = STDERR
|
325
356
|
assert_equal [ :unknown_option, "bar" ], got
|
326
|
-
assert_equal "
|
357
|
+
assert_equal "#{myname}: unknown option --bar\n", msg
|
327
358
|
end
|
328
359
|
|
329
360
|
test "no_argument" do
|
@@ -333,6 +364,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
333
364
|
}
|
334
365
|
assert_equal [ "foo" ], getopt(av, "", lopts)
|
335
366
|
end
|
367
|
+
|
336
368
|
test "no_argument, error" do
|
337
369
|
av = [ "--foo=bar" ]
|
338
370
|
lopts = {
|
@@ -343,8 +375,9 @@ class Test_getopt < Test::Unit::TestCase
|
|
343
375
|
msg = $stderr.string
|
344
376
|
$stderr = STDERR
|
345
377
|
assert_equal [ :argument_given, "foo" ], got
|
346
|
-
assert_equal "
|
378
|
+
assert_equal "#{myname}: option doesn't take an argument --foo\n", msg
|
347
379
|
end
|
380
|
+
|
348
381
|
test "no_argument, exception" do
|
349
382
|
av = [ "--foo=bar" ]
|
350
383
|
lopts = {
|
@@ -366,6 +399,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
366
399
|
}
|
367
400
|
assert_equal [ "foo", "bar" ], getopt(av, "", lopts)
|
368
401
|
end
|
402
|
+
|
369
403
|
test "required_argument, split" do
|
370
404
|
av = [ "--foo", "bar" ]
|
371
405
|
lopts = {
|
@@ -373,6 +407,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
373
407
|
}
|
374
408
|
assert_equal [ "foo", "bar" ], getopt(av, "", lopts)
|
375
409
|
end
|
410
|
+
|
376
411
|
test "required_argument, empty arg" do
|
377
412
|
av = [ "--foo=" ]
|
378
413
|
lopts = {
|
@@ -380,6 +415,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
380
415
|
}
|
381
416
|
assert_equal [ "foo", "" ], getopt(av, "", lopts)
|
382
417
|
end
|
418
|
+
|
383
419
|
test "required_argument, error" do
|
384
420
|
av = [ "--foo" ]
|
385
421
|
lopts = {
|
@@ -390,8 +426,9 @@ class Test_getopt < Test::Unit::TestCase
|
|
390
426
|
msg = $stderr.string
|
391
427
|
$stderr = STDERR
|
392
428
|
assert_equal [ :argument_required, "foo" ], got
|
393
|
-
assert_equal "
|
429
|
+
assert_equal "#{myname}: option requires an argument --foo\n", msg
|
394
430
|
end
|
431
|
+
|
395
432
|
test "required_argument, exception" do
|
396
433
|
av = [ "--foo" ]
|
397
434
|
lopts = {
|
@@ -413,6 +450,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
413
450
|
}
|
414
451
|
assert_equal [ "foo", "bar" ], getopt(av, "", lopts)
|
415
452
|
end
|
453
|
+
|
416
454
|
test "optional_argument, empty arg" do
|
417
455
|
av = [ "--foo=" ]
|
418
456
|
lopts = {
|
@@ -420,6 +458,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
420
458
|
}
|
421
459
|
assert_equal [ "foo", "" ], getopt(av, "", lopts)
|
422
460
|
end
|
461
|
+
|
423
462
|
test "optional_argument, no arg" do
|
424
463
|
av = [ "--foo", "bar" ]
|
425
464
|
lopts = {
|
@@ -427,6 +466,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
427
466
|
}
|
428
467
|
assert_equal [ "foo", nil ], getopt(av, "", lopts)
|
429
468
|
end
|
469
|
+
|
430
470
|
test "optional_argument, no arg, at tail" do
|
431
471
|
av = [ "--foo" ]
|
432
472
|
lopts = {
|
@@ -435,6 +475,8 @@ class Test_getopt < Test::Unit::TestCase
|
|
435
475
|
assert_equal [ "foo", nil ], getopt(av, "", lopts)
|
436
476
|
end
|
437
477
|
|
478
|
+
# abbrev
|
479
|
+
|
438
480
|
test "unique abbrev, no candidates" do
|
439
481
|
av = [ "--fo" ]
|
440
482
|
lopts = {
|
@@ -442,6 +484,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
442
484
|
}
|
443
485
|
assert_equal [ "foo" ], getopt(av, "", lopts)
|
444
486
|
end
|
487
|
+
|
445
488
|
test "unique abbrev" do
|
446
489
|
av = [ "--foo-" ]
|
447
490
|
lopts = {
|
@@ -450,6 +493,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
450
493
|
}
|
451
494
|
assert_equal [ "foo-bar" ], getopt(av, "", lopts)
|
452
495
|
end
|
496
|
+
|
453
497
|
test "abbrev, exact match" do
|
454
498
|
av = [ "--foo" ]
|
455
499
|
lopts = {
|
@@ -458,6 +502,7 @@ class Test_getopt < Test::Unit::TestCase
|
|
458
502
|
}
|
459
503
|
assert_equal [ "foo" ], getopt(av, "", lopts)
|
460
504
|
end
|
505
|
+
|
461
506
|
test "abbrev, ambiguous" do
|
462
507
|
av = [ "--foo" ]
|
463
508
|
lopts = {
|
@@ -469,8 +514,9 @@ class Test_getopt < Test::Unit::TestCase
|
|
469
514
|
msg = $stderr.string
|
470
515
|
$stderr = STDERR
|
471
516
|
assert_equal [ :ambiguous_option, "foo", ["foo1", "foo2"] ], got
|
472
|
-
assert_equal "
|
517
|
+
assert_equal "#{myname}: ambiguos option (--foo1 --foo2) --foo\n", msg
|
473
518
|
end
|
519
|
+
|
474
520
|
test "abbrev, ambiguous, exception" do
|
475
521
|
av = [ "--foo" ]
|
476
522
|
lopts = {
|
@@ -496,6 +542,6 @@ class Test_getopt < Test::Unit::TestCase
|
|
496
542
|
msg = $stderr.string
|
497
543
|
$stderr = STDERR
|
498
544
|
assert_equal [ :unknown_option, "fo" ], got
|
499
|
-
assert_equal "
|
545
|
+
assert_equal "#{myname}: unknown option --fo\n", msg
|
500
546
|
end
|
501
547
|
end
|
data/trad-getopt.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
# rather traditional getopt for Ruby
|
2
2
|
|
3
3
|
module Getopt
|
4
|
-
|
4
|
+
|
5
|
+
VERSION = "1.2.1"
|
5
6
|
|
6
7
|
class GetoptError < StandardError
|
7
8
|
def initialize option, message
|
@@ -63,7 +64,7 @@ def getopt(argv, opts, longopts = nil,
|
|
63
64
|
end
|
64
65
|
|
65
66
|
opts ||= ""
|
66
|
-
program_name ||= $0
|
67
|
+
program_name ||= File.basename $0
|
67
68
|
|
68
69
|
arg = argv.shift
|
69
70
|
return nil if arg.nil?
|
@@ -141,7 +142,7 @@ def getopt(argv, opts, longopts = nil,
|
|
141
142
|
|
142
143
|
optopt = arg[1]
|
143
144
|
arg = arg[2 .. -1]
|
144
|
-
pos = opts.
|
145
|
+
pos = opts.index optopt
|
145
146
|
if pos.nil? || (optopt == ":" && pos != 0)
|
146
147
|
argv.unshift "-#{arg}" unless arg.empty?
|
147
148
|
# keep short option context on error
|
@@ -173,7 +174,15 @@ def getopt(argv, opts, longopts = nil,
|
|
173
174
|
|
174
175
|
rescue Getopt::GetoptError => ex
|
175
176
|
raise if use_exception
|
176
|
-
|
177
|
+
|
178
|
+
if error_message
|
179
|
+
if ex.option.length == 1
|
180
|
+
warn "#{program_name}: #{ex.message} -#{ex.option}"
|
181
|
+
else
|
182
|
+
warn "#{program_name}: #{ex.message} --#{ex.option}"
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
177
186
|
case ex
|
178
187
|
when Getopt::UnknownOptionError
|
179
188
|
return [ :unknown_option, ex.option ]
|
@@ -187,62 +196,3 @@ rescue Getopt::GetoptError => ex
|
|
187
196
|
end
|
188
197
|
|
189
198
|
Kernel.define_singleton_method "getopt", method(:getopt)
|
190
|
-
|
191
|
-
if __FILE__ == $0
|
192
|
-
|
193
|
-
def usage
|
194
|
-
puts <<EOD
|
195
|
-
usage: #{$0} [-hVp] [-Llongopt] opts ...
|
196
|
-
-h print this
|
197
|
-
-V print revision
|
198
|
-
-L define long option like -Lfoo, -Lfoo: or -Lfoo::
|
199
|
-
-p permute
|
200
|
-
opts define short options
|
201
|
-
EOD
|
202
|
-
end
|
203
|
-
|
204
|
-
longopts = {}
|
205
|
-
permute = false
|
206
|
-
|
207
|
-
while op = getopt(ARGV, "hL:pV")
|
208
|
-
op, oparg = op
|
209
|
-
exit 1 if op.is_a? Symbol
|
210
|
-
case op
|
211
|
-
when "h"
|
212
|
-
usage
|
213
|
-
exit
|
214
|
-
when "V"
|
215
|
-
puts "trad-getopt rev.#{Getopt::VERSION}"
|
216
|
-
exit
|
217
|
-
when "p"
|
218
|
-
permute = true
|
219
|
-
when "L"
|
220
|
-
if oparg[-1] == ":"
|
221
|
-
if oparg[-2] == ":"
|
222
|
-
v = :optional_argument
|
223
|
-
oparg[-2] = ""
|
224
|
-
else
|
225
|
-
v = :required_argument
|
226
|
-
end
|
227
|
-
oparg[-1] = ""
|
228
|
-
else
|
229
|
-
v = :no_argument
|
230
|
-
end
|
231
|
-
longopts[oparg] = v
|
232
|
-
else
|
233
|
-
break
|
234
|
-
end
|
235
|
-
end
|
236
|
-
|
237
|
-
opts = ARGV.shift
|
238
|
-
unless opts
|
239
|
-
puts "argument required"
|
240
|
-
exit 1
|
241
|
-
end
|
242
|
-
|
243
|
-
while op = getopt(ARGV, opts, longopts, permute:permute)
|
244
|
-
puts "option #{op} ARGV #{ARGV}"
|
245
|
-
end
|
246
|
-
puts "ARGV #{ARGV}"
|
247
|
-
|
248
|
-
end
|
data/trad-getopt.txt
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: trad-getopt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- NOZAWA Hiromasa
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-04-16 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|
@@ -40,8 +40,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
40
40
|
- !ruby/object:Gem::Version
|
41
41
|
version: '0'
|
42
42
|
requirements: []
|
43
|
-
|
44
|
-
rubygems_version: 2.6.8
|
43
|
+
rubygems_version: 3.0.3
|
45
44
|
signing_key:
|
46
45
|
specification_version: 4
|
47
46
|
summary: rather traditional getopt()
|