optparse 0.4.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 393ddfd5f19439c98112418ff879314c8570d9955e4a26a148b6d3bd94354cda
4
- data.tar.gz: 270b3538983dfd6920cae9b2fcda5d7dcb5304157ae1e323a0d56daf33a6dd60
3
+ metadata.gz: 7133ed3cb8069a5f9e30dc050bdf3f593c0c6c1cdb438662846922cb36efb6ca
4
+ data.tar.gz: 2f831a85adf7093e20bfb5894a28864475fa0fac996c1076d9746cf2466d3587
5
5
  SHA512:
6
- metadata.gz: 80e82b866fccf4b8598d3b495941629255ddfa907a8686a092d9ab5135d145d1c0a83d9d3d42ededcf1d33e21bc5ea6bfbee79f40085140e29a4ac54e6b96a1d
7
- data.tar.gz: 998881fc6568b9725d7874145111cdee53e2366050a63f0e5908395f183d080eda16bf6740e8d83f90fe60064cf85fb424c24cd70e4589b344ee9dc3b997d283
6
+ metadata.gz: 7696cd28fc5ea95a88bdb9815d67e916787ba891baf526e4c966bbf28c8893ad63c6835967f5907f7ff3b0932e8e07e058cc0028a13a5e8fb6f4433d245026b0
7
+ data.tar.gz: 71b51296c92eaeb0ec443a5d502f81fda00884e6cf8b77c8cfff8e5cbe60d5a27d6b170c3107235028136c0c6953e6ba60d08a2744ea890d96d213ba2e31b5b5
data/.document ADDED
@@ -0,0 +1,7 @@
1
+ COPYING
2
+ ChangeLog
3
+ README.md
4
+ doc/
5
+ lib/
6
+ logs/
7
+ misc/
data/.rdoc_options ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ page_dir: doc
3
+ main_page: README.md
4
+ title: Documentation for OptionParser
data/ChangeLog CHANGED
@@ -1,163 +1,9 @@
1
1
  -*- coding: utf-8 -*-
2
2
 
3
- commit acbf6e3e12de4d116f7215b64432f204d293da97
3
+ commit 080360ffd4578aceb9f711eef8f16ee520cd3f00
4
4
  Author: Hiroshi SHIBATA <hsbt@ruby-lang.org>
5
- AuthorDate: 2023-11-07 10:40:38 +0900
5
+ AuthorDate: 2024-11-08 13:22:26 +0900
6
6
  Commit: Hiroshi SHIBATA <hsbt@ruby-lang.org>
7
- CommitDate: 2023-11-07 10:40:38 +0900
7
+ CommitDate: 2024-11-08 13:22:26 +0900
8
8
 
9
- Bump up 0.4.0
10
-
11
- commit 38848ce4b31977d4a892c2a5573b304c77208283
12
- Author: Hiroshi SHIBATA <hsbt@ruby-lang.org>
13
- AuthorDate: 2023-10-05 16:17:40 +0900
14
- Commit: Hiroshi SHIBATA <hsbt@ruby-lang.org>
15
- CommitDate: 2023-10-05 16:26:53 +0900
16
-
17
- Use test-unit-ruby-core gem
18
-
19
- commit a291ef5c26445d423bf0223b0cb64340df18f0cb
20
- Author: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
21
- AuthorDate: 2023-09-11 03:20:53 +0000
22
- Commit: Hiroshi SHIBATA <hsbt@ruby-lang.org>
23
- CommitDate: 2023-09-11 14:02:17 +0900
24
-
25
- Bump actions/checkout from 3 to 4
26
-
27
- Bumps [actions/checkout](https://github.com/actions/checkout) from 3 to 4.
28
- - [Release notes](https://github.com/actions/checkout/releases)
29
- - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
30
- - [Commits](https://github.com/actions/checkout/compare/v3...v4)
31
-
32
- ---
33
- updated-dependencies:
34
- - dependency-name: actions/checkout
35
- dependency-type: direct:production
36
- update-type: version-update:semver-major
37
- ...
38
-
39
- Signed-off-by: dependabot[bot] <support@github.com>
40
-
41
- commit 2940dbb65a7df013995934a93e6906109adda766
42
- Author: BurdetteLamar <burdettelamar@yahoo.com>
43
- AuthorDate: 2023-07-30 16:35:00 +0100
44
- Commit: BurdetteLamar <burdettelamar@yahoo.com>
45
- CommitDate: 2023-07-30 16:35:00 +0100
46
-
47
- [DOC] Corrections to tutorial
48
-
49
- commit e8bee0be8f52f5a3e08d0db09a13798701670391
50
- Author: Nobuyoshi Nakada <nobu@ruby-lang.org>
51
- AuthorDate: 2023-07-30 11:24:59 +0900
52
- Commit: Nobuyoshi Nakada <nobu@ruby-lang.org>
53
- CommitDate: 2023-07-30 11:24:59 +0900
54
-
55
- [DOC] Mark up constant and method names as code
56
-
57
- commit 3cde2178d3d1bd1c7bf9c7e2af930e89e0bad6e5
58
- Author: Nobuyoshi Nakada <nobu@ruby-lang.org>
59
- AuthorDate: 2023-07-12 01:07:11 +0900
60
- Commit: Nobuyoshi Nakada <nobu@ruby-lang.org>
61
- CommitDate: 2023-07-12 01:07:37 +0900
62
-
63
- Upload according to build results
64
-
65
- commit fb91d97c109269aee4fb0f7ecc1b3826dad118d8
66
- Author: Jeremy Evans <code@jeremyevans.net>
67
- AuthorDate: 2023-04-04 13:58:59 -0700
68
- Commit: Hiroshi SHIBATA <hsbt@ruby-lang.org>
69
- CommitDate: 2023-04-05 08:43:45 +0900
70
-
71
- Document requires needed for Date/DateTime/Time/URI/Shellwords support
72
-
73
- Fixes [Bug #19566]
74
-
75
- commit 5bf4fa8a72a00d0891106af21655800db1d2ceff
76
- Author: Hiroshi SHIBATA <hsbt@ruby-lang.org>
77
- AuthorDate: 2023-03-24 13:01:25 +0900
78
- Commit: Hiroshi SHIBATA <hsbt@ruby-lang.org>
79
- CommitDate: 2023-03-24 13:38:04 +0900
80
-
81
- Update test libraries from https://github.com/ruby/ruby/commit/b4e438d8aabaf4bba2b27f374c787543fae07c58
82
-
83
- commit b67cc2407e6f9b5b4c9bc0751e21fa4969da0ceb
84
- Author: Hiroshi SHIBATA <hsbt@ruby-lang.org>
85
- AuthorDate: 2023-02-18 16:37:54 +0900
86
- Commit: Hiroshi SHIBATA <hsbt@ruby-lang.org>
87
- CommitDate: 2023-02-18 16:46:18 +0900
88
-
89
- Skip build and upload package with Windows
90
-
91
- commit fd6621a23da2e8d3d1a3ab75003dbfeb0e42e2ab
92
- Author: Hiroshi SHIBATA <hsbt@ruby-lang.org>
93
- AuthorDate: 2023-02-18 16:31:55 +0900
94
- Commit: Hiroshi SHIBATA <hsbt@ruby-lang.org>
95
- CommitDate: 2023-02-18 16:46:18 +0900
96
-
97
- Exclude JRuby from Windows
98
-
99
- commit 426726fa170ab5a1ddba4f2ef04ae6864ed2a625
100
- Author: Hiroshi SHIBATA <hsbt@ruby-lang.org>
101
- AuthorDate: 2023-02-18 16:25:39 +0900
102
- Commit: Hiroshi SHIBATA <hsbt@ruby-lang.org>
103
- CommitDate: 2023-02-18 16:46:18 +0900
104
-
105
- Try with Windows
106
-
107
- commit 45fade52f519f656f62eb601899e3e96d3f69fca
108
- Author: Hiroshi SHIBATA <hsbt@ruby-lang.org>
109
- AuthorDate: 2023-02-18 16:25:25 +0900
110
- Commit: Hiroshi SHIBATA <hsbt@ruby-lang.org>
111
- CommitDate: 2023-02-18 16:46:18 +0900
112
-
113
- Use ruby/actions/.github/workflows/ruby_versions.yml@master
114
-
115
- commit 366a6a6b569037420013e83ada92012d0c6a2f76
116
- Author: Keishi Tanaka <k-tanaka@ruby-dev.jp>
117
- AuthorDate: 2023-01-21 14:09:00 +0900
118
- Commit: GitHub <noreply@github.com>
119
- CommitDate: 2023-01-21 14:09:00 +0900
120
-
121
- Migrate set-output to $GITHUB_OUTPUT (#50)
122
-
123
- https://github.blog/changelog/2022-10-11-github-actions-deprecating-save-state-and-set-output-commands/
124
-
125
- commit 3e63d878f895a299a6ddeee5f8c615b69edf401c
126
- Author: Junichi Ito <jit@sonicgarden.jp>
127
- AuthorDate: 2022-11-29 08:07:47 +0900
128
- Commit: Nobuyoshi Nakada <nobu@ruby-lang.org>
129
- CommitDate: 2022-12-22 19:58:43 +0900
130
-
131
- Add symbolize_names to getopts
132
-
133
- commit 73661899ad17e1061c7fc0f88cc4ac4e66a3c9a9
134
- Author: Nobuyoshi Nakada <nobu@ruby-lang.org>
135
- AuthorDate: 2022-12-22 19:57:30 +0900
136
- Commit: Nobuyoshi Nakada <nobu@ruby-lang.org>
137
- CommitDate: 2022-12-22 19:57:30 +0900
138
-
139
- bump up to 0.4.0.pre.1
140
-
141
- commit 697ea7bf8e343704e735fb2d1f7787d96b843469
142
- Author: Nobuyoshi Nakada <nobu@ruby-lang.org>
143
- AuthorDate: 2022-12-22 19:55:51 +0900
144
- Commit: Nobuyoshi Nakada <nobu@ruby-lang.org>
145
- CommitDate: 2022-12-22 19:55:51 +0900
146
-
147
- Add .rdoc_options
148
-
149
- commit ab0022bd23d77e515390cbb2c165f7647400be46
150
- Author: Nobuyoshi Nakada <nobu@ruby-lang.org>
151
- AuthorDate: 2021-09-29 11:47:47 +0900
152
- Commit: Nobuyoshi Nakada <nobu@ruby-lang.org>
153
- CommitDate: 2022-12-22 19:53:20 +0900
154
-
155
- Cron test
156
-
157
- commit 33663e2c50eb8496b81750976b3480c14783470d
158
- Author: Nobuyoshi Nakada <nobu@ruby-lang.org>
159
- AuthorDate: 2022-12-22 19:48:22 +0900
160
- Commit: Nobuyoshi Nakada <nobu@ruby-lang.org>
161
- CommitDate: 2022-12-22 19:50:16 +0900
162
-
163
- Add `helper.bump`
9
+ Bump up v0.6.0
data/README.md CHANGED
@@ -50,7 +50,7 @@ p ARGV
50
50
 
51
51
  ## Development
52
52
 
53
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
53
+ After checking out the repo, run `bundle install` to install dependencies. Then, run `rake test` to run the tests.
54
54
 
55
55
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
56
56
 
@@ -0,0 +1 @@
1
+ *.rdoc
@@ -31,7 +31,7 @@ Contents:
31
31
  - {Long Names with Optional Arguments}[#label-Long+Names+with+Optional+Arguments]
32
32
  - {Long Names with Negation}[#label-Long+Names+with+Negation]
33
33
  - {Mixed Names}[#label-Mixed+Names]
34
- - {Argument Styles}[#label-Argument+Styles]
34
+ - {Argument Strings}[#label-Argument+Strings]
35
35
  - {Argument Values}[#label-Argument+Values]
36
36
  - {Explicit Argument Values}[#label-Explicit+Argument+Values]
37
37
  - {Explicit Values in Array}[#label-Explicit+Values+in+Array]
@@ -0,0 +1,9 @@
1
+ require 'optparse'
2
+ parser = OptionParser.new
3
+ parser.on('-x', '--xxx=VALUE', %w[ABC def], 'Argument abbreviations') do |value|
4
+ p ['--xxx', value]
5
+ end
6
+ parser.on('-y', '--yyy=VALUE', {"abc"=>"XYZ", def: "FOO"}, 'Argument abbreviations') do |value|
7
+ p ['--yyy', value]
8
+ end
9
+ parser.parse!
@@ -351,6 +351,29 @@ Executions:
351
351
 
352
352
  Omitting an optional argument does not raise an error.
353
353
 
354
+ ==== Argument Abbreviations
355
+
356
+ Specify an argument list as an Array or a Hash.
357
+
358
+ :include: ruby/argument_abbreviation.rb
359
+
360
+ When an argument is abbreviated, the expanded argument yielded.
361
+
362
+ Executions:
363
+
364
+ $ ruby argument_abbreviation.rb --help
365
+ Usage: argument_abbreviation [options]
366
+ Usage: argument_abbreviation [options]
367
+ -x, --xxx=VALUE Argument abbreviations
368
+ -y, --yyy=VALUE Argument abbreviations
369
+ $ ruby argument_abbreviation.rb --xxx A
370
+ ["--xxx", "ABC"]
371
+ $ ruby argument_abbreviation.rb --xxx c
372
+ argument_abbreviation.rb:9:in `<main>': invalid argument: --xxx c (OptionParser::InvalidArgument)
373
+ $ ruby argument_abbreviation.rb --yyy a --yyy d
374
+ ["--yyy", "XYZ"]
375
+ ["--yyy", "FOO"]
376
+
354
377
  === Argument Values
355
378
 
356
379
  Permissible argument values may be restricted
data/lib/optparse/ac.rb CHANGED
@@ -1,7 +1,11 @@
1
1
  # frozen_string_literal: false
2
2
  require_relative '../optparse'
3
3
 
4
+ #
5
+ # autoconf-like options.
6
+ #
4
7
  class OptionParser::AC < OptionParser
8
+ # :stopdoc:
5
9
  private
6
10
 
7
11
  def _check_ac_args(name, block)
@@ -14,6 +18,7 @@ class OptionParser::AC < OptionParser
14
18
  end
15
19
 
16
20
  ARG_CONV = proc {|val| val.nil? ? true : val}
21
+ private_constant :ARG_CONV
17
22
 
18
23
  def _ac_arg_enable(prefix, name, help_string, block)
19
24
  _check_ac_args(name, block)
@@ -29,16 +34,27 @@ class OptionParser::AC < OptionParser
29
34
  enable
30
35
  end
31
36
 
37
+ # :startdoc:
38
+
32
39
  public
33
40
 
41
+ # Define <tt>--enable</tt> / <tt>--disable</tt> style option
42
+ #
43
+ # Appears as <tt>--enable-<i>name</i></tt> in help message.
34
44
  def ac_arg_enable(name, help_string, &block)
35
45
  _ac_arg_enable("enable", name, help_string, block)
36
46
  end
37
47
 
48
+ # Define <tt>--enable</tt> / <tt>--disable</tt> style option
49
+ #
50
+ # Appears as <tt>--disable-<i>name</i></tt> in help message.
38
51
  def ac_arg_disable(name, help_string, &block)
39
52
  _ac_arg_enable("disable", name, help_string, block)
40
53
  end
41
54
 
55
+ # Define <tt>--with</tt> / <tt>--without</tt> style option
56
+ #
57
+ # Appears as <tt>--with-<i>name</i></tt> in help message.
42
58
  def ac_arg_with(name, help_string, &block)
43
59
  _check_ac_args(name, block)
44
60
 
@@ -7,12 +7,17 @@ class OptionParser
7
7
  #
8
8
  # :include: ../../doc/optparse/creates_option.rdoc
9
9
  #
10
- def define_by_keywords(options, meth, **opts)
11
- meth.parameters.each do |type, name|
10
+ # Defines options which set in to _options_ for keyword parameters
11
+ # of _method_.
12
+ #
13
+ # Parameters for each keywords are given as elements of _params_.
14
+ #
15
+ def define_by_keywords(options, method, **params)
16
+ method.parameters.each do |type, name|
12
17
  case type
13
18
  when :key, :keyreq
14
19
  op, cl = *(type == :key ? %w"[ ]" : ["", ""])
15
- define("--#{name}=#{op}#{name.upcase}#{cl}", *opts[name]) do |o|
20
+ define("--#{name}=#{op}#{name.upcase}#{cl}", *params[name]) do |o|
16
21
  options[name] = o
17
22
  end
18
23
  end
@@ -2,6 +2,11 @@
2
2
  # OptionParser internal utility
3
3
 
4
4
  class << OptionParser
5
+ #
6
+ # Shows version string in packages if Version is defined.
7
+ #
8
+ # +pkgs+:: package list
9
+ #
5
10
  def show_version(*pkgs)
6
11
  progname = ARGV.options.program_name
7
12
  result = false
@@ -47,6 +52,8 @@ class << OptionParser
47
52
  result
48
53
  end
49
54
 
55
+ # :stopdoc:
56
+
50
57
  def each_const(path, base = ::Object)
51
58
  path.split(/::|\//).inject(base) do |klass, name|
52
59
  raise NameError, path unless Module === klass
@@ -68,4 +75,6 @@ class << OptionParser
68
75
  end
69
76
  end
70
77
  end
78
+
79
+ # :startdoc:
71
80
  end
data/lib/optparse.rb CHANGED
@@ -8,7 +8,6 @@
8
8
  # See OptionParser for documentation.
9
9
  #
10
10
 
11
-
12
11
  #--
13
12
  # == Developer Documentation (not for RDoc output)
14
13
  #
@@ -425,7 +424,8 @@
425
424
  # If you have any questions, file a ticket at http://bugs.ruby-lang.org.
426
425
  #
427
426
  class OptionParser
428
- OptionParser::Version = "0.4.0"
427
+ # The version string
428
+ OptionParser::Version = "0.6.0"
429
429
 
430
430
  # :stopdoc:
431
431
  NoArgument = [NO_ARGUMENT = :NONE, nil].freeze
@@ -438,6 +438,8 @@ class OptionParser
438
438
  # and resolved against a list of acceptable values.
439
439
  #
440
440
  module Completion
441
+ # :nodoc:
442
+
441
443
  def self.regexp(key, icase)
442
444
  Regexp.new('\A' + Regexp.quote(key).gsub(/\w+\b/, '\&\w*'), icase)
443
445
  end
@@ -459,7 +461,7 @@ class OptionParser
459
461
  candidates
460
462
  end
461
463
 
462
- def candidate(key, icase = false, pat = nil)
464
+ def candidate(key, icase = false, pat = nil, &_)
463
465
  Completion.candidate(key, icase, pat, &method(:each))
464
466
  end
465
467
 
@@ -510,6 +512,8 @@ class OptionParser
510
512
  # RequiredArgument, etc.
511
513
  #
512
514
  class Switch
515
+ # :nodoc:
516
+
513
517
  attr_reader :pattern, :conv, :short, :long, :arg, :desc, :block
514
518
 
515
519
  #
@@ -697,6 +701,11 @@ class OptionParser
697
701
  q.object_group(self) {pretty_print_contents(q)}
698
702
  end
699
703
 
704
+ def omitted_argument(val) # :nodoc:
705
+ val.pop if val.size == 3 and val.last.nil?
706
+ val
707
+ end
708
+
700
709
  #
701
710
  # Switch that takes no arguments.
702
711
  #
@@ -710,10 +719,10 @@ class OptionParser
710
719
  conv_arg(arg)
711
720
  end
712
721
 
713
- def self.incompatible_argument_styles(*)
722
+ def self.incompatible_argument_styles(*) # :nodoc:
714
723
  end
715
724
 
716
- def self.pattern
725
+ def self.pattern # :nodoc:
717
726
  Object
718
727
  end
719
728
 
@@ -730,7 +739,7 @@ class OptionParser
730
739
  #
731
740
  # Raises an exception if argument is not present.
732
741
  #
733
- def parse(arg, argv)
742
+ def parse(arg, argv, &_)
734
743
  unless arg
735
744
  raise MissingArgument if argv.empty?
736
745
  arg = argv.shift
@@ -755,7 +764,7 @@ class OptionParser
755
764
  if arg
756
765
  conv_arg(*parse_arg(arg, &error))
757
766
  else
758
- conv_arg(arg)
767
+ omitted_argument conv_arg(arg)
759
768
  end
760
769
  end
761
770
 
@@ -774,13 +783,14 @@ class OptionParser
774
783
  #
775
784
  def parse(arg, argv, &error)
776
785
  if !(val = arg) and (argv.empty? or /\A-./ =~ (val = argv[0]))
777
- return nil, block, nil
786
+ return nil, block
778
787
  end
779
788
  opt = (val = parse_arg(val, &error))[1]
780
789
  val = conv_arg(*val)
781
790
  if opt and !arg
782
791
  argv.shift
783
792
  else
793
+ omitted_argument val
784
794
  val[0] = nil
785
795
  end
786
796
  val
@@ -798,6 +808,8 @@ class OptionParser
798
808
  # matching pattern and converter pair. Also provides summary feature.
799
809
  #
800
810
  class List
811
+ # :nodoc:
812
+
801
813
  # Map from acceptable argument types to pattern and converter pairs.
802
814
  attr_reader :atype
803
815
 
@@ -837,7 +849,7 @@ class OptionParser
837
849
  def accept(t, pat = /.*/m, &block)
838
850
  if pat
839
851
  pat.respond_to?(:match) or
840
- raise TypeError, "has no `match'", ParseError.filter_backtrace(caller(2))
852
+ raise TypeError, "has no 'match'", ParseError.filter_backtrace(caller(2))
841
853
  else
842
854
  pat = t if t.respond_to?(:match)
843
855
  end
@@ -1033,11 +1045,31 @@ XXX
1033
1045
  to << "#compdef #{name}\n"
1034
1046
  to << COMPSYS_HEADER
1035
1047
  visit(:compsys, {}, {}) {|o, d|
1036
- to << %Q[ "#{o}[#{d.gsub(/[\"\[\]]/, '\\\\\&')}]" \\\n]
1048
+ to << %Q[ "#{o}[#{d.gsub(/[\\\"\[\]]/, '\\\\\&')}]" \\\n]
1037
1049
  }
1038
1050
  to << " '*:file:_files' && return 0\n"
1039
1051
  end
1040
1052
 
1053
+ def help_exit
1054
+ if STDOUT.tty? && (pager = ENV.values_at(*%w[RUBY_PAGER PAGER]).find {|e| e && !e.empty?})
1055
+ less = ENV["LESS"]
1056
+ args = [{"LESS" => "#{!less || less.empty? ? '-' : less}Fe"}, pager, "w"]
1057
+ print = proc do |f|
1058
+ f.puts help
1059
+ rescue Errno::EPIPE
1060
+ # pager terminated
1061
+ end
1062
+ if Process.respond_to?(:fork) and false
1063
+ IO.popen("-") {|f| f ? Process.exec(*args, in: f) : print.call(STDOUT)}
1064
+ # unreachable
1065
+ end
1066
+ IO.popen(*args, &print)
1067
+ else
1068
+ puts help
1069
+ end
1070
+ exit
1071
+ end
1072
+
1041
1073
  #
1042
1074
  # Default options for ARGV, which never appear in option summary.
1043
1075
  #
@@ -1049,8 +1081,7 @@ XXX
1049
1081
  #
1050
1082
  Officious['help'] = proc do |parser|
1051
1083
  Switch::NoArgument.new do |arg|
1052
- puts parser.help
1053
- exit
1084
+ parser.help_exit
1054
1085
  end
1055
1086
  end
1056
1087
 
@@ -1084,7 +1115,7 @@ XXX
1084
1115
  Switch::OptionalArgument.new do |pkg|
1085
1116
  if pkg
1086
1117
  begin
1087
- require 'optparse/version'
1118
+ require_relative 'optparse/version'
1088
1119
  rescue LoadError
1089
1120
  else
1090
1121
  show_version(*pkg.split(/,/)) or
@@ -1129,6 +1160,10 @@ XXX
1129
1160
  default.to_i + 1
1130
1161
  end
1131
1162
  end
1163
+
1164
+ #
1165
+ # See self.inc
1166
+ #
1132
1167
  def inc(*args)
1133
1168
  self.class.inc(*args)
1134
1169
  end
@@ -1167,11 +1202,19 @@ XXX
1167
1202
  def terminate(arg = nil)
1168
1203
  self.class.terminate(arg)
1169
1204
  end
1205
+ #
1206
+ # See #terminate.
1207
+ #
1170
1208
  def self.terminate(arg = nil)
1171
1209
  throw :terminate, arg
1172
1210
  end
1173
1211
 
1174
1212
  @stack = [DefaultList]
1213
+ #
1214
+ # Returns the global top option list.
1215
+ #
1216
+ # Do not use directly.
1217
+ #
1175
1218
  def self.top() DefaultList end
1176
1219
 
1177
1220
  #
@@ -1192,9 +1235,9 @@ XXX
1192
1235
  #
1193
1236
  # Directs to reject specified class argument.
1194
1237
  #
1195
- # +t+:: Argument class specifier, any object including Class.
1238
+ # +type+:: Argument class specifier, any object including Class.
1196
1239
  #
1197
- # reject(t)
1240
+ # reject(type)
1198
1241
  #
1199
1242
  def reject(*args, &blk) top.reject(*args, &blk) end
1200
1243
  #
@@ -1284,10 +1327,24 @@ XXX
1284
1327
  end
1285
1328
  end
1286
1329
 
1330
+ #
1331
+ # Shows warning message with the program name
1332
+ #
1333
+ # +mesg+:: Message, defaulted to +$!+.
1334
+ #
1335
+ # See Kernel#warn.
1336
+ #
1287
1337
  def warn(mesg = $!)
1288
1338
  super("#{program_name}: #{mesg}")
1289
1339
  end
1290
1340
 
1341
+ #
1342
+ # Shows message with the program name then aborts.
1343
+ #
1344
+ # +mesg+:: Message, defaulted to +$!+.
1345
+ #
1346
+ # See Kernel#abort.
1347
+ #
1291
1348
  def abort(mesg = $!)
1292
1349
  super("#{program_name}: #{mesg}")
1293
1350
  end
@@ -1309,6 +1366,9 @@ XXX
1309
1366
  #
1310
1367
  # Pushes a new List.
1311
1368
  #
1369
+ # If a block is given, yields +self+ and returns the result of the
1370
+ # block, otherwise returns +self+.
1371
+ #
1312
1372
  def new
1313
1373
  @stack.push(List.new)
1314
1374
  if block_given?
@@ -1532,6 +1592,12 @@ XXX
1532
1592
  nolong
1533
1593
  end
1534
1594
 
1595
+ # ----
1596
+ # Option definition phase methods
1597
+ #
1598
+ # These methods are used to define options, or to construct an
1599
+ # OptionParser instance in other words.
1600
+
1535
1601
  # :call-seq:
1536
1602
  # define(*params, &block)
1537
1603
  #
@@ -1607,6 +1673,13 @@ XXX
1607
1673
  top.append(string, nil, nil)
1608
1674
  end
1609
1675
 
1676
+ # ----
1677
+ # Arguments parse phase methods
1678
+ #
1679
+ # These methods parse +argv+, convert, and store the results by
1680
+ # calling handlers. As these methods do not modify +self+, +self+
1681
+ # can be frozen.
1682
+
1610
1683
  #
1611
1684
  # Parses command line arguments +argv+ in order. When a block is given,
1612
1685
  # each non-option argument is yielded. When optional +into+ keyword
@@ -1616,21 +1689,21 @@ XXX
1616
1689
  #
1617
1690
  # Returns the rest of +argv+ left unparsed.
1618
1691
  #
1619
- def order(*argv, into: nil, &nonopt)
1692
+ def order(*argv, **keywords, &nonopt)
1620
1693
  argv = argv[0].dup if argv.size == 1 and Array === argv[0]
1621
- order!(argv, into: into, &nonopt)
1694
+ order!(argv, **keywords, &nonopt)
1622
1695
  end
1623
1696
 
1624
1697
  #
1625
1698
  # Same as #order, but removes switches destructively.
1626
1699
  # Non-option arguments remain in +argv+.
1627
1700
  #
1628
- def order!(argv = default_argv, into: nil, &nonopt)
1701
+ def order!(argv = default_argv, into: nil, **keywords, &nonopt)
1629
1702
  setter = ->(name, val) {into[name.to_sym] = val} if into
1630
- parse_in_order(argv, setter, &nonopt)
1703
+ parse_in_order(argv, setter, **keywords, &nonopt)
1631
1704
  end
1632
1705
 
1633
- def parse_in_order(argv = default_argv, setter = nil, &nonopt) # :nodoc:
1706
+ def parse_in_order(argv = default_argv, setter = nil, exact: require_exact, **, &nonopt) # :nodoc:
1634
1707
  opt, arg, val, rest = nil
1635
1708
  nonopt ||= proc {|a| throw :terminate, a}
1636
1709
  argv.unshift(arg) if arg = catch(:terminate) {
@@ -1641,19 +1714,24 @@ XXX
1641
1714
  opt, rest = $1, $2
1642
1715
  opt.tr!('_', '-')
1643
1716
  begin
1644
- sw, = complete(:long, opt, true)
1645
- if require_exact && !sw.long.include?(arg)
1646
- throw :terminate, arg unless raise_unknown
1647
- raise InvalidOption, arg
1717
+ if exact
1718
+ sw, = search(:long, opt)
1719
+ else
1720
+ sw, = complete(:long, opt, true)
1648
1721
  end
1649
1722
  rescue ParseError
1650
1723
  throw :terminate, arg unless raise_unknown
1651
1724
  raise $!.set_option(arg, true)
1725
+ else
1726
+ unless sw
1727
+ throw :terminate, arg unless raise_unknown
1728
+ raise InvalidOption, arg
1729
+ end
1652
1730
  end
1653
1731
  begin
1654
1732
  opt, cb, val = sw.parse(rest, argv) {|*exc| raise(*exc)}
1655
- val = cb.call(val) if cb
1656
- setter.call(sw.switch_name, val) if setter
1733
+ val = callback!(cb, 1, val) if cb
1734
+ callback!(setter, 2, sw.switch_name, val) if setter
1657
1735
  rescue ParseError
1658
1736
  raise $!.set_option(arg, rest)
1659
1737
  end
@@ -1671,7 +1749,7 @@ XXX
1671
1749
  val = arg.delete_prefix('-')
1672
1750
  has_arg = true
1673
1751
  rescue InvalidOption
1674
- raise if require_exact
1752
+ raise if exact
1675
1753
  # if no short options match, try completion with long
1676
1754
  # options.
1677
1755
  sw, = complete(:long, opt)
@@ -1691,8 +1769,8 @@ XXX
1691
1769
  end
1692
1770
  begin
1693
1771
  argv.unshift(opt) if opt and (!rest or (opt = opt.sub(/\A-*/, '-')) != '-')
1694
- val = cb.call(val) if cb
1695
- setter.call(sw.switch_name, val) if setter
1772
+ val = callback!(cb, 1, val) if cb
1773
+ callback!(setter, 2, sw.switch_name, val) if setter
1696
1774
  rescue ParseError
1697
1775
  raise $!.set_option(arg, arg.length > 2)
1698
1776
  end
@@ -1718,6 +1796,19 @@ XXX
1718
1796
  end
1719
1797
  private :parse_in_order
1720
1798
 
1799
+ # Calls callback with _val_.
1800
+ def callback!(cb, max_arity, *args) # :nodoc:
1801
+ args.compact!
1802
+
1803
+ if (size = args.size) < max_arity and cb.to_proc.lambda?
1804
+ (arity = cb.arity) < 0 and arity = (1-arity)
1805
+ arity = max_arity if arity > max_arity
1806
+ args[arity - 1] = nil if arity > size
1807
+ end
1808
+ cb.call(*args)
1809
+ end
1810
+ private :callback!
1811
+
1721
1812
  #
1722
1813
  # Parses command line arguments +argv+ in permutation mode and returns
1723
1814
  # list of non-option arguments. When optional +into+ keyword
@@ -1725,18 +1816,18 @@ XXX
1725
1816
  # <code>[]=</code> method (so it can be Hash, or OpenStruct, or other
1726
1817
  # similar object).
1727
1818
  #
1728
- def permute(*argv, into: nil)
1819
+ def permute(*argv, **keywords)
1729
1820
  argv = argv[0].dup if argv.size == 1 and Array === argv[0]
1730
- permute!(argv, into: into)
1821
+ permute!(argv, **keywords)
1731
1822
  end
1732
1823
 
1733
1824
  #
1734
1825
  # Same as #permute, but removes switches destructively.
1735
1826
  # Non-option arguments remain in +argv+.
1736
1827
  #
1737
- def permute!(argv = default_argv, into: nil)
1828
+ def permute!(argv = default_argv, **keywords)
1738
1829
  nonopts = []
1739
- order!(argv, into: into, &nonopts.method(:<<))
1830
+ order!(argv, **keywords, &nonopts.method(:<<))
1740
1831
  argv[0, 0] = nonopts
1741
1832
  argv
1742
1833
  end
@@ -1748,20 +1839,20 @@ XXX
1748
1839
  # values are stored there via <code>[]=</code> method (so it can be Hash,
1749
1840
  # or OpenStruct, or other similar object).
1750
1841
  #
1751
- def parse(*argv, into: nil)
1842
+ def parse(*argv, **keywords)
1752
1843
  argv = argv[0].dup if argv.size == 1 and Array === argv[0]
1753
- parse!(argv, into: into)
1844
+ parse!(argv, **keywords)
1754
1845
  end
1755
1846
 
1756
1847
  #
1757
1848
  # Same as #parse, but removes switches destructively.
1758
1849
  # Non-option arguments remain in +argv+.
1759
1850
  #
1760
- def parse!(argv = default_argv, into: nil)
1851
+ def parse!(argv = default_argv, **keywords)
1761
1852
  if ENV.include?('POSIXLY_CORRECT')
1762
- order!(argv, into: into)
1853
+ order!(argv, **keywords)
1763
1854
  else
1764
- permute!(argv, into: into)
1855
+ permute!(argv, **keywords)
1765
1856
  end
1766
1857
  end
1767
1858
 
@@ -1784,7 +1875,7 @@ XXX
1784
1875
  # # params[:bar] = "x" # --bar x
1785
1876
  # # params[:zot] = "z" # --zot Z
1786
1877
  #
1787
- def getopts(*args, symbolize_names: false)
1878
+ def getopts(*args, symbolize_names: false, **keywords)
1788
1879
  argv = Array === args.first ? args.shift : default_argv
1789
1880
  single_options, *long_options = *args
1790
1881
 
@@ -1812,7 +1903,7 @@ XXX
1812
1903
  end
1813
1904
  end
1814
1905
 
1815
- parse_in_order(argv, result.method(:[]=))
1906
+ parse_in_order(argv, result.method(:[]=), **keywords)
1816
1907
  symbolize_names ? result.transform_keys(&:to_sym) : result
1817
1908
  end
1818
1909
 
@@ -1881,6 +1972,9 @@ XXX
1881
1972
  DidYouMean.formatter.message_for(all_candidates & checker.correct(opt))
1882
1973
  end
1883
1974
 
1975
+ #
1976
+ # Return candidates for +word+.
1977
+ #
1884
1978
  def candidate(word)
1885
1979
  list = []
1886
1980
  case word
@@ -1922,10 +2016,10 @@ XXX
1922
2016
  # The optional +into+ keyword argument works exactly like that accepted in
1923
2017
  # method #parse.
1924
2018
  #
1925
- def load(filename = nil, into: nil)
2019
+ def load(filename = nil, **keywords)
1926
2020
  unless filename
1927
2021
  basename = File.basename($0, '.*')
1928
- return true if load(File.expand_path(basename, '~/.options'), into: into) rescue nil
2022
+ return true if load(File.expand_path(basename, '~/.options'), **keywords) rescue nil
1929
2023
  basename << ".options"
1930
2024
  return [
1931
2025
  # XDG
@@ -1937,11 +2031,11 @@ XXX
1937
2031
  '~/config/settings',
1938
2032
  ].any? {|dir|
1939
2033
  next if !dir or dir.empty?
1940
- load(File.expand_path(basename, dir), into: into) rescue nil
2034
+ load(File.expand_path(basename, dir), **keywords) rescue nil
1941
2035
  }
1942
2036
  end
1943
2037
  begin
1944
- parse(*File.readlines(filename, chomp: true), into: into)
2038
+ parse(*File.readlines(filename, chomp: true), **keywords)
1945
2039
  true
1946
2040
  rescue Errno::ENOENT, Errno::ENOTDIR
1947
2041
  false
@@ -1954,10 +2048,10 @@ XXX
1954
2048
  #
1955
2049
  # +env+ defaults to the basename of the program.
1956
2050
  #
1957
- def environment(env = File.basename($0, '.*'))
2051
+ def environment(env = File.basename($0, '.*'), **keywords)
1958
2052
  env = ENV[env] || ENV[env.upcase] or return
1959
2053
  require 'shellwords'
1960
- parse(*Shellwords.shellwords(env))
2054
+ parse(*Shellwords.shellwords(env), **keywords)
1961
2055
  end
1962
2056
 
1963
2057
  #
@@ -2123,6 +2217,7 @@ XXX
2123
2217
  # Reason which caused the error.
2124
2218
  Reason = 'parse error'
2125
2219
 
2220
+ # :nodoc:
2126
2221
  def initialize(*args, additional: nil)
2127
2222
  @additional = additional
2128
2223
  @arg0, = args
@@ -2273,19 +2368,19 @@ XXX
2273
2368
  # Parses +self+ destructively in order and returns +self+ containing the
2274
2369
  # rest arguments left unparsed.
2275
2370
  #
2276
- def order!(&blk) options.order!(self, &blk) end
2371
+ def order!(**keywords, &blk) options.order!(self, **keywords, &blk) end
2277
2372
 
2278
2373
  #
2279
2374
  # Parses +self+ destructively in permutation mode and returns +self+
2280
2375
  # containing the rest arguments left unparsed.
2281
2376
  #
2282
- def permute!() options.permute!(self) end
2377
+ def permute!(**keywords) options.permute!(self, **keywords) end
2283
2378
 
2284
2379
  #
2285
2380
  # Parses +self+ destructively and returns +self+ containing the
2286
2381
  # rest arguments left unparsed.
2287
2382
  #
2288
- def parse!() options.parse!(self) end
2383
+ def parse!(**keywords) options.parse!(self, **keywords) end
2289
2384
 
2290
2385
  #
2291
2386
  # Substitution of getopts is possible as follows. Also see
@@ -2298,8 +2393,8 @@ XXX
2298
2393
  # rescue OptionParser::ParseError
2299
2394
  # end
2300
2395
  #
2301
- def getopts(*args, symbolize_names: false)
2302
- options.getopts(self, *args, symbolize_names: symbolize_names)
2396
+ def getopts(*args, symbolize_names: false, **keywords)
2397
+ options.getopts(self, *args, symbolize_names: symbolize_names, **keywords)
2303
2398
  end
2304
2399
 
2305
2400
  #
@@ -2309,7 +2404,8 @@ XXX
2309
2404
  super
2310
2405
  obj.instance_eval {@optparse = nil}
2311
2406
  end
2312
- def initialize(*args)
2407
+
2408
+ def initialize(*args) # :nodoc:
2313
2409
  super
2314
2410
  @optparse = nil
2315
2411
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: optparse
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nobu Nakada
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-11-07 00:00:00.000000000 Z
11
+ date: 2024-11-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: OptionParser is a class for command-line option analysis.
14
14
  email:
@@ -17,12 +17,16 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - ".document"
21
+ - ".rdoc_options"
20
22
  - COPYING
21
23
  - ChangeLog
22
24
  - README.md
25
+ - doc/optparse/.document
23
26
  - doc/optparse/argument_converters.rdoc
24
27
  - doc/optparse/creates_option.rdoc
25
28
  - doc/optparse/option_params.rdoc
29
+ - doc/optparse/ruby/argument_abbreviation.rb
26
30
  - doc/optparse/ruby/argument_keywords.rb
27
31
  - doc/optparse/ruby/argument_strings.rb
28
32
  - doc/optparse/ruby/argv.rb
@@ -97,7 +101,7 @@ licenses:
97
101
  metadata:
98
102
  homepage_uri: https://github.com/ruby/optparse
99
103
  source_code_uri: https://github.com/ruby/optparse
100
- post_install_message:
104
+ post_install_message:
101
105
  rdoc_options:
102
106
  - "--main=README.md"
103
107
  - "--op=rdoc"
@@ -115,8 +119,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
119
  - !ruby/object:Gem::Version
116
120
  version: '0'
117
121
  requirements: []
118
- rubygems_version: 3.5.0.dev
119
- signing_key:
122
+ rubygems_version: 3.5.11
123
+ signing_key:
120
124
  specification_version: 4
121
125
  summary: OptionParser is a class for command-line option analysis.
122
126
  test_files: []