summaryrefslogtreecommitdiff
path: root/sample
diff options
context:
space:
mode:
authorHiroshi SHIBATA <hsbt@ruby-lang.org>2022-05-09 07:10:45 +0900
committergit <svn-admin@ruby-lang.org>2022-05-09 07:13:55 +0900
commitf7539d5758ee5c83e6ec7792b100e830180896fd (patch)
tree748d9b2657b62b70e3f9865a493734275d272c33 /sample
parent98e3fdb44458615a1376cc60ea0909d07a6c2e4a (diff)
downloadruby-f7539d5758ee5c83e6ec7792b100e830180896fd.tar.gz
[ruby/getoptlong] ruby/ruby used sample, not examples
https://github.com/ruby/getoptlong/commit/39faa7b390
Diffstat (limited to 'sample')
-rw-r--r--sample/getoptlong/abbrev.rb9
-rw-r--r--sample/getoptlong/aliases.rb8
-rw-r--r--sample/getoptlong/argv.rb12
-rw-r--r--sample/getoptlong/each.rb12
-rw-r--r--sample/getoptlong/fibonacci.rb62
-rw-r--r--sample/getoptlong/permute.rb12
-rw-r--r--sample/getoptlong/require_order.rb13
-rw-r--r--sample/getoptlong/return_in_order.rb13
-rw-r--r--sample/getoptlong/simple.rb7
-rw-r--r--sample/getoptlong/types.rb10
10 files changed, 158 insertions, 0 deletions
diff --git a/sample/getoptlong/abbrev.rb b/sample/getoptlong/abbrev.rb
new file mode 100644
index 0000000000..9b89863626
--- /dev/null
+++ b/sample/getoptlong/abbrev.rb
@@ -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
diff --git a/sample/getoptlong/aliases.rb b/sample/getoptlong/aliases.rb
new file mode 100644
index 0000000000..895254c6ae
--- /dev/null
+++ b/sample/getoptlong/aliases.rb
@@ -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
diff --git a/sample/getoptlong/argv.rb b/sample/getoptlong/argv.rb
new file mode 100644
index 0000000000..8efcad22ea
--- /dev/null
+++ b/sample/getoptlong/argv.rb
@@ -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}"
diff --git a/sample/getoptlong/each.rb b/sample/getoptlong/each.rb
new file mode 100644
index 0000000000..661e0a968f
--- /dev/null
+++ b/sample/getoptlong/each.rb
@@ -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}"
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)
diff --git a/sample/getoptlong/permute.rb b/sample/getoptlong/permute.rb
new file mode 100644
index 0000000000..8efcad22ea
--- /dev/null
+++ b/sample/getoptlong/permute.rb
@@ -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}"
diff --git a/sample/getoptlong/require_order.rb b/sample/getoptlong/require_order.rb
new file mode 100644
index 0000000000..357f667905
--- /dev/null
+++ b/sample/getoptlong/require_order.rb
@@ -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}"
diff --git a/sample/getoptlong/return_in_order.rb b/sample/getoptlong/return_in_order.rb
new file mode 100644
index 0000000000..91ce1ef996
--- /dev/null
+++ b/sample/getoptlong/return_in_order.rb
@@ -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}"
diff --git a/sample/getoptlong/simple.rb b/sample/getoptlong/simple.rb
new file mode 100644
index 0000000000..1af6447632
--- /dev/null
+++ b/sample/getoptlong/simple.rb
@@ -0,0 +1,7 @@
+require 'getoptlong'
+
+options = GetoptLong.new(
+ ['--number', '-n', GetoptLong::REQUIRED_ARGUMENT],
+ ['--verbose', '-v', GetoptLong::OPTIONAL_ARGUMENT],
+ ['--help', '-h', GetoptLong::NO_ARGUMENT]
+)
diff --git a/sample/getoptlong/types.rb b/sample/getoptlong/types.rb
new file mode 100644
index 0000000000..ac74bfe12e
--- /dev/null
+++ b/sample/getoptlong/types.rb
@@ -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