diff options
| author | Burdette Lamar <BurdetteLamar@Yahoo.com> | 2022-05-02 11:03:26 -0500 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2022-05-03 01:09:33 +0900 |
| commit | 51ac3c9e80562a63bbab4b67be916a9f37b6e842 (patch) | |
| tree | b8612ab9e406c4dc30a4e3be8678bf7326b9845f /examples | |
| parent | 8587bacc252e95e533d319cc58b58ec11e5561ff (diff) | |
[ruby/getoptlong] Enhanced RDoc for GetoptLong (https://github.com/ruby/getoptlong/pull/4)
Detailed introductory material.
https://github.com/ruby/getoptlong/commit/1544f2fb7b
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/getoptlong/abbrev.rb | 9 | ||||
| -rw-r--r-- | examples/getoptlong/aliases.rb | 8 | ||||
| -rw-r--r-- | examples/getoptlong/argv.rb | 12 | ||||
| -rw-r--r-- | examples/getoptlong/each.rb | 12 | ||||
| -rw-r--r-- | examples/getoptlong/fibonacci.rb | 62 | ||||
| -rw-r--r-- | examples/getoptlong/permute.rb | 12 | ||||
| -rw-r--r-- | examples/getoptlong/require_order.rb | 13 | ||||
| -rw-r--r-- | examples/getoptlong/return_in_order.rb | 13 | ||||
| -rw-r--r-- | examples/getoptlong/simple.rb | 7 | ||||
| -rw-r--r-- | examples/getoptlong/types.rb | 10 |
10 files changed, 158 insertions, 0 deletions
diff --git a/examples/getoptlong/abbrev.rb b/examples/getoptlong/abbrev.rb new file mode 100644 index 0000000000..9b89863626 --- /dev/null +++ b/examples/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/examples/getoptlong/aliases.rb b/examples/getoptlong/aliases.rb new file mode 100644 index 0000000000..895254c6ae --- /dev/null +++ b/examples/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/examples/getoptlong/argv.rb b/examples/getoptlong/argv.rb new file mode 100644 index 0000000000..8efcad22ea --- /dev/null +++ b/examples/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/examples/getoptlong/each.rb b/examples/getoptlong/each.rb new file mode 100644 index 0000000000..661e0a968f --- /dev/null +++ b/examples/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/examples/getoptlong/fibonacci.rb b/examples/getoptlong/fibonacci.rb new file mode 100644 index 0000000000..24a2aab3c3 --- /dev/null +++ b/examples/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/examples/getoptlong/permute.rb b/examples/getoptlong/permute.rb new file mode 100644 index 0000000000..8efcad22ea --- /dev/null +++ b/examples/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/examples/getoptlong/require_order.rb b/examples/getoptlong/require_order.rb new file mode 100644 index 0000000000..357f667905 --- /dev/null +++ b/examples/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/examples/getoptlong/return_in_order.rb b/examples/getoptlong/return_in_order.rb new file mode 100644 index 0000000000..91ce1ef996 --- /dev/null +++ b/examples/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/examples/getoptlong/simple.rb b/examples/getoptlong/simple.rb new file mode 100644 index 0000000000..1af6447632 --- /dev/null +++ b/examples/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/examples/getoptlong/types.rb b/examples/getoptlong/types.rb new file mode 100644 index 0000000000..ac74bfe12e --- /dev/null +++ b/examples/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 |
