1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/getoptlong] ruby/ruby used sample, not examples

39faa7b390
This commit is contained in:
Hiroshi SHIBATA 2022-05-09 07:10:45 +09:00 committed by git
parent 98e3fdb444
commit f7539d5758
10 changed files with 0 additions and 0 deletions

View file

@ -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

View file

@ -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
sample/getoptlong/argv.rb Normal file
View file

@ -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
sample/getoptlong/each.rb Normal file
View file

@ -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}"

View file

@ -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)

View file

@ -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}"

View file

@ -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}"

View file

@ -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}"

View file

@ -0,0 +1,7 @@
require 'getoptlong'
options = GetoptLong.new(
['--number', '-n', GetoptLong::REQUIRED_ARGUMENT],
['--verbose', '-v', GetoptLong::OPTIONAL_ARGUMENT],
['--help', '-h', GetoptLong::NO_ARGUMENT]
)

View file

@ -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