summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorzzak <zzak@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-02-27 09:01:43 +0000
committerzzak <zzak@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-02-27 09:01:43 +0000
commita9625a2f8aef6c43b6564c79088f7776d77029b5 (patch)
tree07bc16e1cf5764392cda014deaa19b64aa617e48 /lib
parent8f1d721810b6f335be808e8097eea587efcd9dd0 (diff)
* lib/optparse.rb: [DOC] Add example of generating help with optparse.
Patch by @joelmccracken documenting-ruby/ruby#19 https://github.com/documenting-ruby/ruby/pull/19 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45195 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r--lib/optparse.rb37
1 files changed, 37 insertions, 0 deletions
diff --git a/lib/optparse.rb b/lib/optparse.rb
index 6c4560f749..27490204df 100644
--- a/lib/optparse.rb
+++ b/lib/optparse.rb
@@ -82,6 +82,43 @@
# p options
# p ARGV
#
+# === Generating Help
+#
+# OptionParser can be used to automatically generate help for the commands you
+# write:
+#
+# require 'optparse'
+#
+# Options = Struct.new(:name)
+#
+# class Parser
+# def self.parse(options)
+# args = Options.new("world")
+#
+# opt_parser = OptionParser.new do |opts|
+# opts.banner = "Usage: example.rb [options]"
+#
+# opts.on("-nNAME", "--name=NAME", "Name to say hello to") do |n|
+# args.name = n
+# end
+#
+# opts.on("-h", "--help", "Prints this help") do
+# puts opts
+# exit
+# end
+# end
+#
+# opt_parser.parse!(options)
+# return args
+# end
+# end
+# options = Parser.parse %w[--help]
+#
+# #=>
+# # Usage: example.rb [options]
+# # -n, --name=NAME Name to say hello to
+# # -h, --help Prints this help#
+#
# === Complete example
#
# The following example is a complete Ruby program. You can run it and see the