trad-getopt 1.2.1 → 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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/test.rb +97 -36
  3. data/trad-getopt.rb +187 -49
  4. data/trad-getopt.txt +19 -20
  5. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 38d82a3905311e9231690786b724538d2701406265862291124d9b16cbd7e980
4
- data.tar.gz: 7a4e1c88210f75a9363296ff6a62139ef8ca0c6c0c58faeb11d6d1602a264388
3
+ metadata.gz: 249a0240a2d24ecd2acdfbbeea4ea735b7741a111c3a3d73f73924ab7c69dbaa
4
+ data.tar.gz: 01f880cd85ba5f25295aa1a7edbe831217fd5f60e19e9c1ffa408e20cd2fe1ed
5
5
  SHA512:
6
- metadata.gz: 37acd030e56c0040730efa270001cf71eee98efe293164700a12658ac00988f6042af636f0294c052907b2f9f5d917b324283e33d5187a1065b2ab2f1d9f69b7
7
- data.tar.gz: db1ecf6e8daac1c477def3d23f4a519de128da70fd53c720194a1dec7c05a380cc4c0c5a4b0474f440e66a1481283879935806c42a3945e92d59d0572be04bf8
6
+ metadata.gz: ae31e46184189f798d898d7b78f4b2036f286ad373a537f4abb235909aea49c0475412c8b224947a911f633b251f32c1c6699a0dfd1f05ebb6f57d679a340078
7
+ data.tar.gz: 255ba6bab37aa24df764a7631f2519568dfbf68572d4e7d2aca67d1f8f9b5a84806871295fc95e4bdae5f0c7a4bf73a6fa65760f6c5275a8620ec5648cbb7298
data/test.rb CHANGED
@@ -41,8 +41,8 @@ class Test_getopt < Test::Unit::TestCase
41
41
  got = getopt(av, "a")
42
42
  msg = $stderr.string
43
43
  $stderr = STDERR
44
- assert_equal [:unknown_option, "x"], got
45
- assert_equal "#{myname}: unknown option -x\n", msg
44
+ assert_equal [:unknown_option, "-x"], got
45
+ assert_equal "#{myname}: Unknown option: -x\n", msg
46
46
  assert_equal [], av
47
47
  end
48
48
 
@@ -66,8 +66,8 @@ class Test_getopt < Test::Unit::TestCase
66
66
  got = getopt(av, "a:")
67
67
  msg = $stderr.string
68
68
  $stderr = STDERR
69
- assert_equal [:argument_required, "a"], got
70
- assert_equal "#{myname}: option requires an argument -a\n", msg
69
+ assert_equal [:argument_required, "-a"], got
70
+ assert_equal "#{myname}: Option requires an argument: -a\n", msg
71
71
  assert_equal [], av
72
72
  end
73
73
 
@@ -94,7 +94,7 @@ class Test_getopt < Test::Unit::TestCase
94
94
  $stderr = StringIO.new
95
95
  got = getopt(av, "a::", optional_short:false)
96
96
  $stderr = STDERR
97
- assert_equal [ :argument_required, "a"], got
97
+ assert_equal [ :argument_required, "-a"], got
98
98
  end
99
99
 
100
100
  # concatenation
@@ -148,8 +148,8 @@ class Test_getopt < Test::Unit::TestCase
148
148
  got = getopt(av, opts)
149
149
  msg = $stderr.string
150
150
  $stderr = STDERR
151
- assert_equal [:unknown_option, "-"], got
152
- assert_equal "#{myname}: unknown option --\n", msg
151
+ assert_equal [:unknown_option, "--"], got
152
+ assert_equal "#{myname}: Unknown option: --\n", msg
153
153
  assert_equal [], av
154
154
  end
155
155
 
@@ -164,8 +164,8 @@ class Test_getopt < Test::Unit::TestCase
164
164
  got = getopt(av, "a:")
165
165
  msg = $stderr.string
166
166
  $stderr = STDERR
167
- assert_equal [:unknown_option, ":"], got
168
- assert_equal "#{myname}: unknown option -:\n", msg
167
+ assert_equal [:unknown_option, "-:"], got
168
+ assert_equal "#{myname}: Unknown option: -:\n", msg
169
169
  assert_equal [], av
170
170
  end
171
171
 
@@ -177,7 +177,7 @@ class Test_getopt < Test::Unit::TestCase
177
177
  getopt(av, "a", error_message:true, program_name:"foo")
178
178
  msg = $stderr.string
179
179
  $stderr = STDERR
180
- assert_equal "foo: unknown option -x\n", msg
180
+ assert_equal "foo: Unknown option: -x\n", msg
181
181
  end
182
182
 
183
183
  test "error_message" do
@@ -195,8 +195,8 @@ class Test_getopt < Test::Unit::TestCase
195
195
  getopt(av, "a", use_exception:true)
196
196
  rescue => ex
197
197
  assert_equal Getopt::UnknownOptionError, ex.class
198
- assert_equal "x", ex.option
199
- assert_equal "unknown option", ex.message
198
+ assert_equal "-x", ex.option
199
+ assert_equal "Unknown option", ex.message
200
200
  end
201
201
  end
202
202
 
@@ -206,8 +206,8 @@ class Test_getopt < Test::Unit::TestCase
206
206
  getopt(av, "a:", use_exception:true)
207
207
  rescue => ex
208
208
  assert_equal Getopt::ArgumentRequiredError, ex.class
209
- assert_equal "a", ex.option
210
- assert_equal "option requires an argument", ex.message
209
+ assert_equal "-a", ex.option
210
+ assert_equal "Option requires an argument", ex.message
211
211
  end
212
212
  end
213
213
 
@@ -233,7 +233,8 @@ class Test_getopt < Test::Unit::TestCase
233
233
  av = [ "-a", "" ]
234
234
  opts = "a:"
235
235
  $stderr = StringIO.new
236
- assert_equal [:argument_required, "a"], getopt(av, opts, allow_empty_optarg:false)
236
+ assert_equal [:argument_required, "-a"],
237
+ getopt(av, opts, allow_empty_optarg:false)
237
238
  $stderr = STDERR
238
239
  end
239
240
 
@@ -241,7 +242,8 @@ class Test_getopt < Test::Unit::TestCase
241
242
  av = [ "--foo=" ]
242
243
  longopts = { "foo" => :required_argument }
243
244
  $stderr = StringIO.new
244
- assert_equal [:argument_required, "foo"], getopt(av, "", longopts, allow_empty_optarg:false)
245
+ assert_equal [:argument_required, "--foo"],
246
+ getopt(av, "", longopts, allow_empty_optarg:false)
245
247
  $stderr = STDERR
246
248
  end
247
249
 
@@ -249,7 +251,8 @@ class Test_getopt < Test::Unit::TestCase
249
251
  av = [ "--foo", "" ]
250
252
  longopts = { "foo" => :required_argument }
251
253
  $stderr = StringIO.new
252
- assert_equal [:argument_required, "foo"], getopt(av, "", longopts, allow_empty_optarg:false)
254
+ assert_equal [:argument_required, "--foo"],
255
+ getopt(av, "", longopts, allow_empty_optarg:false)
253
256
  $stderr = STDERR
254
257
  end
255
258
 
@@ -257,7 +260,8 @@ class Test_getopt < Test::Unit::TestCase
257
260
  av = [ "--foo=" ]
258
261
  longopts = { "foo" => :optional_argument }
259
262
  $stderr = StringIO.new
260
- assert_equal [:argument_required, "foo"], getopt(av, "", longopts, allow_empty_optarg:false)
263
+ assert_equal [:argument_required, "--foo"],
264
+ getopt(av, "", longopts, allow_empty_optarg:false)
261
265
  $stderr = STDERR
262
266
  end
263
267
 
@@ -268,7 +272,7 @@ class Test_getopt < Test::Unit::TestCase
268
272
  opts = "a"
269
273
  assert_equal [ "a" ], getopt(av, opts)
270
274
  $stderr = StringIO.new
271
- assert_equal [ :unknown_option, "-" ], getopt(av, opts)
275
+ assert_equal [ :unknown_option, "--" ], getopt(av, opts)
272
276
  $stderr = STDERR
273
277
  end
274
278
 
@@ -292,7 +296,7 @@ class Test_getopt < Test::Unit::TestCase
292
296
  assert_equal [ "a" ], getopt(av, opts, lopts)
293
297
  assert_equal [ "-" ], getopt(av, opts, lopts)
294
298
  $stderr = StringIO.new
295
- assert_equal [ :unknown_option, "f" ], getopt(av, opts, lopts)
299
+ assert_equal [ :unknown_option, "-f" ], getopt(av, opts, lopts)
296
300
  $stderr = STDERR
297
301
  end
298
302
 
@@ -353,8 +357,8 @@ class Test_getopt < Test::Unit::TestCase
353
357
  got = getopt(av, "", lopts)
354
358
  msg = $stderr.string
355
359
  $stderr = STDERR
356
- assert_equal [ :unknown_option, "bar" ], got
357
- assert_equal "#{myname}: unknown option --bar\n", msg
360
+ assert_equal [ :unknown_option, "--bar" ], got
361
+ assert_equal "#{myname}: Unknown option: --bar\n", msg
358
362
  end
359
363
 
360
364
  test "no_argument" do
@@ -374,8 +378,8 @@ class Test_getopt < Test::Unit::TestCase
374
378
  got = getopt(av, "", lopts)
375
379
  msg = $stderr.string
376
380
  $stderr = STDERR
377
- assert_equal [ :argument_given, "foo" ], got
378
- assert_equal "#{myname}: option doesn't take an argument --foo\n", msg
381
+ assert_equal [ :argument_given, "--foo" ], got
382
+ assert_equal "#{myname}: Option doesn't take an argument: --foo\n", msg
379
383
  end
380
384
 
381
385
  test "no_argument, exception" do
@@ -387,8 +391,8 @@ class Test_getopt < Test::Unit::TestCase
387
391
  getopt(av, "", lopts, use_exception:true)
388
392
  rescue => ex
389
393
  assert_equal Getopt::ArgumentGivenError, ex.class
390
- assert_equal "foo", ex.option
391
- assert_equal "option doesn't take an argument", ex.message
394
+ assert_equal "--foo", ex.option
395
+ assert_equal "Option doesn't take an argument", ex.message
392
396
  end
393
397
  end
394
398
 
@@ -425,8 +429,8 @@ class Test_getopt < Test::Unit::TestCase
425
429
  got = getopt(av, "", lopts)
426
430
  msg = $stderr.string
427
431
  $stderr = STDERR
428
- assert_equal [ :argument_required, "foo" ], got
429
- assert_equal "#{myname}: option requires an argument --foo\n", msg
432
+ assert_equal [ :argument_required, "--foo" ], got
433
+ assert_equal "#{myname}: Option requires an argument: --foo\n", msg
430
434
  end
431
435
 
432
436
  test "required_argument, exception" do
@@ -438,8 +442,8 @@ class Test_getopt < Test::Unit::TestCase
438
442
  getopt(av, "", lopts, use_exception:true)
439
443
  rescue => ex
440
444
  assert_equal Getopt::ArgumentRequiredError, ex.class
441
- assert_equal "foo", ex.option
442
- assert_equal "option requires an argument", ex.message
445
+ assert_equal "--foo", ex.option
446
+ assert_equal "Option requires an argument", ex.message
443
447
  end
444
448
  end
445
449
 
@@ -513,8 +517,8 @@ class Test_getopt < Test::Unit::TestCase
513
517
  got = getopt(av, "", lopts)
514
518
  msg = $stderr.string
515
519
  $stderr = STDERR
516
- assert_equal [ :ambiguous_option, "foo", ["foo1", "foo2"] ], got
517
- assert_equal "#{myname}: ambiguos option (--foo1 --foo2) --foo\n", msg
520
+ assert_equal [ :ambiguous_option, ["--foo", [ "--foo1", "--foo2" ]]], got
521
+ assert_equal "#{myname}: Ambiguos option (--foo1,--foo2): --foo\n", msg
518
522
  end
519
523
 
520
524
  test "abbrev, ambiguous, exception" do
@@ -527,8 +531,8 @@ class Test_getopt < Test::Unit::TestCase
527
531
  getopt(av, "", lopts, use_exception:true)
528
532
  rescue => ex
529
533
  assert_equal Getopt::AmbiguousOptionError, ex.class
530
- assert_equal "foo", ex.option
531
- assert_equal "ambiguos option (--foo1 --foo2)", ex.message
534
+ assert_equal "--foo", ex.option
535
+ assert_equal "Ambiguos option (--foo1,--foo2)", ex.message
532
536
  end
533
537
  end
534
538
 
@@ -541,7 +545,64 @@ class Test_getopt < Test::Unit::TestCase
541
545
  got = getopt(av, "", lopts, abbreviation:false)
542
546
  msg = $stderr.string
543
547
  $stderr = STDERR
544
- assert_equal [ :unknown_option, "fo" ], got
545
- assert_equal "#{myname}: unknown option --fo\n", msg
548
+ assert_equal [ :unknown_option, "--fo" ], got
549
+ assert_equal "#{myname}: Unknown option: --fo\n", msg
546
550
  end
551
+
552
+ # Getopt.parse
553
+
554
+ test "Getopt.parse" do
555
+ av = [ "-a", "foo", "-bx", "-x",
556
+ "-c", "-cx",
557
+ "--noarg", "--reqarg=x",
558
+ "--oparg", "--oparg=x" ]
559
+ lopts = {
560
+ "noarg" => :no_argument,
561
+ "reqarg" => :required_argument,
562
+ "oparg" => :optional_argument,
563
+ }
564
+
565
+ got = Getopt.parse(av, "ab:c::", lopts)
566
+
567
+ assert_equal [ [ :short, :no_argument, "-a" ],
568
+ "foo",
569
+ [ :short, :required_argument, "-b", "x" ],
570
+ [ :error, :unknown_option, "-x" ],
571
+ [ :short, :optional_argument, "-c", nil ],
572
+ [ :short, :optional_argument, "-c", "x" ],
573
+ [ :long, :no_argument, "--noarg" ],
574
+ [ :long, :required_argument, "--reqarg", "x" ],
575
+ [ :long, :optional_argument, "--oparg", nil ],
576
+ [ :long, :optional_argument, "--oparg", "x" ]],
577
+ got
578
+ end
579
+
580
+ # Getopt.list
581
+
582
+ test "Getopt.list" do
583
+ av = [ "-a", "foo", "-bx", "-x",
584
+ "-c", "-cx",
585
+ "--noarg", "--reqarg=x",
586
+ "--oparg", "--oparg=x" ]
587
+ lopts = {
588
+ "noarg" => :no_argument,
589
+ "reqarg" => :required_argument,
590
+ "oparg" => :optional_argument,
591
+ }
592
+
593
+ got = Getopt.list(av, "ab:c::", lopts)
594
+
595
+ assert_equal [ "-a",
596
+ "foo",
597
+ %|-b "x"|,
598
+ [ :error, :unknown_option, "-x" ],
599
+ "-c",
600
+ %|-c"x"|,
601
+ "--noarg",
602
+ %|--reqarg="x"|,
603
+ "--oparg",
604
+ %|--oparg="x"| ],
605
+ got
606
+ end
607
+
547
608
  end
data/trad-getopt.rb CHANGED
@@ -1,8 +1,6 @@
1
- # rather traditional getopt for Ruby
2
-
3
1
  module Getopt
4
2
 
5
- VERSION = "1.2.1"
3
+ VERSION = "2.0.0"
6
4
 
7
5
  class GetoptError < StandardError
8
6
  def initialize option, message
@@ -11,48 +9,171 @@ module Getopt
11
9
  end
12
10
  attr_reader :option
13
11
  end
12
+
14
13
  class UnknownOptionError < GetoptError
15
- def initialize option, message = "unknown option"
14
+ def initialize option, message = "Unknown option"
16
15
  super option, message
17
16
  end
18
17
  end
18
+
19
19
  class ArgumentRequiredError < GetoptError
20
- def initialize option, message = "option requires an argument"
20
+ def initialize option, message = "Option requires an argument"
21
21
  super option, message
22
22
  end
23
23
  end
24
+
24
25
  class ArgumentGivenError < GetoptError
25
- def initialize option, message = "option doesn't take an argument"
26
+ def initialize option, message = "Option doesn't take an argument"
26
27
  super option, message
27
28
  end
28
29
  end
30
+
29
31
  class AmbiguousOptionError < GetoptError
30
32
  def initialize option, message, candidates
31
- message ||= "ambiguos option"
33
+ message ||= "Ambiguos option"
32
34
  @candidates = candidates
33
- cands = candidates.collect { |c| "--#{c}" }.join " "
34
- super option, "#{message} (#{cands})"
35
+ super option, %|#{message} (#{candidates.join ","})|
35
36
  end
36
37
  attr_reader :candidates
37
38
  end
39
+
40
+ def self.parse argv, opts, longopts,
41
+ abbreviation: true,
42
+ allow_empty_optarg: true,
43
+ optional_short: true,
44
+ permute: false,
45
+ program_name: nil,
46
+ stop_by_double_hyphen: true,
47
+ use_exception: false
48
+ # error_message:
49
+ # parse:
50
+
51
+ xargv = []
52
+ loop {
53
+ ret = getopt argv, opts, longopts,
54
+ error_message: false,
55
+ parse: true,
56
+ #
57
+ abbreviation: abbreviation,
58
+ allow_empty_optarg: allow_empty_optarg,
59
+ optional_short: optional_short,
60
+ permute: permute,
61
+ program_name: program_name,
62
+ stop_by_double_hyphen: stop_by_double_hyphen,
63
+ use_exception: use_exception
64
+
65
+ if ret == :stop
66
+ xargv.push :stop
67
+ xargv.concat argv
68
+ break
69
+ elsif ret.nil?
70
+ xargv.push argv.shift
71
+ else
72
+ xargv.push ret
73
+ end
74
+
75
+ break if argv.empty?
76
+ }
77
+ xargv
78
+ end
79
+
80
+ def self.list argv, opts, longopts,
81
+ abbreviation: true,
82
+ allow_empty_optarg: true,
83
+ optional_short: true,
84
+ permute: false,
85
+ program_name: nil,
86
+ stop_by_double_hyphen: true,
87
+ use_exception: false
88
+ # error_message:
89
+ # parse:
90
+
91
+ xargv = []
92
+ loop {
93
+ ret = getopt argv, opts, longopts,
94
+ error_message: false,
95
+ parse: true,
96
+ #
97
+ abbreviation: abbreviation,
98
+ allow_empty_optarg: allow_empty_optarg,
99
+ optional_short: optional_short,
100
+ permute: permute,
101
+ program_name: program_name,
102
+ stop_by_double_hyphen: stop_by_double_hyphen,
103
+ use_exception: use_exception
104
+
105
+ if ret == :stop
106
+ xargv.push "--"
107
+ xargv.concat argv
108
+ break
109
+ end
110
+
111
+ if ret.nil?
112
+ xargv.push argv.shift
113
+ else
114
+ case ret.first
115
+ when :error
116
+ xargv.push ret
117
+ when :short
118
+ case ret[1]
119
+ when :no_argument
120
+ xargv.push ret[2]
121
+ when :required_argument
122
+ xargv.push %|#{ret[2]} "#{ret[3]}"|
123
+ when :optional_argument
124
+ if ret[3]
125
+ xargv.push %|#{ret[2]}"#{ret[3]}"|
126
+ else
127
+ xargv.push ret[2]
128
+ end
129
+ end
130
+ when :long
131
+ case ret[1]
132
+ when :no_argument
133
+ xargv.push ret[2]
134
+ when :required_argument
135
+ xargv.push %|#{ret[2]}="#{ret[3]}"|
136
+ when :optional_argument
137
+ if ret[3]
138
+ xargv.push %|#{ret[2]}="#{ret[3]}"|
139
+ else
140
+ xargv.push ret[2]
141
+ end
142
+ end
143
+ end
144
+ end
145
+
146
+ break if argv.empty?
147
+ }
148
+ xargv
149
+ end
150
+
38
151
  end
39
152
 
40
- def getopt(argv, opts, longopts = nil,
41
- abbreviation: true, allow_empty_optarg: true,
42
- error_message: true, optional_short: true, permute: false,
43
- program_name: nil, stop_by_double_hyphen: true,
44
- use_exception: false)
153
+ def getopt argv, opts, longopts = nil,
154
+ abbreviation: true,
155
+ allow_empty_optarg: true,
156
+ error_message: true,
157
+ optional_short: true,
158
+ parse: false,
159
+ permute: false,
160
+ program_name: nil,
161
+ stop_by_double_hyphen: true,
162
+ use_exception: false
163
+
45
164
  if permute
46
165
  args = []
47
166
  until op = getopt(argv, opts, longopts,
48
- permute: false,
49
167
  abbreviation: abbreviation,
50
168
  allow_empty_optarg: allow_empty_optarg,
51
169
  error_message: error_message,
52
170
  optional_short: optional_short,
171
+ parse: parse,
172
+ permute: false,
53
173
  program_name: program_name,
54
174
  stop_by_double_hyphen: stop_by_double_hyphen,
55
175
  use_exception: use_exception)
176
+
56
177
  if argv.empty?
57
178
  argv.unshift(*args)
58
179
  return nil
@@ -67,68 +188,79 @@ def getopt(argv, opts, longopts = nil,
67
188
  program_name ||= File.basename $0
68
189
 
69
190
  arg = argv.shift
70
- return nil if arg.nil?
191
+ return nil unless arg
192
+
71
193
  if arg == :getopt_short
72
194
  arg = argv.shift
73
- return nil if arg.nil?
195
+ return nil unless arg
74
196
  else
75
197
  if arg == "-"
76
198
  argv.unshift "-"
77
199
  return nil
78
200
  end
79
201
  if stop_by_double_hyphen && arg == "--"
80
- return nil
202
+ return parse ? :stop : nil
81
203
  end
204
+
82
205
  # long option
83
206
  if longopts && arg.index("--") == 0
207
+
84
208
  optopt, optarg = arg[2..-1].split "=", 2
85
209
 
86
210
  if abbreviation
87
- abbr = longopts.collect { |o| o if o[0] =~ /^#{optopt}/ }.compact
211
+ abbr = longopts.collect {|o| o if o[0] =~ /^#{optopt}/ }.compact
88
212
  if abbr.empty?
89
- raise Getopt::UnknownOptionError.new optopt
213
+ raise Getopt::UnknownOptionError.new "--#{optopt}"
90
214
  end
91
215
  if abbr.size == 1
92
216
  optopt = abbr.first.first
93
- elsif exact = abbr.find { |a| a[0] == optopt }
217
+ elsif exact = abbr.find {|a| a[0] == optopt }
94
218
  optopt = exact.first
95
219
  else
96
- cands = abbr.collect { |o| o.first }
97
- raise Getopt::AmbiguousOptionError.new optopt, nil, cands
220
+ cands = abbr.collect {|o| "--#{o.first}" }
221
+ raise Getopt::AmbiguousOptionError.new "--#{optopt}", nil, cands
98
222
  end
99
223
  else
100
224
  unless longopts[optopt]
101
- raise Getopt::UnknownOptionError.new optopt
225
+ raise Getopt::UnknownOptionError.new "--#{optopt}"
102
226
  end
103
227
  end
104
228
 
105
229
  case longopts[optopt]
106
230
  when :no_argument
107
231
  if optarg
108
- raise Getopt::ArgumentGivenError.new optopt
232
+ raise Getopt::ArgumentGivenError.new "--#{optopt}"
109
233
  end
110
- return [ optopt ]
234
+ ret = [ optopt ]
235
+ info = [ :long, :no_argument ]
111
236
  when :required_argument
112
237
  unless optarg
113
238
  optarg = argv.shift
114
239
  unless optarg
115
- raise Getopt::ArgumentRequiredError.new optopt
240
+ raise Getopt::ArgumentRequiredError.new "--#{optopt}"
116
241
  end
117
242
  end
118
243
  if ! allow_empty_optarg && optarg.empty?
119
- raise Getopt::ArgumentRequiredError.new optopt
244
+ raise Getopt::ArgumentRequiredError.new "--#{optopt}"
120
245
  end
121
- return [ optopt, optarg ]
246
+ ret = [ optopt, optarg ]
247
+ info = [ :long, :required_argument ]
122
248
  when :optional_argument
123
249
  if ! allow_empty_optarg && optarg && optarg.empty?
124
- raise Getopt::ArgumentRequiredError.new optopt
250
+ raise Getopt::ArgumentRequiredError.new "--#{optopt}"
125
251
  end
126
- return [ optopt, optarg ]
252
+ ret = [ optopt, optarg ]
253
+ info = [ :long, :optional_argument ]
127
254
  else
128
255
  raise ArgumentError,
129
- "wrong long option type - #{longopts[optopt].inspect}"
256
+ "Wrong long option type - #{longopts[optopt].inspect}"
130
257
  end
131
- # NOTREACHED
258
+
259
+ if parse
260
+ ret[0] = "--#{ret[0]}"
261
+ ret = info + ret
262
+ end
263
+ return ret
132
264
  end
133
265
  end
134
266
 
@@ -147,21 +279,23 @@ def getopt(argv, opts, longopts = nil,
147
279
  argv.unshift "-#{arg}" unless arg.empty?
148
280
  # keep short option context on error
149
281
  argv.unshift :getopt_short if arg[0] == "-"
150
- raise Getopt::UnknownOptionError.new optopt
282
+ raise Getopt::UnknownOptionError.new "-#{optopt}"
151
283
  end
152
284
 
153
285
  if opts[pos.succ] == ":"
154
286
  if optional_short && opts[pos.succ.succ] == ":"
155
287
  # short option with optional argument
156
288
  optarg = arg.empty? ? nil : arg
157
- return [ optopt, optarg ]
289
+ ret = [ optopt, optarg ]
290
+ info = [ :short, :optional_argument ]
158
291
  else
159
292
  # short option with required argument
160
293
  optarg = arg.empty? ? argv.shift : arg
161
294
  if optarg.nil? || (optarg.empty? && ! allow_empty_optarg)
162
- raise Getopt::ArgumentRequiredError.new optopt
295
+ raise Getopt::ArgumentRequiredError.new "-#{optopt}"
163
296
  end
164
- return [ optopt, optarg ]
297
+ ret = [ optopt, optarg ]
298
+ info = [ :short, :required_argument ]
165
299
  end
166
300
  else
167
301
  # short option without argument
@@ -169,30 +303,34 @@ def getopt(argv, opts, longopts = nil,
169
303
  argv.unshift "-#{arg}"
170
304
  argv.unshift :getopt_short if arg[0] == "-"
171
305
  end
172
- return [ optopt ]
306
+ ret = [ optopt ]
307
+ info = [ :short, :no_argument ]
308
+ end
309
+
310
+ if parse
311
+ ret[0] = "-#{ret[0]}"
312
+ ret = info + ret
173
313
  end
314
+ ret
174
315
 
175
316
  rescue Getopt::GetoptError => ex
176
317
  raise if use_exception
177
318
 
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
319
+ warn "#{program_name}: #{ex.message}: #{ex.option}" if error_message
185
320
 
186
321
  case ex
187
322
  when Getopt::UnknownOptionError
188
- return [ :unknown_option, ex.option ]
323
+ ret = [ :unknown_option, ex.option ]
189
324
  when Getopt::ArgumentRequiredError
190
- return [ :argument_required, ex.option ]
325
+ ret = [ :argument_required, ex.option ]
191
326
  when Getopt::ArgumentGivenError
192
- return [ :argument_given, ex.option ]
327
+ ret = [ :argument_given, ex.option ]
193
328
  when Getopt::AmbiguousOptionError
194
- return [ :ambiguous_option, ex.option, ex.candidates ]
329
+ ret = [ :ambiguous_option, [ ex.option, ex.candidates ]]
195
330
  end
331
+
332
+ ret.unshift :error if parse
333
+ return ret
196
334
  end
197
335
 
198
336
  Kernel.define_singleton_method "getopt", method(:getopt)
data/trad-getopt.txt CHANGED
@@ -95,8 +95,9 @@ longopts にはキーをオプション名、値をシンボルとするハッ
95
95
 
96
96
  - :ambiguousu_option (Getopt::AmbiguousOptionError)
97
97
  ロングオプションの省略形が一意でない。このときエラーメッセージに候補
98
- を示す。また返り値の第 3 要素にも候補のリストを置く。例外を有効とし
99
- ている場合は Getopt::AmbiguousOptionError#candidates() で取得できる。
98
+ を示す。:ambiguousu_option のみ返り値の第 2 要素はオプション文字と
99
+ 候補リストのリストになる。例外を有効としている場合、候補リストは
100
+ Getopt::AmbiguousOptionError#candidates() で取得できる。
100
101
 
101
102
  = キーワード引数
102
103
 
@@ -115,6 +116,10 @@ longopts にはキーをオプション名、値をシンボルとするハッ
115
116
  偽にするとオプショナル引数つきショートオプションを無効にする。opts
116
117
  中の "::" を ":" と解釈してただの引数つきオプションとする。
117
118
 
119
+ - parse => false
120
+ 真にすると、オプション文字でなくオプションに関する情報をまとめたリスト
121
+ を返すようになる。Getopt.parse()、Getopt.list() が使う。
122
+
118
123
  - permute => false
119
124
  真にすると、オプションに出会うまで非オプションを飛ばす GNU の getopt
120
125
  を模した動作になる。argv 中にオプションが無ければ nil を返す。読んだ
@@ -160,31 +165,25 @@ longopts にはキーをオプション名、値をシンボルとするハッ
160
165
  ----------------------------------------------------------------------------
161
166
  require "trad-getopt"
162
167
 
163
- opts = "ab:"
164
-
165
168
  longopts = {
166
169
  "foo" => :no_argument,
167
- "foo-bar" => :optional_argument,
168
- "baz" => :required_argument,
170
+ "bar" => :required_argument,
171
+ "baz" => :optional_argument,
169
172
  }
170
173
 
171
- while op = getopt(ARGV, opts, longopts, permute:false)
172
- case op[0]
174
+ while (op, oparg = getopt(ARGV, "ab:c::", longopts))
175
+ case op
173
176
  when "a", "foo"
174
- puts "option #{op[0]}"
175
- when "b", "baz"
176
- puts "option #{op[0]} with #{op[1]}"
177
- when "foo-bar"
178
- if op[1]
179
- puts "option #{op[0]} with #{op[1]}"
180
- else
181
- puts "option #{op[0]} with no-arg"
182
- end
183
- when :unknown_option, :argument_required, :argument_given, :ambiguous_option
177
+ puts "option #{op}"
178
+ when "b", "bar"
179
+ puts "option #{op} with #{oparg.inspect}"
180
+ when "c", "baz"
181
+ puts "option #{op} with #{oparg.inspect}"
182
+ when Symbol
183
+ # errors
184
184
  exit 1
185
185
  end
186
186
  end
187
+
187
188
  puts "argv #{ARGV}"
188
189
  ----------------------------------------------------------------------------
189
-
190
- #eof
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.2.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - NOZAWA Hiromasa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-16 00:00:00.000000000 Z
11
+ date: 2019-04-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: