Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/getoptlong/abbrev.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'getoptlong'

options = GetoptLong.new(
['--xxx', GetoptLong::NO_ARGUMENT],
['--xyz', GetoptLong::NO_ARGUMENT]
)
options.each do |option, argument|
p [option, argument]
end
8 changes: 8 additions & 0 deletions examples/getoptlong/aliases.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'getoptlong'

options = GetoptLong.new(
['--xxx', '-x', '--aaa', '-a', '-p', GetoptLong::NO_ARGUMENT]
)
options.each do |option, argument|
p [option, argument]
end
12 changes: 12 additions & 0 deletions examples/getoptlong/argv.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'getoptlong'

options = GetoptLong.new(
['--xxx', GetoptLong::REQUIRED_ARGUMENT],
['--yyy', GetoptLong::OPTIONAL_ARGUMENT],
['--zzz', GetoptLong::NO_ARGUMENT]
)
puts "Original ARGV: #{ARGV}"
options.each do |option, argument|
p [option, argument]
end
puts "Remaining ARGV: #{ARGV}"
12 changes: 12 additions & 0 deletions examples/getoptlong/each.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'getoptlong'

options = GetoptLong.new(
['--xxx', '-x', GetoptLong::REQUIRED_ARGUMENT],
['--yyy', '-y', GetoptLong::OPTIONAL_ARGUMENT],
['--zzz', '-z',GetoptLong::NO_ARGUMENT]
)
puts "Original ARGV: #{ARGV}"
options.each do |option, argument|
p [option, argument]
end
puts "Remaining ARGV: #{ARGV}"
62 changes: 62 additions & 0 deletions examples/getoptlong/fibonacci.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require 'getoptlong'

options = GetoptLong.new(
['--number', '-n', GetoptLong::REQUIRED_ARGUMENT],
['--verbose', '-v', GetoptLong::OPTIONAL_ARGUMENT],
['--help', '-h', GetoptLong::NO_ARGUMENT]
)

def help(status = 0)
puts <<~HELP
Usage:

-n n, --number n:
Compute Fibonacci number for n.
-v [boolean], --verbose [boolean]:
Show intermediate results; default is 'false'.
-h, --help:
Show this help.
HELP
exit(status)
end

def print_fibonacci (number)
return 0 if number == 0
return 1 if number == 1 or number == 2
i = 0
j = 1
(2..number).each do
k = i + j
i = j
j = k
puts j if @verbose
end
puts j unless @verbose
end

options.each do |option, argument|
case option
when '--number'
@number = argument.to_i
when '--verbose'
@verbose = if argument.empty?
true
elsif argument.match(/true/i)
true
elsif argument.match(/false/i)
false
else
puts '--verbose argument must be true or false'
help(255)
end
when '--help'
help
end
end

unless @number
puts 'Option --number is required.'
help(255)
end

print_fibonacci(@number)
12 changes: 12 additions & 0 deletions examples/getoptlong/permute.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'getoptlong'

options = GetoptLong.new(
['--xxx', GetoptLong::REQUIRED_ARGUMENT],
['--yyy', GetoptLong::OPTIONAL_ARGUMENT],
['--zzz', GetoptLong::NO_ARGUMENT]
)
puts "Original ARGV: #{ARGV}"
options.each do |option, argument|
p [option, argument]
end
puts "Remaining ARGV: #{ARGV}"
13 changes: 13 additions & 0 deletions examples/getoptlong/require_order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'getoptlong'

options = GetoptLong.new(
['--xxx', GetoptLong::REQUIRED_ARGUMENT],
['--yyy', GetoptLong::OPTIONAL_ARGUMENT],
['--zzz', GetoptLong::NO_ARGUMENT]
)
options.ordering = GetoptLong::REQUIRE_ORDER
puts "Original ARGV: #{ARGV}"
options.each do |option, argument|
p [option, argument]
end
puts "Remaining ARGV: #{ARGV}"
13 changes: 13 additions & 0 deletions examples/getoptlong/return_in_order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'getoptlong'

options = GetoptLong.new(
['--xxx', GetoptLong::REQUIRED_ARGUMENT],
['--yyy', GetoptLong::OPTIONAL_ARGUMENT],
['--zzz', GetoptLong::NO_ARGUMENT]
)
options.ordering = GetoptLong::RETURN_IN_ORDER
puts "Original ARGV: #{ARGV}"
options.each do |option, argument|
p [option, argument]
end
puts "Remaining ARGV: #{ARGV}"
7 changes: 7 additions & 0 deletions examples/getoptlong/simple.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'getoptlong'

options = GetoptLong.new(
['--number', '-n', GetoptLong::REQUIRED_ARGUMENT],
['--verbose', '-v', GetoptLong::OPTIONAL_ARGUMENT],
['--help', '-h', GetoptLong::NO_ARGUMENT]
)
10 changes: 10 additions & 0 deletions examples/getoptlong/types.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'getoptlong'

options = GetoptLong.new(
['--xxx', GetoptLong::REQUIRED_ARGUMENT],
['--yyy', GetoptLong::OPTIONAL_ARGUMENT],
['--zzz', GetoptLong::NO_ARGUMENT]
)
options.each do |option, argument|
p [option, argument]
end
Loading