Skip to content

Commit 1544f2f

Browse files
Enhanced RDoc for GetoptLong (#4)
Detailed introductory material.
1 parent 5683297 commit 1544f2f

File tree

11 files changed

+569
-160
lines changed

11 files changed

+569
-160
lines changed

examples/getoptlong/abbrev.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

examples/getoptlong/aliases.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

examples/getoptlong/argv.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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}"

examples/getoptlong/each.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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}"

examples/getoptlong/fibonacci.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

examples/getoptlong/permute.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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}"

examples/getoptlong/require_order.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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}"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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}"

examples/getoptlong/simple.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
)

examples/getoptlong/types.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)