summaryrefslogtreecommitdiff
path: root/sample/getoptlong/fibonacci.rb
diff options
context:
space:
mode:
Diffstat (limited to 'sample/getoptlong/fibonacci.rb')
-rw-r--r--sample/getoptlong/fibonacci.rb62
1 files changed, 62 insertions, 0 deletions
diff --git a/sample/getoptlong/fibonacci.rb b/sample/getoptlong/fibonacci.rb
new file mode 100644
index 0000000000..24a2aab3c3
--- /dev/null
+++ b/sample/getoptlong/fibonacci.rb
@@ -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)