getoptlong 0.1.1 → 0.2.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: fe118ad350e6e4ce078d6e4ded76429fddb613f33c4105c5f96678fef3609c87
4
- data.tar.gz: daa88d5ccd67de73c0d68fe0891f9badbf09a4ef8f2cedb6143e08ec4cf312b2
3
+ metadata.gz: 4b270eb9d604cd1ffd4d5f0470b369d9c336c950d7feb0d2a07b4552048f0c85
4
+ data.tar.gz: 2443fc1bdf21695a9fe0c444d239064930eb3fbbe7b36638f5ed30879a22bc76
5
5
  SHA512:
6
- metadata.gz: 256c66e80a1e88c3eb1d73770a7e8f439f47d7162c96ce1c2748893861ebae2e38418b55e4f0faf5edda4aa735123ccfcc3f9c0fbe289fa6338e3694826ee975
7
- data.tar.gz: ba59b59fabb7123d30207277db73b55db4dcb5a339b59810d75ee23baf969677f93200fc859d206fc3b16346647de5bb03d9c2eacf0ce12e849619247a060aa9
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
@@ -26,7 +26,5 @@ Gem::Specification.new do |spec|
26
26
  spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
27
27
  `git ls-files -z 2>/dev/null`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
28
28
  end
29
- spec.bindir = "exe"
30
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
29
  spec.require_paths = ["lib"]
32
30
  end
data/lib/getoptlong.rb CHANGED
@@ -6,87 +6,369 @@
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:
18
213
  #
19
- # GetoptLong allows for POSIX-style options like <tt>--file</tt> as well
20
- # as single letter options like <tt>-f</tt>
214
+ # $ ruby argv.rb --xxx Foo --yyy Bar Baz --zzz Bat Bam
21
215
  #
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.
216
+ # Output:
25
217
  #
26
- # Here is a simple example of usage:
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"]
27
223
  #
28
- # require 'getoptlong'
224
+ # === Ordering
29
225
  #
30
- # opts = GetoptLong.new(
31
- # [ '--help', '-h', GetoptLong::NO_ARGUMENT ],
32
- # [ '--repeat', '-n', GetoptLong::REQUIRED_ARGUMENT ],
33
- # [ '--name', GetoptLong::OPTIONAL_ARGUMENT ]
34
- # )
226
+ # There are three settings that control the way the options
227
+ # are interpreted:
35
228
  #
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
229
+ # - +PERMUTE+.
230
+ # - +REQUIRE_ORDER+.
231
+ # - +RETURN_IN_ORDER+.
44
232
  #
233
+ # The initial setting for a new \GetoptLong object is +REQUIRE_ORDER+
234
+ # if environment variable +POSIXLY_CORRECT+ is defined, +PERMUTE+ otherwise.
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
88
370
  # Version.
89
- VERSION = "0.1.1"
371
+ VERSION = "0.2.0"
90
372
 
91
373
  #
92
374
  # Orderings.
@@ -114,20 +396,18 @@ class GetoptLong
114
396
  class InvalidOption < Error; end
115
397
 
116
398
  #
117
- # Set up option processing.
118
- #
119
- # The options to support are passed to new() as an array of arrays.
120
- # Each sub-array contains any number of String option names which carry
121
- # 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].
122
401
  #
123
- # GetoptLong::NO_ARGUMENT :: Option does not take an argument.
402
+ # Example:
124
403
  #
125
- # GetoptLong::REQUIRED_ARGUMENT :: Option always takes an argument.
404
+ # :include: ../sample/getoptlong/simple.rb
126
405
  #
127
- # GetoptLong::OPTIONAL_ARGUMENT :: Option may or may not take an argument.
406
+ # Raises an exception if:
128
407
  #
129
- # The first option name is considered to be the preferred (canonical) name.
130
- # 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.
131
411
  #
132
412
  def initialize(*arguments)
133
413
  #
@@ -189,54 +469,22 @@ class GetoptLong
189
469
  end
190
470
  end
191
471
 
472
+ # Sets the ordering; see {Ordering}[#class-GetoptLong-label-Ordering];
473
+ # returns the new ordering.
192
474
  #
193
- # Set the handling of the ordering of options and arguments.
194
- # A RuntimeError is raised if option processing has already started.
195
- #
196
- # The supplied value must be a member of GetoptLong::ORDERINGS. It alters
197
- # the processing of options as follows:
198
- #
199
- # <b>REQUIRE_ORDER</b> :
200
- #
201
- # Options are required to occur before non-options.
202
- #
203
- # Processing of options ends as soon as a word is encountered that has not
204
- # been preceded by an appropriate option flag.
205
- #
206
- # For example, if -a and -b are options which do not take arguments,
207
- # parsing command line arguments of '-a one -b two' would result in
208
- # 'one', '-b', 'two' being left in ARGV, and only ('-a', '') being
209
- # processed as an option/arg pair.
210
- #
211
- # This is the default ordering, if the environment variable
212
- # POSIXLY_CORRECT is set. (This is for compatibility with GNU getopt_long.)
213
- #
214
- # <b>PERMUTE</b> :
215
- #
216
- # Options can occur anywhere in the command line parsed. This is the
217
- # default behavior.
218
- #
219
- # Every sequence of words which can be interpreted as an option (with or
220
- # without argument) is treated as an option; non-option words are skipped.
221
- #
222
- # For example, if -a does not require an argument and -b optionally takes
223
- # an argument, parsing '-a one -b two three' would result in ('-a','') and
224
- # ('-b', 'two') being processed as option/arg pairs, and 'one','three'
225
- # 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+:
226
478
  #
227
- # If the ordering is set to PERMUTE but the environment variable
228
- # POSIXLY_CORRECT is set, REQUIRE_ORDER is used instead. This is for
229
- # 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
230
486
  #
231
- # <b>RETURN_IN_ORDER</b> :
232
- #
233
- # All words on the command line are processed as options. Words not
234
- # preceded by a short or long option flag are passed as arguments
235
- # with an option of '' (empty string).
236
- #
237
- # For example, if -a requires an argument but -b does not, a command line
238
- # of '-a one -b two three' would result in option/arg pairs of ('-a', 'one')
239
- # ('-b', ''), ('', 'two'), ('', 'three') being processed.
487
+ # Raises an exception if +ordering+ is invalid.
240
488
  #
241
489
  def ordering=(ordering)
242
490
  #
@@ -262,14 +510,16 @@ class GetoptLong
262
510
  end
263
511
 
264
512
  #
265
- # Return ordering.
513
+ # Returns the ordering setting.
266
514
  #
267
515
  attr_reader :ordering
268
516
 
269
517
  #
270
- # 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+.
271
521
  #
272
- # Raises a RuntimeError if option processing has already started.
522
+ # Raises an exception if option processing has begun.
273
523
  #
274
524
  def set_options(*arguments)
275
525
  #
@@ -341,22 +591,23 @@ class GetoptLong
341
591
  end
342
592
 
343
593
  #
344
- # 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.
345
598
  #
346
599
  attr_writer :quiet
347
600
 
348
601
  #
349
- # Return the flag of `quiet' mode.
602
+ # Returns the quiet mode setting.
350
603
  #
351
604
  attr_reader :quiet
352
-
353
- #
354
- # `quiet?' is an alias of `quiet'.
355
- #
356
605
  alias quiet? quiet
357
606
 
358
607
  #
359
- # Explicitly terminate option processing.
608
+ # Terminate option processing;
609
+ # returns +nil+ if processing has already terminated;
610
+ # otherwise returns +self+.
360
611
  #
361
612
  def terminate
362
613
  return nil if @status == STATUS_TERMINATED
@@ -376,14 +627,14 @@ class GetoptLong
376
627
  end
377
628
 
378
629
  #
379
- # Returns true if option processing has terminated, false otherwise.
630
+ # Returns +true+ if option processing has terminated, +false+ otherwise.
380
631
  #
381
632
  def terminated?
382
633
  return @status == STATUS_TERMINATED
383
634
  end
384
635
 
385
636
  #
386
- # Set an error (a protected method).
637
+ # \Set an error (a protected method).
387
638
  #
388
639
  def set_error(type, message)
389
640
  $stderr.print("#{$0}: #{message}\n") if !@quiet
@@ -400,32 +651,25 @@ class GetoptLong
400
651
  protected :set_error
401
652
 
402
653
  #
403
- # Examine whether an option processing is failed.
654
+ # Returns whether option processing has failed.
404
655
  #
405
656
  attr_reader :error
406
-
407
- #
408
- # `error?' is an alias of `error'.
409
- #
410
657
  alias error? error
411
658
 
412
659
  # Return the appropriate error message in POSIX-defined format.
413
- # If no error has occurred, returns nil.
660
+ # If no error has occurred, returns +nil+.
414
661
  #
415
662
  def error_message
416
663
  return @error_message
417
664
  end
418
665
 
419
666
  #
420
- # Get next option name and its argument, as an Array of two elements.
421
- #
422
- # The option name is always converted to the first (preferred)
423
- # name given in the original options to GetoptLong.new.
667
+ # Returns the next option as a 2-element array containing:
424
668
  #
425
- # Example: ['--option', 'value']
669
+ # - The option name (the name itself, not an alias).
670
+ # - The option value.
426
671
  #
427
- # Returns nil if the processing is complete (as determined by
428
- # STATUS_TERMINATED).
672
+ # Returns +nil+ if there are no more options.
429
673
  #
430
674
  def get
431
675
  option_name, option_argument = nil, ''
@@ -585,21 +829,32 @@ class GetoptLong
585
829
 
586
830
  return @canonical_names[option_name], option_argument
587
831
  end
832
+ alias get_option get
588
833
 
589
834
  #
590
- # `get_option' is an alias of `get'.
835
+ # Calls the given block with each option;
836
+ # each option is a 2-element array containing:
591
837
  #
592
- alias get_option get
593
-
594
- # 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
595
844
  #
596
- # The block is called repeatedly with two arguments:
597
- # The first is the option name.
598
- # The second is the argument which followed it (if any).
599
- # Example: ('--opt', 'value')
845
+ # Command line:
600
846
  #
601
- # The option name is always converted to the first (preferred)
602
- # 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"]
603
858
  #
604
859
  def each
605
860
  loop do
@@ -608,9 +863,5 @@ class GetoptLong
608
863
  yield option_name, option_argument
609
864
  end
610
865
  end
611
-
612
- #
613
- # `each_option' is an alias of `each'.
614
- #
615
866
  alias each_option each
616
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.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yukihiro Matsumoto
8
8
  autorequire:
9
- bindir: exe
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-22 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,6 +28,16 @@ files:
26
28
  - bin/setup
27
29
  - getoptlong.gemspec
28
30
  - lib/getoptlong.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
29
41
  homepage: https://github.com/ruby/getoptlong
30
42
  licenses:
31
43
  - Ruby
@@ -48,7 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
48
60
  - !ruby/object:Gem::Version
49
61
  version: '0'
50
62
  requirements: []
51
- rubygems_version: 3.2.2
63
+ rubygems_version: 3.4.0.dev
52
64
  signing_key:
53
65
  specification_version: 4
54
66
  summary: GetoptLong for Ruby