getoptlong 0.1.0 → 0.2.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: 97e8f8ce5f0f8b6e7e95b96399f60b00e3d2bc0e806d820db2c990abc9349fca
4
- data.tar.gz: 1b376994a15f54503d342eb6286ba32b6117b409121fe267f0b5ac28da9f6969
3
+ metadata.gz: 4b270eb9d604cd1ffd4d5f0470b369d9c336c950d7feb0d2a07b4552048f0c85
4
+ data.tar.gz: 2443fc1bdf21695a9fe0c444d239064930eb3fbbe7b36638f5ed30879a22bc76
5
5
  SHA512:
6
- metadata.gz: bdf3632da65a872ce28b4bbbb511b353d31f9c50c47fb31a07102ffc782fcb8b6ac3068abb149f043bd4331b371f2f0140d9c3a24be0c427a8b365dfe6d2a3d7
7
- data.tar.gz: 9e83ccaf5dd67baf196394f0359e3f28f3f0017c67996fc35726da3e012b8e0e418a1c26c13da6658070ce997dbc6f253100c19b9603cd6d5357ce6193af2a34
6
+ metadata.gz: 3fdf473be5224dbd4151bd035f77862b77834ded4059a3ac4f4088f16ddbf8c84c52d90829dbe26a1da67a8e227489f362d3c190acd4f405baa97d142dff5524
7
+ data.tar.gz: ee73c1412d372f84e1de744d17a4aa3a2eb223fa79f39efccafd4e1f918c82632343c3fd440fa7428bf13e90fdb1b617737e6d4b93486af6389a7417f95b6496
@@ -0,0 +1,6 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: 'github-actions'
4
+ directory: '/'
5
+ schedule:
6
+ interval: 'weekly'
@@ -0,0 +1,31 @@
1
+ name: test
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ ruby-versions:
7
+ runs-on: ubuntu-latest
8
+ outputs:
9
+ versions: ${{ steps.versions.outputs.value }}
10
+ steps:
11
+ - id: versions
12
+ run: |
13
+ versions=$(curl -s 'https://cache.ruby-lang.org/pub/misc/ci_versions/all.json' | jq -c '. + ["2.6"]')
14
+ echo "::set-output name=value::${versions}"
15
+ test:
16
+ needs: ruby-versions
17
+ name: build (${{ matrix.ruby }} / ${{ matrix.os }})
18
+ strategy:
19
+ matrix:
20
+ ruby: ${{ fromJson(needs.ruby-versions.outputs.versions) }}
21
+ os: [ ubuntu-latest, macos-latest ]
22
+ runs-on: ${{ matrix.os }}
23
+ steps:
24
+ - uses: actions/checkout@v3
25
+ - name: Set up Ruby
26
+ uses: ruby/setup-ruby@v1
27
+ with:
28
+ ruby-version: ${{ matrix.ruby }}
29
+ bundler-cache: true
30
+ - name: Run test
31
+ run: bundle exec rake test
data/Gemfile CHANGED
@@ -1,8 +1,4 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- gemspec
4
-
5
- group :development do
6
- gem "bundler"
7
- gem "rake"
8
- end
3
+ gem "rake"
4
+ gem "test-unit"
data/Rakefile CHANGED
@@ -1,3 +1,10 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/test_*.rb']
8
+ end
2
9
 
3
10
  task :default => :test
data/getoptlong.gemspec CHANGED
@@ -1,17 +1,22 @@
1
- lib = File.expand_path("lib", __dir__)
2
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
- require "getoptlong/version"
1
+ # frozen_string_literal: true
2
+
3
+ name = File.basename(__FILE__, ".gemspec")
4
+ version = ["lib", Array.new(name.count("-")+1, "..").join("/")].find do |dir|
5
+ break File.foreach(File.join(__dir__, dir, "#{name.tr('-', '/')}.rb")) do |line|
6
+ /^\s*VERSION\s*=\s*"(.*)"/ =~ line and break $1
7
+ end rescue nil
8
+ end
4
9
 
5
10
  Gem::Specification.new do |spec|
6
- spec.name = "getoptlong"
7
- spec.version = GetoptLong::VERSION
11
+ spec.name = name
12
+ spec.version = version
8
13
  spec.authors = ["Yukihiro Matsumoto"]
9
14
  spec.email = ["matz@ruby-lang.org"]
10
15
 
11
16
  spec.summary = %q{GetoptLong for Ruby}
12
17
  spec.description = spec.summary
13
18
  spec.homepage = "https://github.com/ruby/getoptlong"
14
- spec.license = "BSD-2-Clause"
19
+ spec.licenses = ["Ruby", "BSD-2-Clause"]
15
20
 
16
21
  spec.metadata["homepage_uri"] = spec.homepage
17
22
  spec.metadata["source_code_uri"] = spec.homepage
@@ -19,9 +24,7 @@ Gem::Specification.new do |spec|
19
24
  # Specify which files should be added to the gem when it is released.
20
25
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
26
  spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ `git ls-files -z 2>/dev/null`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
28
  end
24
- spec.bindir = "exe"
25
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
29
  spec.require_paths = ["lib"]
27
30
  end
data/lib/getoptlong.rb CHANGED
@@ -6,85 +6,370 @@
6
6
  #
7
7
  # You may redistribute and/or modify this library under the same license
8
8
  # terms as Ruby.
9
+
10
+ # \Class \GetoptLong provides parsing both for options
11
+ # and for regular arguments.
9
12
  #
10
- # See GetoptLong for documentation.
13
+ # Using \GetoptLong, you can define options for your program.
14
+ # The program can then capture and respond to whatever options
15
+ # are included in the command that executes the program.
11
16
  #
12
- # Additional documents and the latest version of `getoptlong.rb' can be
13
- # found at http://www.sra.co.jp/people/m-kasahr/ruby/getoptlong/
14
-
15
- # The GetoptLong class allows you to parse command line options similarly to
16
- # the GNU getopt_long() C library call. Note, however, that GetoptLong is a
17
- # pure Ruby implementation.
17
+ # A simple example: file <tt>simple.rb</tt>:
18
+ #
19
+ # :include: ../sample/getoptlong/simple.rb
20
+ #
21
+ # If you are somewhat familiar with options,
22
+ # you may want to skip to this
23
+ # {full example}[#class-GetoptLong-label-Full+Example].
24
+ #
25
+ # == Options
26
+ #
27
+ # A \GetoptLong option has:
28
+ #
29
+ # - A string <em>option name</em>.
30
+ # - Zero or more string <em>aliases</em> for the name.
31
+ # - An <em>option type</em>.
32
+ #
33
+ # Options may be defined by calling singleton method GetoptLong.new,
34
+ # which returns a new \GetoptLong object.
35
+ # Options may then be processed by calling other methods
36
+ # such as GetoptLong#each.
37
+ #
38
+ # === Option Name and Aliases
39
+ #
40
+ # In the array that defines an option,
41
+ # the first element is the string option name.
42
+ # Often the name takes the 'long' form, beginning with two hyphens.
43
+ #
44
+ # The option name may have any number of aliases,
45
+ # which are defined by additional string elements.
46
+ #
47
+ # The name and each alias must be of one of two forms:
48
+ #
49
+ # - Two hyphens, followed by one or more letters.
50
+ # - One hyphen, followed by a single letter.
51
+ #
52
+ # File <tt>aliases.rb</tt>:
53
+ #
54
+ # :include: ../sample/getoptlong/aliases.rb
55
+ #
56
+ # An option may be cited by its name,
57
+ # or by any of its aliases;
58
+ # the parsed option always reports the name, not an alias:
59
+ #
60
+ # $ ruby aliases.rb -a -p --xxx --aaa -x
61
+ #
62
+ # Output:
63
+ #
64
+ # ["--xxx", ""]
65
+ # ["--xxx", ""]
66
+ # ["--xxx", ""]
67
+ # ["--xxx", ""]
68
+ # ["--xxx", ""]
69
+ #
70
+ #
71
+ # An option may also be cited by an abbreviation of its name or any alias,
72
+ # as long as that abbreviation is unique among the options.
73
+ #
74
+ # File <tt>abbrev.rb</tt>:
75
+ #
76
+ # :include: ../sample/getoptlong/abbrev.rb
77
+ #
78
+ # Command line:
79
+ #
80
+ # $ ruby abbrev.rb --xxx --xx --xyz --xy
81
+ #
82
+ # Output:
83
+ #
84
+ # ["--xxx", ""]
85
+ # ["--xxx", ""]
86
+ # ["--xyz", ""]
87
+ # ["--xyz", ""]
88
+ #
89
+ # This command line raises GetoptLong::AmbiguousOption:
90
+ #
91
+ # $ ruby abbrev.rb --x
92
+ #
93
+ # === Repetition
94
+ #
95
+ # An option may be cited more than once:
96
+ #
97
+ # $ ruby abbrev.rb --xxx --xyz --xxx --xyz
98
+ #
99
+ # Output:
100
+ #
101
+ # ["--xxx", ""]
102
+ # ["--xyz", ""]
103
+ # ["--xxx", ""]
104
+ # ["--xyz", ""]
105
+ #
106
+ # === Treating Remaining Options as Arguments
107
+ #
108
+ # A option-like token that appears
109
+ # anywhere after the token <tt>--</tt> is treated as an ordinary argument,
110
+ # and is not processed as an option:
111
+ #
112
+ # $ ruby abbrev.rb --xxx --xyz -- --xxx --xyz
113
+ #
114
+ # Output:
115
+ #
116
+ # ["--xxx", ""]
117
+ # ["--xyz", ""]
118
+ #
119
+ # === Option Types
120
+ #
121
+ # Each option definition includes an option type,
122
+ # which controls whether the option takes an argument.
123
+ #
124
+ # File <tt>types.rb</tt>:
125
+ #
126
+ # :include: ../sample/getoptlong/types.rb
127
+ #
128
+ # Note that an option type has to do with the <em>option argument</em>
129
+ # (whether it is required, optional, or forbidden),
130
+ # not with whether the option itself is required.
131
+ #
132
+ # ==== Option with Required Argument
133
+ #
134
+ # An option of type <tt>GetoptLong::REQUIRED_ARGUMENT</tt>
135
+ # must be followed by an argument, which is associated with that option:
136
+ #
137
+ # $ ruby types.rb --xxx foo
138
+ #
139
+ # Output:
140
+ #
141
+ # ["--xxx", "foo"]
142
+ #
143
+ # If the option is not last, its argument is whatever follows it
144
+ # (even if the argument looks like another option):
145
+ #
146
+ # $ ruby types.rb --xxx --yyy
147
+ #
148
+ # Output:
149
+ #
150
+ # ["--xxx", "--yyy"]
151
+ #
152
+ # If the option is last, an exception is raised:
153
+ #
154
+ # $ ruby types.rb
155
+ # # Raises GetoptLong::MissingArgument
156
+ #
157
+ # ==== Option with Optional Argument
158
+ #
159
+ # An option of type <tt>GetoptLong::OPTIONAL_ARGUMENT</tt>
160
+ # may be followed by an argument, which if given is associated with that option.
161
+ #
162
+ # If the option is last, it does not have an argument:
163
+ #
164
+ # $ ruby types.rb --yyy
165
+ #
166
+ # Output:
167
+ #
168
+ # ["--yyy", ""]
169
+ #
170
+ # If the option is followed by another option, it does not have an argument:
171
+ #
172
+ # $ ruby types.rb --yyy --zzz
173
+ #
174
+ # Output:
175
+ #
176
+ # ["--yyy", ""]
177
+ # ["--zzz", ""]
178
+ #
179
+ # Otherwise the option is followed by its argument, which is associated
180
+ # with that option:
181
+ #
182
+ # $ ruby types.rb --yyy foo
183
+ #
184
+ # Output:
185
+ #
186
+ # ["--yyy", "foo"]
187
+ #
188
+ # ==== Option with No Argument
189
+ #
190
+ # An option of type <tt>GetoptLong::NO_ARGUMENT</tt> takes no argument:
191
+ #
192
+ # ruby types.rb --zzz foo
193
+ #
194
+ # Output:
195
+ #
196
+ # ["--zzz", ""]
197
+ #
198
+ # === ARGV
199
+ #
200
+ # You can process options either with method #each and a block,
201
+ # or with method #get.
202
+ #
203
+ # During processing, each found option is removed, along with its argument
204
+ # if there is one.
205
+ # After processing, each remaining element was neither an option
206
+ # nor the argument for an option.
207
+ #
208
+ # File <tt>argv.rb</tt>:
209
+ #
210
+ # :include: ../sample/getoptlong/argv.rb
211
+ #
212
+ # Command line:
213
+ #
214
+ # $ ruby argv.rb --xxx Foo --yyy Bar Baz --zzz Bat Bam
18
215
  #
19
- # GetoptLong allows for POSIX-style options like <tt>--file</tt> as well
20
- # as single letter options like <tt>-f</tt>
216
+ # Output:
21
217
  #
22
- # The empty option <tt>--</tt> (two minus symbols) is used to end option
23
- # processing. This can be particularly important if options have optional
24
- # arguments.
218
+ # Original ARGV: ["--xxx", "Foo", "--yyy", "Bar", "Baz", "--zzz", "Bat", "Bam"]
219
+ # ["--xxx", "Foo"]
220
+ # ["--yyy", "Bar"]
221
+ # ["--zzz", ""]
222
+ # Remaining ARGV: ["Baz", "Bat", "Bam"]
25
223
  #
26
- # Here is a simple example of usage:
224
+ # === Ordering
27
225
  #
28
- # require 'getoptlong'
226
+ # There are three settings that control the way the options
227
+ # are interpreted:
29
228
  #
30
- # opts = GetoptLong.new(
31
- # [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
32
- # [ '--repeat', '-n', GetoptLong::REQUIRED_ARGUMENT ],
33
- # [ '--name', GetoptLong::OPTIONAL_ARGUMENT ]
34
- # )
229
+ # - +PERMUTE+.
230
+ # - +REQUIRE_ORDER+.
231
+ # - +RETURN_IN_ORDER+.
35
232
  #
36
- # dir = nil
37
- # name = nil
38
- # repetitions = 1
39
- # opts.each do |opt, arg|
40
- # case opt
41
- # when '--help'
42
- # puts <<-EOF
43
- # hello [OPTION] ... DIR
233
+ # The initial setting for a new \GetoptLong object is +REQUIRE_ORDER+
234
+ # if environment variable +POSIXLY_CORRECT+ is defined, +PERMUTE+ otherwise.
44
235
  #
236
+ # ==== PERMUTE Ordering
237
+ #
238
+ # In the +PERMUTE+ ordering, options and other, non-option,
239
+ # arguments may appear in any order and any mixture.
240
+ #
241
+ # File <tt>permute.rb</tt>:
242
+ #
243
+ # :include: ../sample/getoptlong/permute.rb
244
+ #
245
+ # Command line:
246
+ #
247
+ # $ ruby permute.rb Foo --zzz Bar --xxx Baz --yyy Bat Bam --xxx Bag Bah
248
+ #
249
+ # Output:
250
+ #
251
+ # Original ARGV: ["Foo", "--zzz", "Bar", "--xxx", "Baz", "--yyy", "Bat", "Bam", "--xxx", "Bag", "Bah"]
252
+ # ["--zzz", ""]
253
+ # ["--xxx", "Baz"]
254
+ # ["--yyy", "Bat"]
255
+ # ["--xxx", "Bag"]
256
+ # Remaining ARGV: ["Foo", "Bar", "Bam", "Bah"]
257
+ #
258
+ # ==== REQUIRE_ORDER Ordering
259
+ #
260
+ # In the +REQUIRE_ORDER+ ordering, all options precede all non-options;
261
+ # that is, each word after the first non-option word
262
+ # is treated as a non-option word (even if it begins with a hyphen).
263
+ #
264
+ # File <tt>require_order.rb</tt>:
265
+ #
266
+ # :include: ../sample/getoptlong/require_order.rb
267
+ #
268
+ # Command line:
269
+ #
270
+ # $ ruby require_order.rb --xxx Foo Bar --xxx Baz --yyy Bat -zzz
271
+ #
272
+ # Output:
273
+ #
274
+ # Original ARGV: ["--xxx", "Foo", "Bar", "--xxx", "Baz", "--yyy", "Bat", "-zzz"]
275
+ # ["--xxx", "Foo"]
276
+ # Remaining ARGV: ["Bar", "--xxx", "Baz", "--yyy", "Bat", "-zzz"]
277
+ #
278
+ # ==== RETURN_IN_ORDER Ordering
279
+ #
280
+ # In the +RETURN_IN_ORDER+ ordering, every word is treated as an option.
281
+ # A word that begins with a hyphen (or two) is treated in the usual way;
282
+ # a word +word+ that does not so begin is treated as an option
283
+ # whose name is an empty string, and whose value is +word+.
284
+ #
285
+ # File <tt>return_in_order.rb</tt>:
286
+ #
287
+ # :include: ../sample/getoptlong/return_in_order.rb
288
+ #
289
+ # Command line:
290
+ #
291
+ # $ ruby return_in_order.rb Foo --xxx Bar Baz --zzz Bat Bam
292
+ #
293
+ # Output:
294
+ #
295
+ # Original ARGV: ["Foo", "--xxx", "Bar", "Baz", "--zzz", "Bat", "Bam"]
296
+ # ["", "Foo"]
297
+ # ["--xxx", "Bar"]
298
+ # ["", "Baz"]
299
+ # ["--zzz", ""]
300
+ # ["", "Bat"]
301
+ # ["", "Bam"]
302
+ # Remaining ARGV: []
303
+ #
304
+ # === Full Example
305
+ #
306
+ # File <tt>fibonacci.rb</tt>:
307
+ #
308
+ # :include: ../sample/getoptlong/fibonacci.rb
309
+ #
310
+ # Command line:
311
+ #
312
+ # $ ruby fibonacci.rb
313
+ #
314
+ # Output:
315
+ #
316
+ # Option --number is required.
317
+ # Usage:
318
+ #
319
+ # -n n, --number n:
320
+ # Compute Fibonacci number for n.
321
+ # -v [boolean], --verbose [boolean]:
322
+ # Show intermediate results; default is 'false'.
323
+ # -h, --help:
324
+ # Show this help.
325
+ #
326
+ # Command line:
327
+ #
328
+ # $ ruby fibonacci.rb --number
329
+ #
330
+ # Raises GetoptLong::MissingArgument:
331
+ #
332
+ # fibonacci.rb: option `--number' requires an argument
333
+ #
334
+ # Command line:
335
+ #
336
+ # $ ruby fibonacci.rb --number 6
337
+ #
338
+ # Output:
339
+ #
340
+ # 8
341
+ #
342
+ # Command line:
343
+ #
344
+ # $ ruby fibonacci.rb --number 6 --verbose
345
+ #
346
+ # Output:
347
+ # 1
348
+ # 2
349
+ # 3
350
+ # 5
351
+ # 8
352
+ #
353
+ # Command line:
354
+ #
355
+ # $ ruby fibonacci.rb --number 6 --verbose yes
356
+ #
357
+ # Output:
358
+ #
359
+ # --verbose argument must be true or false
360
+ # Usage:
361
+ #
362
+ # -n n, --number n:
363
+ # Compute Fibonacci number for n.
364
+ # -v [boolean], --verbose [boolean]:
365
+ # Show intermediate results; default is 'false'.
45
366
  # -h, --help:
46
- # show help
47
- #
48
- # --repeat x, -n x:
49
- # repeat x times
50
- #
51
- # --name [name]:
52
- # greet user by name, if name not supplied default is John
53
- #
54
- # DIR: The directory in which to issue the greeting.
55
- # EOF
56
- # when '--repeat'
57
- # repetitions = arg.to_i
58
- # when '--name'
59
- # if arg == ''
60
- # name = 'John'
61
- # else
62
- # name = arg
63
- # end
64
- # end
65
- # end
66
- #
67
- # if ARGV.length != 1
68
- # puts "Missing dir argument (try --help)"
69
- # exit 0
70
- # end
71
- #
72
- # dir = ARGV.shift
73
- #
74
- # Dir.chdir(dir)
75
- # for i in (1..repetitions)
76
- # print "Hello"
77
- # if name
78
- # print ", #{name}"
79
- # end
80
- # puts
81
- # end
82
- #
83
- # Example command line:
84
- #
85
- # hello -n 6 --name -- /tmp
367
+ # Show this help.
86
368
  #
87
369
  class GetoptLong
370
+ # Version.
371
+ VERSION = "0.2.0"
372
+
88
373
  #
89
374
  # Orderings.
90
375
  #
@@ -111,20 +396,18 @@ class GetoptLong
111
396
  class InvalidOption < Error; end
112
397
 
113
398
  #
114
- # Set up option processing.
115
- #
116
- # The options to support are passed to new() as an array of arrays.
117
- # Each sub-array contains any number of String option names which carry
118
- # the same meaning, and one of the following flags:
399
+ # Returns a new \GetoptLong object based on the given +arguments+.
400
+ # See {Options}[#class-GetoptLong-label-Options].
119
401
  #
120
- # GetoptLong::NO_ARGUMENT :: Option does not take an argument.
402
+ # Example:
121
403
  #
122
- # GetoptLong::REQUIRED_ARGUMENT :: Option always takes an argument.
404
+ # :include: ../sample/getoptlong/simple.rb
123
405
  #
124
- # GetoptLong::OPTIONAL_ARGUMENT :: Option may or may not take an argument.
406
+ # Raises an exception if:
125
407
  #
126
- # The first option name is considered to be the preferred (canonical) name.
127
- # Other than that, the elements of each sub-array can be in any order.
408
+ # - Any of +arguments+ is not an array.
409
+ # - Any option name or alias is not a string.
410
+ # - Any option type is invalid.
128
411
  #
129
412
  def initialize(*arguments)
130
413
  #
@@ -186,54 +469,22 @@ class GetoptLong
186
469
  end
187
470
  end
188
471
 
472
+ # Sets the ordering; see {Ordering}[#class-GetoptLong-label-Ordering];
473
+ # returns the new ordering.
189
474
  #
190
- # Set the handling of the ordering of options and arguments.
191
- # A RuntimeError is raised if option processing has already started.
192
- #
193
- # The supplied value must be a member of GetoptLong::ORDERINGS. It alters
194
- # the processing of options as follows:
195
- #
196
- # <b>REQUIRE_ORDER</b> :
197
- #
198
- # Options are required to occur before non-options.
199
- #
200
- # Processing of options ends as soon as a word is encountered that has not
201
- # been preceded by an appropriate option flag.
202
- #
203
- # For example, if -a and -b are options which do not take arguments,
204
- # parsing command line arguments of '-a one -b two' would result in
205
- # 'one', '-b', 'two' being left in ARGV, and only ('-a', '') being
206
- # processed as an option/arg pair.
207
- #
208
- # This is the default ordering, if the environment variable
209
- # POSIXLY_CORRECT is set. (This is for compatibility with GNU getopt_long.)
210
- #
211
- # <b>PERMUTE</b> :
212
- #
213
- # Options can occur anywhere in the command line parsed. This is the
214
- # default behavior.
215
- #
216
- # Every sequence of words which can be interpreted as an option (with or
217
- # without argument) is treated as an option; non-option words are skipped.
218
- #
219
- # For example, if -a does not require an argument and -b optionally takes
220
- # an argument, parsing '-a one -b two three' would result in ('-a','') and
221
- # ('-b', 'two') being processed as option/arg pairs, and 'one','three'
222
- # being left in ARGV.
475
+ # If the given +ordering+ is +PERMUTE+ and environment variable
476
+ # +POSIXLY_CORRECT+ is defined, sets the ordering to +REQUIRE_ORDER+;
477
+ # otherwise sets the ordering to +ordering+:
223
478
  #
224
- # If the ordering is set to PERMUTE but the environment variable
225
- # POSIXLY_CORRECT is set, REQUIRE_ORDER is used instead. This is for
226
- # compatibility with GNU getopt_long.
479
+ # options = GetoptLong.new
480
+ # options.ordering == GetoptLong::PERMUTE # => true
481
+ # options.ordering = GetoptLong::RETURN_IN_ORDER
482
+ # options.ordering == GetoptLong::RETURN_IN_ORDER # => true
483
+ # ENV['POSIXLY_CORRECT'] = 'true'
484
+ # options.ordering = GetoptLong::PERMUTE
485
+ # options.ordering == GetoptLong::REQUIRE_ORDER # => true
227
486
  #
228
- # <b>RETURN_IN_ORDER</b> :
229
- #
230
- # All words on the command line are processed as options. Words not
231
- # preceded by a short or long option flag are passed as arguments
232
- # with an option of '' (empty string).
233
- #
234
- # For example, if -a requires an argument but -b does not, a command line
235
- # of '-a one -b two three' would result in option/arg pairs of ('-a', 'one')
236
- # ('-b', ''), ('', 'two'), ('', 'three') being processed.
487
+ # Raises an exception if +ordering+ is invalid.
237
488
  #
238
489
  def ordering=(ordering)
239
490
  #
@@ -259,14 +510,16 @@ class GetoptLong
259
510
  end
260
511
 
261
512
  #
262
- # Return ordering.
513
+ # Returns the ordering setting.
263
514
  #
264
515
  attr_reader :ordering
265
516
 
266
517
  #
267
- # Set options. Takes the same argument as GetoptLong.new.
518
+ # Replaces existing options with those given by +arguments+,
519
+ # which have the same form as the arguments to ::new;
520
+ # returns +self+.
268
521
  #
269
- # Raises a RuntimeError if option processing has already started.
522
+ # Raises an exception if option processing has begun.
270
523
  #
271
524
  def set_options(*arguments)
272
525
  #
@@ -338,22 +591,23 @@ class GetoptLong
338
591
  end
339
592
 
340
593
  #
341
- # Set/Unset `quiet' mode.
594
+ # Sets quiet mode and returns the given argument:
595
+ #
596
+ # - When +false+ or +nil+, error messages are written to <tt>$stdout</tt>.
597
+ # - Otherwise, error messages are not written.
342
598
  #
343
599
  attr_writer :quiet
344
600
 
345
601
  #
346
- # Return the flag of `quiet' mode.
602
+ # Returns the quiet mode setting.
347
603
  #
348
604
  attr_reader :quiet
349
-
350
- #
351
- # `quiet?' is an alias of `quiet'.
352
- #
353
605
  alias quiet? quiet
354
606
 
355
607
  #
356
- # Explicitly terminate option processing.
608
+ # Terminate option processing;
609
+ # returns +nil+ if processing has already terminated;
610
+ # otherwise returns +self+.
357
611
  #
358
612
  def terminate
359
613
  return nil if @status == STATUS_TERMINATED
@@ -373,14 +627,14 @@ class GetoptLong
373
627
  end
374
628
 
375
629
  #
376
- # Returns true if option processing has terminated, false otherwise.
630
+ # Returns +true+ if option processing has terminated, +false+ otherwise.
377
631
  #
378
632
  def terminated?
379
633
  return @status == STATUS_TERMINATED
380
634
  end
381
635
 
382
636
  #
383
- # Set an error (a protected method).
637
+ # \Set an error (a protected method).
384
638
  #
385
639
  def set_error(type, message)
386
640
  $stderr.print("#{$0}: #{message}\n") if !@quiet
@@ -397,32 +651,25 @@ class GetoptLong
397
651
  protected :set_error
398
652
 
399
653
  #
400
- # Examine whether an option processing is failed.
654
+ # Returns whether option processing has failed.
401
655
  #
402
656
  attr_reader :error
403
-
404
- #
405
- # `error?' is an alias of `error'.
406
- #
407
657
  alias error? error
408
658
 
409
659
  # Return the appropriate error message in POSIX-defined format.
410
- # If no error has occurred, returns nil.
660
+ # If no error has occurred, returns +nil+.
411
661
  #
412
662
  def error_message
413
663
  return @error_message
414
664
  end
415
665
 
416
666
  #
417
- # Get next option name and its argument, as an Array of two elements.
418
- #
419
- # The option name is always converted to the first (preferred)
420
- # name given in the original options to GetoptLong.new.
667
+ # Returns the next option as a 2-element array containing:
421
668
  #
422
- # Example: ['--option', 'value']
669
+ # - The option name (the name itself, not an alias).
670
+ # - The option value.
423
671
  #
424
- # Returns nil if the processing is complete (as determined by
425
- # STATUS_TERMINATED).
672
+ # Returns +nil+ if there are no more options.
426
673
  #
427
674
  def get
428
675
  option_name, option_argument = nil, ''
@@ -582,21 +829,32 @@ class GetoptLong
582
829
 
583
830
  return @canonical_names[option_name], option_argument
584
831
  end
832
+ alias get_option get
585
833
 
586
834
  #
587
- # `get_option' is an alias of `get'.
835
+ # Calls the given block with each option;
836
+ # each option is a 2-element array containing:
588
837
  #
589
- alias get_option get
590
-
591
- # Iterator version of `get'.
838
+ # - The option name (the name itself, not an alias).
839
+ # - The option value.
840
+ #
841
+ # Example:
842
+ #
843
+ # :include: ../sample/getoptlong/each.rb
592
844
  #
593
- # The block is called repeatedly with two arguments:
594
- # The first is the option name.
595
- # The second is the argument which followed it (if any).
596
- # Example: ('--opt', 'value')
845
+ # Command line:
597
846
  #
598
- # The option name is always converted to the first (preferred)
599
- # name given in the original options to GetoptLong.new.
847
+ # ruby each.rb -xxx Foo -x Bar --yyy Baz -y Bat --zzz
848
+ #
849
+ # Output:
850
+ #
851
+ # Original ARGV: ["-xxx", "Foo", "-x", "Bar", "--yyy", "Baz", "-y", "Bat", "--zzz"]
852
+ # ["--xxx", "xx"]
853
+ # ["--xxx", "Bar"]
854
+ # ["--yyy", "Baz"]
855
+ # ["--yyy", "Bat"]
856
+ # ["--zzz", ""]
857
+ # Remaining ARGV: ["Foo"]
600
858
  #
601
859
  def each
602
860
  loop do
@@ -605,9 +863,5 @@ class GetoptLong
605
863
  yield option_name, option_argument
606
864
  end
607
865
  end
608
-
609
- #
610
- # `each_option' is an alias of `each'.
611
- #
612
866
  alias each_option each
613
867
  end
@@ -0,0 +1,9 @@
1
+ require 'getoptlong'
2
+
3
+ options = GetoptLong.new(
4
+ ['--xxx', GetoptLong::NO_ARGUMENT],
5
+ ['--xyz', GetoptLong::NO_ARGUMENT]
6
+ )
7
+ options.each do |option, argument|
8
+ p [option, argument]
9
+ end
@@ -0,0 +1,8 @@
1
+ require 'getoptlong'
2
+
3
+ options = GetoptLong.new(
4
+ ['--xxx', '-x', '--aaa', '-a', '-p', GetoptLong::NO_ARGUMENT]
5
+ )
6
+ options.each do |option, argument|
7
+ p [option, argument]
8
+ end
@@ -0,0 +1,12 @@
1
+ require 'getoptlong'
2
+
3
+ options = GetoptLong.new(
4
+ ['--xxx', GetoptLong::REQUIRED_ARGUMENT],
5
+ ['--yyy', GetoptLong::OPTIONAL_ARGUMENT],
6
+ ['--zzz', GetoptLong::NO_ARGUMENT]
7
+ )
8
+ puts "Original ARGV: #{ARGV}"
9
+ options.each do |option, argument|
10
+ p [option, argument]
11
+ end
12
+ puts "Remaining ARGV: #{ARGV}"
@@ -0,0 +1,12 @@
1
+ require 'getoptlong'
2
+
3
+ options = GetoptLong.new(
4
+ ['--xxx', '-x', GetoptLong::REQUIRED_ARGUMENT],
5
+ ['--yyy', '-y', GetoptLong::OPTIONAL_ARGUMENT],
6
+ ['--zzz', '-z',GetoptLong::NO_ARGUMENT]
7
+ )
8
+ puts "Original ARGV: #{ARGV}"
9
+ options.each do |option, argument|
10
+ p [option, argument]
11
+ end
12
+ puts "Remaining ARGV: #{ARGV}"
@@ -0,0 +1,62 @@
1
+ require 'getoptlong'
2
+
3
+ options = GetoptLong.new(
4
+ ['--number', '-n', GetoptLong::REQUIRED_ARGUMENT],
5
+ ['--verbose', '-v', GetoptLong::OPTIONAL_ARGUMENT],
6
+ ['--help', '-h', GetoptLong::NO_ARGUMENT]
7
+ )
8
+
9
+ def help(status = 0)
10
+ puts <<~HELP
11
+ Usage:
12
+
13
+ -n n, --number n:
14
+ Compute Fibonacci number for n.
15
+ -v [boolean], --verbose [boolean]:
16
+ Show intermediate results; default is 'false'.
17
+ -h, --help:
18
+ Show this help.
19
+ HELP
20
+ exit(status)
21
+ end
22
+
23
+ def print_fibonacci (number)
24
+ return 0 if number == 0
25
+ return 1 if number == 1 or number == 2
26
+ i = 0
27
+ j = 1
28
+ (2..number).each do
29
+ k = i + j
30
+ i = j
31
+ j = k
32
+ puts j if @verbose
33
+ end
34
+ puts j unless @verbose
35
+ end
36
+
37
+ options.each do |option, argument|
38
+ case option
39
+ when '--number'
40
+ @number = argument.to_i
41
+ when '--verbose'
42
+ @verbose = if argument.empty?
43
+ true
44
+ elsif argument.match(/true/i)
45
+ true
46
+ elsif argument.match(/false/i)
47
+ false
48
+ else
49
+ puts '--verbose argument must be true or false'
50
+ help(255)
51
+ end
52
+ when '--help'
53
+ help
54
+ end
55
+ end
56
+
57
+ unless @number
58
+ puts 'Option --number is required.'
59
+ help(255)
60
+ end
61
+
62
+ print_fibonacci(@number)
@@ -0,0 +1,12 @@
1
+ require 'getoptlong'
2
+
3
+ options = GetoptLong.new(
4
+ ['--xxx', GetoptLong::REQUIRED_ARGUMENT],
5
+ ['--yyy', GetoptLong::OPTIONAL_ARGUMENT],
6
+ ['--zzz', GetoptLong::NO_ARGUMENT]
7
+ )
8
+ puts "Original ARGV: #{ARGV}"
9
+ options.each do |option, argument|
10
+ p [option, argument]
11
+ end
12
+ puts "Remaining ARGV: #{ARGV}"
@@ -0,0 +1,13 @@
1
+ require 'getoptlong'
2
+
3
+ options = GetoptLong.new(
4
+ ['--xxx', GetoptLong::REQUIRED_ARGUMENT],
5
+ ['--yyy', GetoptLong::OPTIONAL_ARGUMENT],
6
+ ['--zzz', GetoptLong::NO_ARGUMENT]
7
+ )
8
+ options.ordering = GetoptLong::REQUIRE_ORDER
9
+ puts "Original ARGV: #{ARGV}"
10
+ options.each do |option, argument|
11
+ p [option, argument]
12
+ end
13
+ puts "Remaining ARGV: #{ARGV}"
@@ -0,0 +1,13 @@
1
+ require 'getoptlong'
2
+
3
+ options = GetoptLong.new(
4
+ ['--xxx', GetoptLong::REQUIRED_ARGUMENT],
5
+ ['--yyy', GetoptLong::OPTIONAL_ARGUMENT],
6
+ ['--zzz', GetoptLong::NO_ARGUMENT]
7
+ )
8
+ options.ordering = GetoptLong::RETURN_IN_ORDER
9
+ puts "Original ARGV: #{ARGV}"
10
+ options.each do |option, argument|
11
+ p [option, argument]
12
+ end
13
+ puts "Remaining ARGV: #{ARGV}"
@@ -0,0 +1,7 @@
1
+ require 'getoptlong'
2
+
3
+ options = GetoptLong.new(
4
+ ['--number', '-n', GetoptLong::REQUIRED_ARGUMENT],
5
+ ['--verbose', '-v', GetoptLong::OPTIONAL_ARGUMENT],
6
+ ['--help', '-h', GetoptLong::NO_ARGUMENT]
7
+ )
@@ -0,0 +1,10 @@
1
+ require 'getoptlong'
2
+
3
+ options = GetoptLong.new(
4
+ ['--xxx', GetoptLong::REQUIRED_ARGUMENT],
5
+ ['--yyy', GetoptLong::OPTIONAL_ARGUMENT],
6
+ ['--zzz', GetoptLong::NO_ARGUMENT]
7
+ )
8
+ options.each do |option, argument|
9
+ p [option, argument]
10
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: getoptlong
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yukihiro Matsumoto
8
- autorequire:
9
- bindir: exe
8
+ autorequire:
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-06 00:00:00.000000000 Z
11
+ date: 2022-12-05 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: GetoptLong for Ruby
14
14
  email:
@@ -17,6 +17,8 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - ".github/dependabot.yml"
21
+ - ".github/workflows/test.yml"
20
22
  - ".gitignore"
21
23
  - Gemfile
22
24
  - LICENSE.txt
@@ -26,14 +28,24 @@ files:
26
28
  - bin/setup
27
29
  - getoptlong.gemspec
28
30
  - lib/getoptlong.rb
29
- - lib/getoptlong/version.rb
31
+ - sample/getoptlong/abbrev.rb
32
+ - sample/getoptlong/aliases.rb
33
+ - sample/getoptlong/argv.rb
34
+ - sample/getoptlong/each.rb
35
+ - sample/getoptlong/fibonacci.rb
36
+ - sample/getoptlong/permute.rb
37
+ - sample/getoptlong/require_order.rb
38
+ - sample/getoptlong/return_in_order.rb
39
+ - sample/getoptlong/simple.rb
40
+ - sample/getoptlong/types.rb
30
41
  homepage: https://github.com/ruby/getoptlong
31
42
  licenses:
43
+ - Ruby
32
44
  - BSD-2-Clause
33
45
  metadata:
34
46
  homepage_uri: https://github.com/ruby/getoptlong
35
47
  source_code_uri: https://github.com/ruby/getoptlong
36
- post_install_message:
48
+ post_install_message:
37
49
  rdoc_options: []
38
50
  require_paths:
39
51
  - lib
@@ -48,8 +60,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
48
60
  - !ruby/object:Gem::Version
49
61
  version: '0'
50
62
  requirements: []
51
- rubygems_version: 3.0.3
52
- signing_key:
63
+ rubygems_version: 3.4.0.dev
64
+ signing_key:
53
65
  specification_version: 4
54
66
  summary: GetoptLong for Ruby
55
67
  test_files: []
@@ -1,3 +0,0 @@
1
- class GetoptLong
2
- VERSION = "0.1.0"
3
- end