optparse 0.6.0 → 0.7.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7133ed3cb8069a5f9e30dc050bdf3f593c0c6c1cdb438662846922cb36efb6ca
4
- data.tar.gz: 2f831a85adf7093e20bfb5894a28864475fa0fac996c1076d9746cf2466d3587
3
+ metadata.gz: 75528067add8b99394cfaa038be937a623a7fda39714a19d06e6547f0fbd0d3e
4
+ data.tar.gz: 9587c77cbedaad767f474737cd226f120706e56432c4dc61dda83efc8a157f63
5
5
  SHA512:
6
- metadata.gz: 7696cd28fc5ea95a88bdb9815d67e916787ba891baf526e4c966bbf28c8893ad63c6835967f5907f7ff3b0932e8e07e058cc0028a13a5e8fb6f4433d245026b0
7
- data.tar.gz: 71b51296c92eaeb0ec443a5d502f81fda00884e6cf8b77c8cfff8e5cbe60d5a27d6b170c3107235028136c0c6953e6ba60d08a2744ea890d96d213ba2e31b5b5
6
+ metadata.gz: 402d2a4aa4f89f43250e459d8a254049b977c33882c31069d17ed2cc2057b30cc4113372bf3ee082c000dbbc80a4c92a1e0f4dbad6a9ab6798605ff68721a33c
7
+ data.tar.gz: 0a2cc0d212fd7d70aa8822bd7d93f3ea772d19952b99307660235db613d33b637ac5e3d9334a4cf5dd103573ce2382be9f8d4d664b73f4f5644fd3e9ae537434
data/.rdoc_options CHANGED
@@ -2,3 +2,4 @@
2
2
  page_dir: doc
3
3
  main_page: README.md
4
4
  title: Documentation for OptionParser
5
+ op_dir: rdoc
data/BSDL ADDED
@@ -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.
@@ -377,4 +377,4 @@ Executions:
377
377
  $ ruby match_converter.rb --capitalize foo
378
378
  ["Foo", String]
379
379
  $ ruby match_converter.rb --capitalize "foo bar"
380
- match_converter.rb:9:in `<main>': invalid argument: --capitalize foo bar (OptionParser::InvalidArgument)
380
+ match_converter.rb:9:in '<main>': invalid argument: --capitalize foo bar (OptionParser::InvalidArgument)
@@ -91,7 +91,7 @@ Executions:
91
91
  Usage: short_required [options]
92
92
  -xXXX Short name with required argument
93
93
  $ ruby short_required.rb -x
94
- short_required.rb:6:in `<main>': missing argument: -x (OptionParser::MissingArgument)
94
+ short_required.rb:6:in '<main>': missing argument: -x (OptionParser::MissingArgument)
95
95
  $ ruby short_required.rb -x FOO
96
96
  ["-x", "FOO"]
97
97
 
@@ -181,7 +181,7 @@ Executions:
181
181
  Usage: long_required [options]
182
182
  --xxx XXX Long name with required argument
183
183
  $ ruby long_required.rb --xxx
184
- long_required.rb:6:in `<main>': missing argument: --xxx (OptionParser::MissingArgument)
184
+ long_required.rb:6:in '<main>': missing argument: --xxx (OptionParser::MissingArgument)
185
185
  $ ruby long_required.rb --xxx FOO
186
186
  ["--xxx", "FOO"]
187
187
 
@@ -243,11 +243,11 @@ Usage: mixed_names [options]
243
243
  $ ruby mixed_names.rb --xxx
244
244
  ["--xxx", true]
245
245
  $ ruby mixed_names.rb -y
246
- mixed_names.rb:12:in `<main>': missing argument: -y (OptionParser::MissingArgument)
246
+ mixed_names.rb:12:in '<main>': missing argument: -y (OptionParser::MissingArgument)
247
247
  $ ruby mixed_names.rb -y FOO
248
248
  ["--yyy", "FOO"]
249
249
  $ ruby mixed_names.rb --yyy
250
- mixed_names.rb:12:in `<main>': missing argument: --yyy (OptionParser::MissingArgument)
250
+ mixed_names.rb:12:in '<main>': missing argument: --yyy (OptionParser::MissingArgument)
251
251
  $ ruby mixed_names.rb --yyy BAR
252
252
  ["--yyy", "BAR"]
253
253
  $ ruby mixed_names.rb -z
@@ -279,7 +279,7 @@ Executions:
279
279
  Usage: argument_keywords [options]
280
280
  -x, --xxx Required argument
281
281
  $ ruby argument_styles.rb --xxx
282
- argument_styles.rb:6:in `<main>': missing argument: --xxx (OptionParser::MissingArgument)
282
+ argument_styles.rb:6:in '<main>': missing argument: --xxx (OptionParser::MissingArgument)
283
283
  $ ruby argument_styles.rb --xxx FOO
284
284
  ["--xxx", "FOO"]
285
285
 
@@ -298,7 +298,7 @@ Executions:
298
298
  Usage: argument_strings [options]
299
299
  -x, --xxx=XXX Required argument
300
300
  $ ruby argument_strings.rb --xxx
301
- argument_strings.rb:9:in `<main>': missing argument: --xxx (OptionParser::MissingArgument)
301
+ argument_strings.rb:9:in '<main>': missing argument: --xxx (OptionParser::MissingArgument)
302
302
  $ ruby argument_strings.rb --xxx FOO
303
303
  ["--xxx", "FOO"]
304
304
 
@@ -331,7 +331,7 @@ Executions:
331
331
  -xXXX Values for required argument
332
332
  -y [YYY] Values for optional argument
333
333
  $ ruby explicit_array_values.rb -x
334
- explicit_array_values.rb:9:in `<main>': missing argument: -x (OptionParser::MissingArgument)
334
+ explicit_array_values.rb:9:in '<main>': missing argument: -x (OptionParser::MissingArgument)
335
335
  $ ruby explicit_array_values.rb -x foo
336
336
  ["-x", "foo"]
337
337
  $ ruby explicit_array_values.rb -x f
@@ -339,9 +339,9 @@ Executions:
339
339
  $ ruby explicit_array_values.rb -x bar
340
340
  ["-x", "bar"]
341
341
  $ ruby explicit_array_values.rb -y ba
342
- explicit_array_values.rb:9:in `<main>': ambiguous argument: -y ba (OptionParser::AmbiguousArgument)
342
+ explicit_array_values.rb:9:in '<main>': ambiguous argument: -y ba (OptionParser::AmbiguousArgument)
343
343
  $ ruby explicit_array_values.rb -x baz
344
- explicit_array_values.rb:9:in `<main>': invalid argument: -x baz (OptionParser::InvalidArgument)
344
+ explicit_array_values.rb:9:in '<main>': invalid argument: -x baz (OptionParser::InvalidArgument)
345
345
 
346
346
 
347
347
  ===== Explicit Values in Hash
@@ -361,7 +361,7 @@ Executions:
361
361
  -xXXX Values for required argument
362
362
  -y [YYY] Values for optional argument
363
363
  $ ruby explicit_hash_values.rb -x
364
- explicit_hash_values.rb:9:in `<main>': missing argument: -x (OptionParser::MissingArgument)
364
+ explicit_hash_values.rb:9:in '<main>': missing argument: -x (OptionParser::MissingArgument)
365
365
  $ ruby explicit_hash_values.rb -x foo
366
366
  ["-x", 0]
367
367
  $ ruby explicit_hash_values.rb -x f
@@ -369,7 +369,7 @@ Executions:
369
369
  $ ruby explicit_hash_values.rb -x bar
370
370
  ["-x", 1]
371
371
  $ ruby explicit_hash_values.rb -x baz
372
- explicit_hash_values.rb:9:in `<main>': invalid argument: -x baz (OptionParser::InvalidArgument)
372
+ explicit_hash_values.rb:9:in '<main>': invalid argument: -x baz (OptionParser::InvalidArgument)
373
373
  $ ruby explicit_hash_values.rb -y
374
374
  ["-y", nil]
375
375
  $ ruby explicit_hash_values.rb -y baz
@@ -377,14 +377,15 @@ Executions:
377
377
  $ ruby explicit_hash_values.rb -y bat
378
378
  ["-y", 3]
379
379
  $ ruby explicit_hash_values.rb -y ba
380
- explicit_hash_values.rb:9:in `<main>': ambiguous argument: -y ba (OptionParser::AmbiguousArgument)
380
+ explicit_hash_values.rb:9:in '<main>': ambiguous argument: -y ba (OptionParser::AmbiguousArgument)
381
381
  $ ruby explicit_hash_values.rb -y bam
382
382
  ["-y", nil]
383
383
 
384
384
  ==== Argument Value Patterns
385
385
 
386
386
  You can restrict permissible argument values
387
- by specifying a Regexp that the given argument must match.
387
+ by specifying a +Regexp+ that the given argument must match,
388
+ or a +Range+ or +Array+ that the converted value must be included in.
388
389
 
389
390
  File +matched_values.rb+ defines options with matched argument values.
390
391
 
@@ -395,12 +396,22 @@ Executions:
395
396
  $ ruby matched_values.rb --help
396
397
  Usage: matched_values [options]
397
398
  --xxx XXX Matched values
399
+ --yyy YYY Check by range
400
+ --zzz ZZZ Check by list
398
401
  $ ruby matched_values.rb --xxx foo
399
402
  ["--xxx", "foo"]
400
403
  $ ruby matched_values.rb --xxx FOO
401
404
  ["--xxx", "FOO"]
402
405
  $ ruby matched_values.rb --xxx bar
403
- matched_values.rb:6:in `<main>': invalid argument: --xxx bar (OptionParser::InvalidArgument)
406
+ matched_values.rb:12:in '<main>': invalid argument: --xxx bar (OptionParser::InvalidArgument)
407
+ $ ruby matched_values.rb --yyy 1
408
+ ["--yyy", 1]
409
+ $ ruby matched_values.rb --yyy 4
410
+ matched_values.rb:12:in '<main>': invalid argument: --yyy 4 (OptionParser::InvalidArgument)
411
+ $ ruby matched_values.rb --zzz 1
412
+ ["--zzz", 1]
413
+ $ ruby matched_values.rb --zzz 2
414
+ matched_values.rb:12:in '<main>': invalid argument: --zzz 2 (OptionParser::InvalidArgument)
404
415
 
405
416
  === Argument Converters
406
417
 
@@ -3,4 +3,10 @@ parser = OptionParser.new
3
3
  parser.on('--xxx XXX', /foo/i, 'Matched values') do |value|
4
4
  p ['--xxx', value]
5
5
  end
6
+ parser.on('--yyy YYY', Integer, 'Check by range', 1..3) do |value|
7
+ p ['--yyy', value]
8
+ end
9
+ parser.on('--zzz ZZZ', Integer, 'Check by list', [1, 3, 4]) do |value|
10
+ p ['--zzz', value]
11
+ end
6
12
  parser.parse!
@@ -111,7 +111,7 @@ Executions:
111
111
  ["x", true]
112
112
  ["input_file.txt", "output_file.txt"]
113
113
  $ ruby basic.rb -a
114
- basic.rb:16:in `<main>': invalid option: -a (OptionParser::InvalidOption)
114
+ basic.rb:16:in '<main>': invalid option: -a (OptionParser::InvalidOption)
115
115
 
116
116
  === Defining Options
117
117
 
@@ -232,11 +232,11 @@ Executions:
232
232
  $ ruby mixed_names.rb --xxx
233
233
  ["--xxx", true]
234
234
  $ ruby mixed_names.rb -y
235
- mixed_names.rb:12:in `<main>': missing argument: -y (OptionParser::MissingArgument)
235
+ mixed_names.rb:12:in '<main>': missing argument: -y (OptionParser::MissingArgument)
236
236
  $ ruby mixed_names.rb -y FOO
237
237
  ["--yyy", "FOO"]
238
238
  $ ruby mixed_names.rb --yyy
239
- mixed_names.rb:12:in `<main>': missing argument: --yyy (OptionParser::MissingArgument)
239
+ mixed_names.rb:12:in '<main>': missing argument: --yyy (OptionParser::MissingArgument)
240
240
  $ ruby mixed_names.rb --yyy BAR
241
241
  ["--yyy", "BAR"]
242
242
  $ ruby mixed_names.rb -z
@@ -270,9 +270,9 @@ Executions:
270
270
  $ ruby name_abbrev.rb --draft
271
271
  ["--draft", true]
272
272
  $ ruby name_abbrev.rb --d
273
- name_abbrev.rb:9:in `<main>': ambiguous option: --d (OptionParser::AmbiguousOption)
273
+ name_abbrev.rb:9:in '<main>': ambiguous option: --d (OptionParser::AmbiguousOption)
274
274
  $ ruby name_abbrev.rb --dr
275
- name_abbrev.rb:9:in `<main>': ambiguous option: --dr (OptionParser::AmbiguousOption)
275
+ name_abbrev.rb:9:in '<main>': ambiguous option: --dr (OptionParser::AmbiguousOption)
276
276
  $ ruby name_abbrev.rb --dry
277
277
  ["--dry-run", true]
278
278
  $ ruby name_abbrev.rb --dra
@@ -285,7 +285,7 @@ You can disable abbreviation using method +require_exact+.
285
285
  Executions:
286
286
 
287
287
  $ ruby no_abbreviation.rb --dry-ru
288
- no_abbreviation.rb:10:in `<main>': invalid option: --dry-ru (OptionParser::InvalidOption)
288
+ no_abbreviation.rb:10:in '<main>': invalid option: --dry-ru (OptionParser::InvalidOption)
289
289
  $ ruby no_abbreviation.rb --dry-run
290
290
  ["--dry-run", true]
291
291
 
@@ -323,7 +323,7 @@ Executions:
323
323
  Omitting a required argument raises an error:
324
324
 
325
325
  $ ruby required_argument.rb -x
326
- required_argument.rb:9:in `<main>': missing argument: -x (OptionParser::MissingArgument)
326
+ required_argument.rb:9:in '<main>': missing argument: -x (OptionParser::MissingArgument)
327
327
 
328
328
  ==== Option with Optional Argument
329
329
 
@@ -369,7 +369,7 @@ Executions:
369
369
  $ ruby argument_abbreviation.rb --xxx A
370
370
  ["--xxx", "ABC"]
371
371
  $ ruby argument_abbreviation.rb --xxx c
372
- argument_abbreviation.rb:9:in `<main>': invalid argument: --xxx c (OptionParser::InvalidArgument)
372
+ argument_abbreviation.rb:9:in '<main>': invalid argument: --xxx c (OptionParser::InvalidArgument)
373
373
  $ ruby argument_abbreviation.rb --yyy a --yyy d
374
374
  ["--yyy", "XYZ"]
375
375
  ["--yyy", "FOO"]
@@ -403,7 +403,7 @@ Executions:
403
403
  -xXXX Values for required argument
404
404
  -y [YYY] Values for optional argument
405
405
  $ ruby explicit_array_values.rb -x
406
- explicit_array_values.rb:9:in `<main>': missing argument: -x (OptionParser::MissingArgument)
406
+ explicit_array_values.rb:9:in '<main>': missing argument: -x (OptionParser::MissingArgument)
407
407
  $ ruby explicit_array_values.rb -x foo
408
408
  ["-x", "foo"]
409
409
  $ ruby explicit_array_values.rb -x f
@@ -411,9 +411,9 @@ Executions:
411
411
  $ ruby explicit_array_values.rb -x bar
412
412
  ["-x", "bar"]
413
413
  $ ruby explicit_array_values.rb -y ba
414
- explicit_array_values.rb:9:in `<main>': ambiguous argument: -y ba (OptionParser::AmbiguousArgument)
414
+ explicit_array_values.rb:9:in '<main>': ambiguous argument: -y ba (OptionParser::AmbiguousArgument)
415
415
  $ ruby explicit_array_values.rb -x baz
416
- explicit_array_values.rb:9:in `<main>': invalid argument: -x baz (OptionParser::InvalidArgument)
416
+ explicit_array_values.rb:9:in '<main>': invalid argument: -x baz (OptionParser::InvalidArgument)
417
417
 
418
418
 
419
419
  ===== Explicit Values in Hash
@@ -433,7 +433,7 @@ Executions:
433
433
  -xXXX Values for required argument
434
434
  -y [YYY] Values for optional argument
435
435
  $ ruby explicit_hash_values.rb -x
436
- explicit_hash_values.rb:9:in `<main>': missing argument: -x (OptionParser::MissingArgument)
436
+ explicit_hash_values.rb:9:in '<main>': missing argument: -x (OptionParser::MissingArgument)
437
437
  $ ruby explicit_hash_values.rb -x foo
438
438
  ["-x", 0]
439
439
  $ ruby explicit_hash_values.rb -x f
@@ -441,7 +441,7 @@ Executions:
441
441
  $ ruby explicit_hash_values.rb -x bar
442
442
  ["-x", 1]
443
443
  $ ruby explicit_hash_values.rb -x baz
444
- explicit_hash_values.rb:9:in `<main>': invalid argument: -x baz (OptionParser::InvalidArgument)
444
+ explicit_hash_values.rb:9:in '<main>': invalid argument: -x baz (OptionParser::InvalidArgument)
445
445
  $ ruby explicit_hash_values.rb -y
446
446
  ["-y", nil]
447
447
  $ ruby explicit_hash_values.rb -y baz
@@ -449,7 +449,7 @@ Executions:
449
449
  $ ruby explicit_hash_values.rb -y bat
450
450
  ["-y", 3]
451
451
  $ ruby explicit_hash_values.rb -y ba
452
- explicit_hash_values.rb:9:in `<main>': ambiguous argument: -y ba (OptionParser::AmbiguousArgument)
452
+ explicit_hash_values.rb:9:in '<main>': ambiguous argument: -y ba (OptionParser::AmbiguousArgument)
453
453
  $ ruby explicit_hash_values.rb -y bam
454
454
  ["-y", nil]
455
455
 
@@ -472,7 +472,7 @@ Executions:
472
472
  $ ruby matched_values.rb --xxx FOO
473
473
  ["--xxx", "FOO"]
474
474
  $ ruby matched_values.rb --xxx bar
475
- matched_values.rb:6:in `<main>': invalid argument: --xxx bar (OptionParser::InvalidArgument)
475
+ matched_values.rb:6:in '<main>': invalid argument: --xxx bar (OptionParser::InvalidArgument)
476
476
 
477
477
  === Keyword Argument +into+
478
478
 
@@ -524,7 +524,7 @@ Executions:
524
524
  -y, --yyyYYY Short and long, required argument
525
525
  -z, --zzz [ZZZ] Short and long, optional argument
526
526
  $ ruby missing_options.rb --yyy FOO
527
- missing_options.rb:11:in `<main>': Missing required options: [:xxx, :zzz] (RuntimeError)
527
+ missing_options.rb:11:in '<main>': Missing required options: [:xxx, :zzz] (RuntimeError)
528
528
 
529
529
  ==== Default Values for Options
530
530
 
data/lib/optparse.rb CHANGED
@@ -7,6 +7,7 @@
7
7
  #
8
8
  # See OptionParser for documentation.
9
9
  #
10
+ require 'set' unless defined?(Set)
10
11
 
11
12
  #--
12
13
  # == Developer Documentation (not for RDoc output)
@@ -142,7 +143,7 @@
142
143
  # Used:
143
144
  #
144
145
  # $ ruby optparse-test.rb -r
145
- # optparse-test.rb:9:in `<main>': missing argument: -r (OptionParser::MissingArgument)
146
+ # optparse-test.rb:9:in '<main>': missing argument: -r (OptionParser::MissingArgument)
146
147
  # $ ruby optparse-test.rb -r my-library
147
148
  # You required my-library!
148
149
  #
@@ -235,7 +236,7 @@
235
236
  # $ ruby optparse-test.rb --user 2
236
237
  # #<struct User id=2, name="Gandalf">
237
238
  # $ ruby optparse-test.rb --user 3
238
- # optparse-test.rb:15:in `block in find_user': No User Found for id 3 (RuntimeError)
239
+ # optparse-test.rb:15:in 'block in find_user': No User Found for id 3 (RuntimeError)
239
240
  #
240
241
  # === Store options to a Hash
241
242
  #
@@ -425,7 +426,8 @@
425
426
  #
426
427
  class OptionParser
427
428
  # The version string
428
- OptionParser::Version = "0.6.0"
429
+ VERSION = "0.7.0"
430
+ Version = VERSION # for compatibility
429
431
 
430
432
  # :stopdoc:
431
433
  NoArgument = [NO_ARGUMENT = :NONE, nil].freeze
@@ -461,6 +463,10 @@ class OptionParser
461
463
  candidates
462
464
  end
463
465
 
466
+ def self.completable?(key)
467
+ String.try_convert(key) or defined?(key.id2name)
468
+ end
469
+
464
470
  def candidate(key, icase = false, pat = nil, &_)
465
471
  Completion.candidate(key, icase, pat, &method(:each))
466
472
  end
@@ -496,7 +502,6 @@ class OptionParser
496
502
  end
497
503
  end
498
504
 
499
-
500
505
  #
501
506
  # Map from option/keyword string to object with completion.
502
507
  #
@@ -504,7 +509,6 @@ class OptionParser
504
509
  include Completion
505
510
  end
506
511
 
507
-
508
512
  #
509
513
  # Individual switch class. Not important to the user.
510
514
  #
@@ -546,11 +550,11 @@ class OptionParser
546
550
 
547
551
  def initialize(pattern = nil, conv = nil,
548
552
  short = nil, long = nil, arg = nil,
549
- desc = ([] if short or long), block = nil, &_block)
553
+ desc = ([] if short or long), block = nil, values = nil, &_block)
550
554
  raise if Array === pattern
551
555
  block ||= _block
552
- @pattern, @conv, @short, @long, @arg, @desc, @block =
553
- pattern, conv, short, long, arg, desc, block
556
+ @pattern, @conv, @short, @long, @arg, @desc, @block, @values =
557
+ pattern, conv, short, long, arg, desc, block, values
554
558
  end
555
559
 
556
560
  #
@@ -583,11 +587,15 @@ class OptionParser
583
587
  # exception.
584
588
  #
585
589
  def conv_arg(arg, val = []) # :nodoc:
590
+ v, = *val
586
591
  if conv
587
592
  val = conv.call(*val)
588
593
  else
589
594
  val = proc {|v| v}.call(*val)
590
595
  end
596
+ if @values
597
+ @values.include?(val) or raise InvalidArgument, v
598
+ end
591
599
  return arg, block, val
592
600
  end
593
601
  private :conv_arg
@@ -668,7 +676,7 @@ class OptionParser
668
676
 
669
677
  (sopts+lopts).each do |opt|
670
678
  # "(-x -c -r)-l[left justify]"
671
- if /^--\[no-\](.+)$/ =~ opt
679
+ if /\A--\[no-\](.+)$/ =~ opt
672
680
  o = $1
673
681
  yield("--#{o}", desc.join(""))
674
682
  yield("--no-#{o}", desc.join(""))
@@ -1032,7 +1040,6 @@ class OptionParser
1032
1040
  DefaultList.short['-'] = Switch::NoArgument.new {}
1033
1041
  DefaultList.long[''] = Switch::NoArgument.new {throw :terminate}
1034
1042
 
1035
-
1036
1043
  COMPSYS_HEADER = <<'XXX' # :nodoc:
1037
1044
 
1038
1045
  typeset -A opt_args
@@ -1051,16 +1058,16 @@ XXX
1051
1058
  end
1052
1059
 
1053
1060
  def help_exit
1054
- if STDOUT.tty? && (pager = ENV.values_at(*%w[RUBY_PAGER PAGER]).find {|e| e && !e.empty?})
1061
+ if $stdout.tty? && (pager = ENV.values_at(*%w[RUBY_PAGER PAGER]).find {|e| e && !e.empty?})
1055
1062
  less = ENV["LESS"]
1056
- args = [{"LESS" => "#{!less || less.empty? ? '-' : less}Fe"}, pager, "w"]
1063
+ args = [{"LESS" => "#{less} -Fe"}, pager, "w"]
1057
1064
  print = proc do |f|
1058
1065
  f.puts help
1059
1066
  rescue Errno::EPIPE
1060
1067
  # pager terminated
1061
1068
  end
1062
1069
  if Process.respond_to?(:fork) and false
1063
- IO.popen("-") {|f| f ? Process.exec(*args, in: f) : print.call(STDOUT)}
1070
+ IO.popen("-") {|f| f ? Process.exec(*args, in: f) : print.call($stdout)}
1064
1071
  # unreachable
1065
1072
  end
1066
1073
  IO.popen(*args, &print)
@@ -1102,7 +1109,7 @@ XXX
1102
1109
  #
1103
1110
  Officious['*-completion-zsh'] = proc do |parser|
1104
1111
  Switch::OptionalArgument.new do |arg|
1105
- parser.compsys(STDOUT, arg)
1112
+ parser.compsys($stdout, arg)
1106
1113
  exit
1107
1114
  end
1108
1115
  end
@@ -1288,7 +1295,15 @@ XXX
1288
1295
  # to $0.
1289
1296
  #
1290
1297
  def program_name
1291
- @program_name || File.basename($0, '.*')
1298
+ @program_name || strip_ext(File.basename($0))
1299
+ end
1300
+
1301
+ private def strip_ext(name) # :nodoc:
1302
+ exts = /#{
1303
+ require "rbconfig"
1304
+ Regexp.union(*RbConfig::CONFIG["EXECUTABLE_EXTS"]&.split(" "))
1305
+ }\z/o
1306
+ name.sub(exts, "")
1292
1307
  end
1293
1308
 
1294
1309
  # for experimental cascading :-)
@@ -1467,6 +1482,7 @@ XXX
1467
1482
  klass = nil
1468
1483
  q, a = nil
1469
1484
  has_arg = false
1485
+ values = nil
1470
1486
 
1471
1487
  opts.each do |o|
1472
1488
  # argument class
@@ -1480,7 +1496,7 @@ XXX
1480
1496
  end
1481
1497
 
1482
1498
  # directly specified pattern(any object possible to match)
1483
- if (!(String === o || Symbol === o)) and o.respond_to?(:match)
1499
+ if !Completion.completable?(o) and o.respond_to?(:match)
1484
1500
  pattern = notwice(o, pattern, 'pattern')
1485
1501
  if pattern.respond_to?(:convert)
1486
1502
  conv = pattern.method(:convert).to_proc
@@ -1494,7 +1510,12 @@ XXX
1494
1510
  case o
1495
1511
  when Proc, Method
1496
1512
  block = notwice(o, block, 'block')
1497
- when Array, Hash
1513
+ when Array, Hash, Set
1514
+ if Array === o
1515
+ o, v = o.partition {|v,| Completion.completable?(v)}
1516
+ values = notwice(v, values, 'values') unless v.empty?
1517
+ next if o.empty?
1518
+ end
1498
1519
  case pattern
1499
1520
  when CompletingHash
1500
1521
  when nil
@@ -1504,11 +1525,13 @@ XXX
1504
1525
  raise ArgumentError, "argument pattern given twice"
1505
1526
  end
1506
1527
  o.each {|pat, *v| pattern[pat] = v.fetch(0) {pat}}
1528
+ when Range
1529
+ values = notwice(o, values, 'values')
1507
1530
  when Module
1508
1531
  raise ArgumentError, "unsupported argument type: #{o}", ParseError.filter_backtrace(caller(4))
1509
1532
  when *ArgumentStyle.keys
1510
1533
  style = notwice(ArgumentStyle[o], style, 'style')
1511
- when /^--no-([^\[\]=\s]*)(.+)?/
1534
+ when /\A--no-([^\[\]=\s]*)(.+)?/
1512
1535
  q, a = $1, $2
1513
1536
  o = notwice(a ? Object : TrueClass, klass, 'type')
1514
1537
  not_pattern, not_conv = search(:atype, o) unless not_style
@@ -1519,7 +1542,7 @@ XXX
1519
1542
  (q = q.downcase).tr!('_', '-')
1520
1543
  long << "no-#{q}"
1521
1544
  nolong << q
1522
- when /^--\[no-\]([^\[\]=\s]*)(.+)?/
1545
+ when /\A--\[no-\]([^\[\]=\s]*)(.+)?/
1523
1546
  q, a = $1, $2
1524
1547
  o = notwice(a ? Object : TrueClass, klass, 'type')
1525
1548
  if a
@@ -1532,7 +1555,7 @@ XXX
1532
1555
  not_pattern, not_conv = search(:atype, FalseClass) unless not_style
1533
1556
  not_style = Switch::NoArgument
1534
1557
  nolong << "no-#{o}"
1535
- when /^--([^\[\]=\s]*)(.+)?/
1558
+ when /\A--([^\[\]=\s]*)(.+)?/
1536
1559
  q, a = $1, $2
1537
1560
  if a
1538
1561
  o = notwice(NilClass, klass, 'type')
@@ -1542,7 +1565,7 @@ XXX
1542
1565
  ldesc << "--#{q}"
1543
1566
  (o = q.downcase).tr!('_', '-')
1544
1567
  long << o
1545
- when /^-(\[\^?\]?(?:[^\\\]]|\\.)*\])(.+)?/
1568
+ when /\A-(\[\^?\]?(?:[^\\\]]|\\.)*\])(.+)?/
1546
1569
  q, a = $1, $2
1547
1570
  o = notwice(Object, klass, 'type')
1548
1571
  if a
@@ -1553,7 +1576,7 @@ XXX
1553
1576
  end
1554
1577
  sdesc << "-#{q}"
1555
1578
  short << Regexp.new(q)
1556
- when /^-(.)(.+)?/
1579
+ when /\A-(.)(.+)?/
1557
1580
  q, a = $1, $2
1558
1581
  if a
1559
1582
  o = notwice(NilClass, klass, 'type')
@@ -1562,7 +1585,7 @@ XXX
1562
1585
  end
1563
1586
  sdesc << "-#{q}"
1564
1587
  short << q
1565
- when /^=/
1588
+ when /\A=/
1566
1589
  style = notwice(default_style.guess(arg = o), style, 'style')
1567
1590
  default_pattern, conv = search(:atype, Object) unless default_pattern
1568
1591
  else
@@ -1571,12 +1594,18 @@ XXX
1571
1594
  end
1572
1595
 
1573
1596
  default_pattern, conv = search(:atype, default_style.pattern) unless default_pattern
1597
+ if Range === values and klass
1598
+ unless (!values.begin or klass === values.begin) and
1599
+ (!values.end or klass === values.end)
1600
+ raise ArgumentError, "range does not match class"
1601
+ end
1602
+ end
1574
1603
  if !(short.empty? and long.empty?)
1575
1604
  if has_arg and default_style == Switch::NoArgument
1576
1605
  default_style = Switch::RequiredArgument
1577
1606
  end
1578
1607
  s = (style || default_style).new(pattern || default_pattern,
1579
- conv, sdesc, ldesc, arg, desc, block)
1608
+ conv, sdesc, ldesc, arg, desc, block, values)
1580
1609
  elsif !block
1581
1610
  if style or pattern
1582
1611
  raise ArgumentError, "no switch given", ParseError.filter_backtrace(caller)
@@ -1585,7 +1614,7 @@ XXX
1585
1614
  else
1586
1615
  short << pattern
1587
1616
  s = (style || default_style).new(pattern,
1588
- conv, nil, nil, arg, desc, block)
1617
+ conv, nil, nil, arg, desc, block, values)
1589
1618
  end
1590
1619
  return s, short, long,
1591
1620
  (not_style.new(not_pattern, not_conv, sdesc, ldesc, nil, desc, block) if not_style),
@@ -1827,7 +1856,7 @@ XXX
1827
1856
  #
1828
1857
  def permute!(argv = default_argv, **keywords)
1829
1858
  nonopts = []
1830
- order!(argv, **keywords, &nonopts.method(:<<))
1859
+ order!(argv, **keywords) {|nonopt| nonopts << nonopt}
1831
1860
  argv[0, 0] = nonopts
1832
1861
  argv
1833
1862
  end
@@ -1880,13 +1909,16 @@ XXX
1880
1909
  single_options, *long_options = *args
1881
1910
 
1882
1911
  result = {}
1912
+ setter = (symbolize_names ?
1913
+ ->(name, val) {result[name.to_sym] = val}
1914
+ : ->(name, val) {result[name] = val})
1883
1915
 
1884
1916
  single_options.scan(/(.)(:)?/) do |opt, val|
1885
1917
  if val
1886
- result[opt] = nil
1918
+ setter[opt, nil]
1887
1919
  define("-#{opt} VAL")
1888
1920
  else
1889
- result[opt] = false
1921
+ setter[opt, false]
1890
1922
  define("-#{opt}")
1891
1923
  end
1892
1924
  end if single_options
@@ -1895,16 +1927,16 @@ XXX
1895
1927
  arg, desc = arg.split(';', 2)
1896
1928
  opt, val = arg.split(':', 2)
1897
1929
  if val
1898
- result[opt] = val.empty? ? nil : val
1930
+ setter[opt, (val unless val.empty?)]
1899
1931
  define("--#{opt}=#{result[opt] || "VAL"}", *[desc].compact)
1900
1932
  else
1901
- result[opt] = false
1933
+ setter[opt, false]
1902
1934
  define("--#{opt}", *[desc].compact)
1903
1935
  end
1904
1936
  end
1905
1937
 
1906
- parse_in_order(argv, result.method(:[]=), **keywords)
1907
- symbolize_names ? result.transform_keys(&:to_sym) : result
1938
+ parse_in_order(argv, setter, **keywords)
1939
+ result
1908
1940
  end
1909
1941
 
1910
1942
  #
@@ -1954,7 +1986,7 @@ XXX
1954
1986
  visit(:complete, typ, opt, icase, *pat) {|o, *sw| return sw}
1955
1987
  }
1956
1988
  exc = ambiguous ? AmbiguousOption : InvalidOption
1957
- raise exc.new(opt, additional: self.method(:additional_message).curry[typ])
1989
+ raise exc.new(opt, additional: proc {|o| additional_message(typ, o)})
1958
1990
  end
1959
1991
  private :complete
1960
1992
 
@@ -2019,19 +2051,27 @@ XXX
2019
2051
  def load(filename = nil, **keywords)
2020
2052
  unless filename
2021
2053
  basename = File.basename($0, '.*')
2022
- return true if load(File.expand_path(basename, '~/.options'), **keywords) rescue nil
2054
+ return true if load(File.expand_path("~/.options/#{basename}"), **keywords) rescue nil
2023
2055
  basename << ".options"
2056
+ if !(xdg = ENV['XDG_CONFIG_HOME']) or xdg.empty?
2057
+ # https://specifications.freedesktop.org/basedir-spec/latest/#variables
2058
+ #
2059
+ # If $XDG_CONFIG_HOME is either not set or empty, a default
2060
+ # equal to $HOME/.config should be used.
2061
+ xdg = ['~/.config', true]
2062
+ end
2024
2063
  return [
2025
- # XDG
2026
- ENV['XDG_CONFIG_HOME'],
2027
- '~/.config',
2064
+ xdg,
2065
+
2028
2066
  *ENV['XDG_CONFIG_DIRS']&.split(File::PATH_SEPARATOR),
2029
2067
 
2030
2068
  # Haiku
2031
- '~/config/settings',
2032
- ].any? {|dir|
2069
+ ['~/config/settings', true],
2070
+ ].any? {|dir, expand|
2033
2071
  next if !dir or dir.empty?
2034
- load(File.expand_path(basename, dir), **keywords) rescue nil
2072
+ filename = File.join(dir, basename)
2073
+ filename = File.expand_path(filename) if expand
2074
+ load(filename, **keywords) rescue nil
2035
2075
  }
2036
2076
  end
2037
2077
  begin
@@ -2237,9 +2277,10 @@ XXX
2237
2277
  argv
2238
2278
  end
2239
2279
 
2280
+ DIR = File.join(__dir__, '')
2240
2281
  def self.filter_backtrace(array)
2241
2282
  unless $DEBUG
2242
- array.delete_if(&%r"\A#{Regexp.quote(__FILE__)}:"o.method(:=~))
2283
+ array.delete_if {|bt| bt.start_with?(DIR)}
2243
2284
  end
2244
2285
  array
2245
2286
  end
metadata CHANGED
@@ -1,16 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: optparse
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nobu Nakada
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2024-11-08 00:00:00.000000000 Z
10
+ date: 2025-10-31 00:00:00.000000000 Z
12
11
  dependencies: []
13
- description: OptionParser is a class for command-line option analysis.
12
+ description: |
13
+ OptionParser is a class for command-line option analysis. It is much more
14
+ advanced, yet also easier to use, than GetoptLong, and is a more Ruby-oriented
15
+ solution.
14
16
  email:
15
17
  - nobu@ruby-lang.org
16
18
  executables: []
@@ -19,8 +21,8 @@ extra_rdoc_files: []
19
21
  files:
20
22
  - ".document"
21
23
  - ".rdoc_options"
24
+ - BSDL
22
25
  - COPYING
23
- - ChangeLog
24
26
  - README.md
25
27
  - doc/optparse/.document
26
28
  - doc/optparse/argument_converters.rdoc
@@ -101,11 +103,7 @@ licenses:
101
103
  metadata:
102
104
  homepage_uri: https://github.com/ruby/optparse
103
105
  source_code_uri: https://github.com/ruby/optparse
104
- post_install_message:
105
- rdoc_options:
106
- - "--main=README.md"
107
- - "--op=rdoc"
108
- - "--page-dir=doc"
106
+ rdoc_options: []
109
107
  require_paths:
110
108
  - lib
111
109
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -119,8 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
117
  - !ruby/object:Gem::Version
120
118
  version: '0'
121
119
  requirements: []
122
- rubygems_version: 3.5.11
123
- signing_key:
120
+ rubygems_version: 3.6.9
124
121
  specification_version: 4
125
122
  summary: OptionParser is a class for command-line option analysis.
126
123
  test_files: []
data/ChangeLog DELETED
@@ -1,9 +0,0 @@
1
- -*- coding: utf-8 -*-
2
-
3
- commit 080360ffd4578aceb9f711eef8f16ee520cd3f00
4
- Author: Hiroshi SHIBATA <hsbt@ruby-lang.org>
5
- AuthorDate: 2024-11-08 13:22:26 +0900
6
- Commit: Hiroshi SHIBATA <hsbt@ruby-lang.org>
7
- CommitDate: 2024-11-08 13:22:26 +0900
8
-
9
- Bump up v0.6.0