summaryrefslogtreecommitdiff
path: root/doc/ruby
diff options
context:
space:
mode:
Diffstat (limited to 'doc/ruby')
-rw-r--r--doc/ruby/abbreviation.rb9
-rw-r--r--doc/ruby/collected_options.rb8
-rw-r--r--doc/ruby/default_values.rb8
-rw-r--r--doc/ruby/missing_options.rb12
-rw-r--r--doc/ruby/no_abbreviation.rb10
5 files changed, 47 insertions, 0 deletions
diff --git a/doc/ruby/abbreviation.rb b/doc/ruby/abbreviation.rb
new file mode 100644
index 0000000000..b438c1b3dd
--- /dev/null
+++ b/doc/ruby/abbreviation.rb
@@ -0,0 +1,9 @@
+require 'optparse'
+parser = OptionParser.new
+parser.on('-n', '--dry-run',) do |value|
+ p ['--dry-run', value]
+end
+parser.on('-d', '--draft',) do |value|
+ p ['--draft', value]
+end
+parser.parse!
diff --git a/doc/ruby/collected_options.rb b/doc/ruby/collected_options.rb
new file mode 100644
index 0000000000..2115e03a9a
--- /dev/null
+++ b/doc/ruby/collected_options.rb
@@ -0,0 +1,8 @@
+require 'optparse'
+parser = OptionParser.new
+parser.on('-x', '--xxx', 'Short and long, no argument')
+parser.on('-yYYY', '--yyy', 'Short and long, required argument')
+parser.on('-z [ZZZ]', '--zzz', 'Short and long, optional argument')
+options = {}
+parser.parse!(into: options)
+p options
diff --git a/doc/ruby/default_values.rb b/doc/ruby/default_values.rb
new file mode 100644
index 0000000000..24c26faea2
--- /dev/null
+++ b/doc/ruby/default_values.rb
@@ -0,0 +1,8 @@
+require 'optparse'
+parser = OptionParser.new
+parser.on('-x', '--xxx', 'Short and long, no argument')
+parser.on('-yYYY', '--yyy', 'Short and long, required argument')
+parser.on('-z [ZZZ]', '--zzz', 'Short and long, optional argument')
+options = {yyy: 'AAA', zzz: 'BBB'}
+parser.parse!(into: options)
+p options
diff --git a/doc/ruby/missing_options.rb b/doc/ruby/missing_options.rb
new file mode 100644
index 0000000000..9428463cfd
--- /dev/null
+++ b/doc/ruby/missing_options.rb
@@ -0,0 +1,12 @@
+require 'optparse'
+parser = OptionParser.new
+parser.on('-x', '--xxx', 'Short and long, no argument')
+parser.on('-yYYY', '--yyy', 'Short and long, required argument')
+parser.on('-z [ZZZ]', '--zzz', 'Short and long, optional argument')
+options = {}
+parser.parse!(into: options)
+required_options = [:xxx, :zzz]
+missing_options = required_options - options.keys
+unless missing_options.empty?
+ fail "Missing required options: #{missing_options}"
+end
diff --git a/doc/ruby/no_abbreviation.rb b/doc/ruby/no_abbreviation.rb
new file mode 100644
index 0000000000..5464492705
--- /dev/null
+++ b/doc/ruby/no_abbreviation.rb
@@ -0,0 +1,10 @@
+require 'optparse'
+parser = OptionParser.new
+parser.on('-n', '--dry-run',) do |value|
+ p ['--dry-run', value]
+end
+parser.on('-d', '--draft',) do |value|
+ p ['--draft', value]
+end
+parser.require_exact = true
+parser.parse!