lib/optparse.rb: improve docs

* lib/optparse.rb: [DOC] simplify shell prompt in examples;
  other minor improvements.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63445 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
stomar 2018-05-16 18:55:28 +00:00
parent a667fe79ea
commit db4cba88d9

View File

@ -125,6 +125,7 @@
# For options that require an argument, option specification strings may include an # For options that require an argument, option specification strings may include an
# option name in all caps. If an option is used without the required argument, # option name in all caps. If an option is used without the required argument,
# an exception will be raised. # an exception will be raised.
#
# require 'optparse' # require 'optparse'
# #
# options = {} # options = {}
@ -137,9 +138,9 @@
# #
# Used: # Used:
# #
# bash-3.2$ ruby optparse-test.rb -r # $ ruby optparse-test.rb -r
# optparse-test.rb:9:in `<main>': missing argument: -r (OptionParser::MissingArgument) # optparse-test.rb:9:in `<main>': missing argument: -r (OptionParser::MissingArgument)
# bash-3.2$ ruby optparse-test.rb -r my-library # $ ruby optparse-test.rb -r my-library
# You required my-library! # You required my-library!
# #
# === Type Coercion # === Type Coercion
@ -187,13 +188,12 @@
# end.parse! # end.parse!
# #
# Used: # Used:
# bash-3.2$ ruby optparse-test.rb -t nonsense #
# $ ruby optparse-test.rb -t nonsense
# ... invalid argument: -t nonsense (OptionParser::InvalidArgument) # ... invalid argument: -t nonsense (OptionParser::InvalidArgument)
# from ... time.rb:5:in `block in <top (required)>' # $ ruby optparse-test.rb -t 10-11-12
# from optparse-test.rb:31:in `<main>'
# bash-3.2$ ruby optparse-test.rb -t 10-11-12
# 2010-11-12 00:00:00 -0500 # 2010-11-12 00:00:00 -0500
# bash-3.2$ ruby optparse-test.rb -t 9:30 # $ ruby optparse-test.rb -t 9:30
# 2014-08-13 09:30:00 -0400 # 2014-08-13 09:30:00 -0400
# #
# ==== Creating Custom Conversions # ==== Creating Custom Conversions
@ -225,12 +225,13 @@
# #
# op.parse! # op.parse!
# #
# output: # Used:
# bash-3.2$ ruby optparse-test.rb --user 1 #
# $ ruby optparse-test.rb --user 1
# #<struct User id=1, name="Sam"> # #<struct User id=1, name="Sam">
# bash-3.2$ ruby optparse-test.rb --user 2 # $ ruby optparse-test.rb --user 2
# #<struct User id=2, name="Gandalf"> # #<struct User id=2, name="Gandalf">
# bash-3.2$ ruby optparse-test.rb --user 3 # $ ruby optparse-test.rb --user 3
# optparse-test.rb:15:in `block in find_user': No User Found for id 3 (RuntimeError) # optparse-test.rb:15:in `block in find_user': No User Found for id 3 (RuntimeError)
# #
# === Store options to a Hash # === Store options to a Hash
@ -245,12 +246,14 @@
# opts.on('-b NUM', Integer) # opts.on('-b NUM', Integer)
# opts.on('-v', '--verbose') # opts.on('-v', '--verbose')
# end.parse!(into: params) # end.parse!(into: params)
#
# p params # p params
# #
# output: # Used:
# bash-3.2$ ruby optparse-test.rb -a #
# $ ruby optparse-test.rb -a
# {:a=>true} # {:a=>true}
# bash-3.2$ ruby optparse-test.rb -a -v # $ ruby optparse-test.rb -a -v
# {:a=>true, :verbose=>true} # {:a=>true, :verbose=>true}
# $ ruby optparse-test.rb -a -b 100 # $ ruby optparse-test.rb -a -b 100
# {:a=>true, :b=>100} # {:a=>true, :b=>100}