mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
parent
98e3fdb444
commit
f7539d5758
10 changed files with 0 additions and 0 deletions
9
sample/getoptlong/abbrev.rb
Normal file
9
sample/getoptlong/abbrev.rb
Normal 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
|
8
sample/getoptlong/aliases.rb
Normal file
8
sample/getoptlong/aliases.rb
Normal 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
12
sample/getoptlong/argv.rb
Normal 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
12
sample/getoptlong/each.rb
Normal 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}"
|
62
sample/getoptlong/fibonacci.rb
Normal file
62
sample/getoptlong/fibonacci.rb
Normal 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)
|
12
sample/getoptlong/permute.rb
Normal file
12
sample/getoptlong/permute.rb
Normal 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}"
|
13
sample/getoptlong/require_order.rb
Normal file
13
sample/getoptlong/require_order.rb
Normal 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}"
|
13
sample/getoptlong/return_in_order.rb
Normal file
13
sample/getoptlong/return_in_order.rb
Normal 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}"
|
7
sample/getoptlong/simple.rb
Normal file
7
sample/getoptlong/simple.rb
Normal 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]
|
||||
)
|
10
sample/getoptlong/types.rb
Normal file
10
sample/getoptlong/types.rb
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue