Skip to content

Commit 92c9d05

Browse files
BurdetteLamarXrXr
authored andcommittedApr 14, 2021
[ruby/optparse] Rdoc (#15)
* Resolve shared mixed_names.rb * Add long option with negation * Show --help for all examples * Table of contents for tutorial * Move argument converters to separate rdoc * Add references to argument_converters.rdoc * Tune up argument converters * Change explicit links to auto-links ruby/optparse@c91ed8d33d
1 parent b1a8d31 commit 92c9d05

10 files changed

+487
-365
lines changed
 

‎doc/argument_converters.rdoc

+362
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,362 @@
1+
== Argument Converters
2+
3+
An option can specify that its argument is to be converted
4+
from the default \String to an instance of another class.
5+
6+
=== Contents
7+
8+
- {Built-In Argument Converters}[#label-Built-In+Argument+Converters]
9+
- {Date}[#label-Date]
10+
- {DateTime}[#label-DateTime]
11+
- {Time}[#label-Time]
12+
- {URI}[#label-URI]
13+
- {Shellwords}[#label-Shellwords]
14+
- {Integer}[#label-Integer]
15+
- {Float}[#label-Float]
16+
- {Numeric}[#label-Numeric]
17+
- {DecimalInteger}[#label-DecimalInteger]
18+
- {OctalInteger}[#label-OctalInteger]
19+
- {DecimalNumeric}[#label-DecimalNumeric]
20+
- {TrueClass}[#label-TrueClass]
21+
- {FalseClass}[#label-FalseClass]
22+
- {Object}[#label-Object]
23+
- {String}[#label-String]
24+
- {Array}[#label-Array]
25+
- {Regexp}[#label-Regexp]
26+
- {Custom Argument Converters}[#label-Custom+Argument+Converters]
27+
28+
=== Built-In Argument Converters
29+
30+
\OptionParser has a number of built-in argument converters,
31+
which are demonstrated below.
32+
33+
==== \Date
34+
35+
File +date.rb+
36+
defines an option whose argument is to be converted to a \Date object.
37+
The argument is converted by method Date#parse.
38+
39+
:include: ruby/date.rb
40+
41+
Executions:
42+
43+
$ ruby date.rb --date 2001-02-03
44+
[#<Date: 2001-02-03 ((2451944j,0s,0n),+0s,2299161j)>, Date]
45+
$ ruby date.rb --date 20010203
46+
[#<Date: 2001-02-03 ((2451944j,0s,0n),+0s,2299161j)>, Date]
47+
$ ruby date.rb --date "3rd Feb 2001"
48+
[#<Date: 2001-02-03 ((2451944j,0s,0n),+0s,2299161j)>, Date]
49+
50+
==== \DateTime
51+
52+
File +datetime.rb+
53+
defines an option whose argument is to be converted to a \DateTime object.
54+
The argument is converted by method DateTime#parse.
55+
56+
:include: ruby/datetime.rb
57+
58+
Executions:
59+
60+
$ ruby datetime.rb --datetime 2001-02-03T04:05:06+07:00
61+
[#<DateTime: 2001-02-03T04:05:06+07:00 ((2451943j,75906s,0n),+25200s,2299161j)>, DateTime]
62+
$ ruby datetime.rb --datetime 20010203T040506+0700
63+
[#<DateTime: 2001-02-03T04:05:06+07:00 ((2451943j,75906s,0n),+25200s,2299161j)>, DateTime]
64+
$ ruby datetime.rb --datetime "3rd Feb 2001 04:05:06 PM"
65+
[#<DateTime: 2001-02-03T16:05:06+00:00 ((2451944j,57906s,0n),+0s,2299161j)>, DateTime]
66+
67+
==== \Time
68+
69+
File +time.rb+
70+
defines an option whose argument is to be converted to a \Time object.
71+
The argument is converted by method Time#httpdate or Time#parse.
72+
73+
:include: ruby/time.rb
74+
75+
Executions:
76+
77+
$ ruby time.rb --time "Thu, 06 Oct 2011 02:26:12 GMT"
78+
[2011-10-06 02:26:12 UTC, Time]
79+
$ ruby time.rb --time 2010-10-31
80+
[2010-10-31 00:00:00 -0500, Time]
81+
82+
==== \URI
83+
84+
File +uri.rb+
85+
defines an option whose argument is to be converted to a \URI object.
86+
The argument is converted by method URI#parse.
87+
88+
:include: ruby/uri.rb
89+
90+
Executions:
91+
92+
$ ruby uri.rb --uri https://github.com
93+
[#<URI::HTTPS https://github.com>, URI::HTTPS]
94+
$ ruby uri.rb --uri http://github.com
95+
[#<URI::HTTP http://github.com>, URI::HTTP]
96+
$ ruby uri.rb --uri file://~/var
97+
[#<URI::File file://~/var>, URI::File]
98+
99+
==== \Shellwords
100+
101+
File +shellwords.rb+
102+
defines an option whose argument is to be converted to an \Array object by method
103+
Shellwords#shellwords.
104+
105+
:include: ruby/shellwords.rb
106+
107+
Executions:
108+
109+
$ ruby shellwords.rb --shellwords "ruby my_prog.rb | less"
110+
[["ruby", "my_prog.rb", "|", "less"], Array]
111+
$ ruby shellwords.rb --shellwords "here are 'two words'"
112+
[["here", "are", "two words"], Array]
113+
114+
==== \Integer
115+
116+
File +integer.rb+
117+
defines an option whose argument is to be converted to an \Integer object.
118+
The argument is converted by method Kernel#Integer.
119+
120+
:include: ruby/integer.rb
121+
122+
Executions:
123+
124+
$ ruby integer.rb --integer 100
125+
[100, Integer]
126+
$ ruby integer.rb --integer -100
127+
[-100, Integer]
128+
$ ruby integer.rb --integer 0100
129+
[64, Integer]
130+
$ ruby integer.rb --integer 0x100
131+
[256, Integer]
132+
$ ruby integer.rb --integer 0b100
133+
[4, Integer]
134+
135+
==== \Float
136+
137+
File +float.rb+
138+
defines an option whose argument is to be converted to a \Float object.
139+
The argument is converted by method Kernel#Float.
140+
141+
:include: ruby/float.rb
142+
143+
Executions:
144+
145+
$ ruby float.rb --float 1
146+
[1.0, Float]
147+
$ ruby float.rb --float 3.14159
148+
[3.14159, Float]
149+
$ ruby float.rb --float 1.234E2
150+
[123.4, Float]
151+
$ ruby float.rb --float 1.234E-2
152+
[0.01234, Float]
153+
154+
==== \Numeric
155+
156+
File +numeric.rb+
157+
defines an option whose argument is to be converted to an instance
158+
of \Rational, \Float, or \Integer.
159+
The argument is converted by method Kernel#Rational,
160+
Kernel#Float, or Kernel#Integer.
161+
162+
:include: ruby/numeric.rb
163+
164+
Executions:
165+
166+
$ ruby numeric.rb --numeric 1/3
167+
[(1/3), Rational]
168+
$ ruby numeric.rb --numeric 3.333E-1
169+
[0.3333, Float]
170+
$ ruby numeric.rb --numeric 3
171+
[3, Integer]
172+
173+
==== \DecimalInteger
174+
175+
File +decimal_integer.rb+
176+
defines an option whose argument is to be converted to an \Integer object.
177+
The argument is converted by method Kernel#Integer.
178+
179+
:include: ruby/decimal_integer.rb
180+
181+
The argument may not be in a binary or hexadecimal format;
182+
a leading zero is ignored (not parsed as octal).
183+
184+
Executions:
185+
186+
$ ruby decimal_integer.rb --decimal_integer 100
187+
[100, Integer]
188+
$ ruby decimal_integer.rb --decimal_integer -100
189+
[-100, Integer]
190+
$ ruby decimal_integer.rb --decimal_integer 0100
191+
[100, Integer]
192+
$ ruby decimal_integer.rb --decimal_integer -0100
193+
[-100, Integer]
194+
195+
==== \OctalInteger
196+
197+
File +octal_integer.rb+
198+
defines an option whose argument is to be converted to an \Integer object.
199+
The argument is converted by method Kernel#Integer.
200+
201+
:include: ruby/octal_integer.rb
202+
203+
The argument may not be in a binary or hexadecimal format;
204+
it is parsed as octal, regardless of whether it has a leading zero.
205+
206+
Executions:
207+
208+
$ ruby octal_integer.rb --octal_integer 100
209+
[64, Integer]
210+
$ ruby octal_integer.rb --octal_integer -100
211+
[-64, Integer]
212+
$ ruby octal_integer.rb --octal_integer 0100
213+
[64, Integer]
214+
215+
==== \DecimalNumeric
216+
217+
File +decimal_numeric.rb+
218+
defines an option whose argument is to be converted to an \Integer object.
219+
The argument is converted by method {Kernel#Integer
220+
221+
:include: ruby/decimal_numeric.rb
222+
223+
The argument may not be in a binary or hexadecimal format;
224+
a leading zero causes the argument to be parsed as octal.
225+
226+
Executions:
227+
228+
$ ruby decimal_numeric.rb --decimal_numeric 100
229+
[100, Integer]
230+
$ ruby decimal_numeric.rb --decimal_numeric -100
231+
[-100, Integer]
232+
$ ruby decimal_numeric.rb --decimal_numeric 0100
233+
[64, Integer]
234+
235+
==== \TrueClass
236+
237+
File +true_class.rb+
238+
defines an option whose argument is to be converted to +true+ or +false+.
239+
The argument is evaluated by method Object#nil?.
240+
241+
:include: ruby/true_class.rb
242+
243+
The argument may be any of those shown in the examples below.
244+
245+
Executions:
246+
247+
$ ruby true_class.rb --true_class true
248+
[true, TrueClass]
249+
$ ruby true_class.rb --true_class yes
250+
[true, TrueClass]
251+
$ ruby true_class.rb --true_class +
252+
[true, TrueClass]
253+
$ ruby true_class.rb --true_class false
254+
[false, FalseClass]
255+
$ ruby true_class.rb --true_class no
256+
[false, FalseClass]
257+
$ ruby true_class.rb --true_class -
258+
[false, FalseClass]
259+
$ ruby true_class.rb --true_class nil
260+
[false, FalseClass]
261+
262+
==== \FalseClass
263+
264+
File +false_class.rb+
265+
defines an option whose argument is to be converted to +true+ or +false+.
266+
The argument is evaluated by method Object#nil?.
267+
268+
:include: ruby/false_class.rb
269+
270+
The argument may be any of those shown in the examples below.
271+
272+
Executions:
273+
274+
$ ruby false_class.rb --false_class false
275+
[false, FalseClass]
276+
$ ruby false_class.rb --false_class no
277+
[false, FalseClass]
278+
$ ruby false_class.rb --false_class -
279+
[false, FalseClass]
280+
$ ruby false_class.rb --false_class nil
281+
[false, FalseClass]
282+
$ ruby false_class.rb --false_class true
283+
[true, TrueClass]
284+
$ ruby false_class.rb --false_class yes
285+
[true, TrueClass]
286+
$ ruby false_class.rb --false_class +
287+
[true, TrueClass]
288+
289+
==== \Object
290+
291+
File +object.rb+
292+
defines an option whose argument is not to be converted from \String.
293+
294+
:include: ruby/object.rb
295+
296+
Executions:
297+
298+
$ ruby object.rb --object foo
299+
["foo", String]
300+
$ ruby object.rb --object nil
301+
["nil", String]
302+
303+
==== \String
304+
305+
File +string.rb+
306+
defines an option whose argument is not to be converted from \String.
307+
308+
:include: ruby/string.rb
309+
310+
Executions:
311+
312+
$ ruby string.rb --string foo
313+
["foo", String]
314+
$ ruby string.rb --string nil
315+
["nil", String]
316+
317+
==== \Array
318+
319+
File +array.rb+
320+
defines an option whose argument is to be converted from \String
321+
to an array of strings, based on comma-separated substrings.
322+
323+
:include: ruby/array.rb
324+
325+
Executions:
326+
327+
$ ruby array.rb --array ""
328+
[[], Array]
329+
$ ruby array.rb --array foo,bar,baz
330+
[["foo", "bar", "baz"], Array]
331+
$ ruby array.rb --array "foo, bar, baz"
332+
[["foo", " bar", " baz"], Array]
333+
334+
==== \Regexp
335+
336+
File +regexp.rb+
337+
defines an option whose argument is to be converted to a \Regexp object.
338+
339+
:include: ruby/regexp.rb
340+
341+
Executions:
342+
343+
$ ruby regexp.rb --regexp foo
344+
345+
=== Custom Argument Converters
346+
347+
You can create custom argument converters.
348+
To create a custom converter, call OptionParser#accept with a class argument,
349+
along with a block that converts the argument and returns the converted value.
350+
351+
:include: ruby/custom_converter.rb
352+
353+
Executions:
354+
355+
$ ruby custom_converter.rb --complex 0
356+
[(0+0i), Complex]
357+
$ ruby custom_converter.rb --complex 1
358+
[(1+0i), Complex]
359+
$ ruby custom_converter.rb --complex 1+2i
360+
[(1+2i), Complex]
361+
$ ruby custom_converter.rb --complex 0.3-0.5i
362+
[(0.3-0.5i), Complex]

0 commit comments

Comments
 (0)
Failed to load comments.