From f7539d5758ee5c83e6ec7792b100e830180896fd Mon Sep 17 00:00:00 2001 From: Hiroshi SHIBATA Date: Mon, 9 May 2022 07:10:45 +0900 Subject: [ruby/getoptlong] ruby/ruby used sample, not examples https://github.com/ruby/getoptlong/commit/39faa7b390 --- sample/getoptlong/abbrev.rb | 9 ++++++ sample/getoptlong/aliases.rb | 8 +++++ sample/getoptlong/argv.rb | 12 +++++++ sample/getoptlong/each.rb | 12 +++++++ sample/getoptlong/fibonacci.rb | 62 ++++++++++++++++++++++++++++++++++++ sample/getoptlong/permute.rb | 12 +++++++ sample/getoptlong/require_order.rb | 13 ++++++++ sample/getoptlong/return_in_order.rb | 13 ++++++++ sample/getoptlong/simple.rb | 7 ++++ sample/getoptlong/types.rb | 10 ++++++ 10 files changed, 158 insertions(+) create mode 100644 sample/getoptlong/abbrev.rb create mode 100644 sample/getoptlong/aliases.rb create mode 100644 sample/getoptlong/argv.rb create mode 100644 sample/getoptlong/each.rb create mode 100644 sample/getoptlong/fibonacci.rb create mode 100644 sample/getoptlong/permute.rb create mode 100644 sample/getoptlong/require_order.rb create mode 100644 sample/getoptlong/return_in_order.rb create mode 100644 sample/getoptlong/simple.rb create mode 100644 sample/getoptlong/types.rb (limited to 'sample') 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 -- cgit v1.2.1