Skip to content

Commit d07cb96

Browse files
Rdoc for help (#21)
1 parent 385dd43 commit d07cb96

File tree

5 files changed

+165
-0
lines changed

5 files changed

+165
-0
lines changed

doc/optparse/ruby/help.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'optparse'
2+
parser = OptionParser.new
3+
parser.on(
4+
'-x', '--xxx',
5+
'Adipiscing elit. Aenean commodo ligula eget.',
6+
'Aenean massa. Cum sociis natoque penatibus',
7+
)
8+
parser.on(
9+
'-y', '--yyy YYY',
10+
'Lorem ipsum dolor sit amet, consectetuer.'
11+
)
12+
parser.on(
13+
'-z', '--zzz [ZZZ]',
14+
'Et magnis dis parturient montes, nascetur',
15+
'ridiculus mus. Donec quam felis, ultricies',
16+
'nec, pellentesque eu, pretium quis, sem.',
17+
)
18+
parser.parse!
19+
20+
21+

doc/optparse/ruby/help_banner.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'optparse'
2+
parser = OptionParser.new
3+
parser.banner = "Usage: ruby help_banner.rb"
4+
parser.parse!
5+
6+
7+

doc/optparse/ruby/help_format.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require 'optparse'
2+
parser = OptionParser.new(
3+
'ruby help_format.rb [options]', # Banner
4+
20, # Width of options field
5+
' ' * 2 # Indentation
6+
)
7+
parser.on(
8+
'-x', '--xxx',
9+
'Adipiscing elit. Aenean commodo ligula eget.',
10+
'Aenean massa. Cum sociis natoque penatibus',
11+
)
12+
parser.on(
13+
'-y', '--yyy YYY',
14+
'Lorem ipsum dolor sit amet, consectetuer.'
15+
)
16+
parser.on(
17+
'-z', '--zzz [ZZZ]',
18+
'Et magnis dis parturient montes, nascetur',
19+
'ridiculus mus. Donec quam felis, ultricies',
20+
'nec, pellentesque eu, pretium quis, sem.',
21+
)
22+
parser.parse!
23+
24+
25+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'optparse'
2+
parser = OptionParser.new
3+
parser.program_name = 'help_program_name.rb'
4+
parser.parse!
5+
6+
7+

doc/optparse/tutorial.rdoc

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,43 @@ The class also has:
5050
- {Checking for Missing Options}[#label-Checking+for+Missing+Options]
5151
- {Default Values for Options}[#label-Default+Values+for+Options]
5252
- {Argument Converters}[#label-Argument+Converters]
53+
- {Help}[#label-Help]
54+
55+
=== To Begin With
56+
57+
To use \OptionParser:
58+
59+
1. Require the \OptionParser code.
60+
2. Create an \OptionParser object.
61+
3. Define one or more options.
62+
4. Parse the command line.
63+
64+
File +basic.rb+ defines three options, <tt>-x</tt>,
65+
<tt>-y</tt>, and <tt>-z</tt>, each with a descriptive string,
66+
and each with a block.
67+
68+
:include: ruby/basic.rb
69+
70+
From these defined options, the parser automatically builds help text:
71+
72+
$ ruby basic.rb --help
73+
Usage: basic [options]
74+
-x Whether to X
75+
-y Whether to Y
76+
-z Whether to Z
77+
78+
When an option is found during parsing,
79+
the block defined for the option is called with the argument value.
80+
81+
Executions:
82+
83+
$ ruby basic.rb -x -z
84+
["x", true]
85+
["z", true]
86+
$ ruby basic.rb -z -y -x
87+
["z", true]
88+
["y", true]
89+
["x", true]
5390

5491
=== To Begin With
5592

@@ -422,3 +459,71 @@ Executions:
422459
You can also define custom converters.
423460
See {Argument Converters}[./argument_converters_rdoc.html]
424461
for both built-in and custom converters.
462+
463+
=== Help
464+
465+
\OptionParser makes automatically generated help text available.
466+
467+
The help text consists of:
468+
469+
- A banner, showing the usage.
470+
- Option short and long names.
471+
- Option dummy argument names.
472+
- Option descriptions.
473+
474+
Example code:
475+
476+
:include: ruby/help.rb
477+
478+
The option names and dummy argument names are defined as described above.
479+
480+
The option description consists of the strings that are not themselves option names;
481+
An option can have more than one description string.
482+
Execution:
483+
484+
Usage: help [options]
485+
-x, --xxx Adipiscing elit. Aenean commodo ligula eget.
486+
Aenean massa. Cum sociis natoque penatibus
487+
-y, --yyy YYY Lorem ipsum dolor sit amet, consectetuer.
488+
-z, --zzz [ZZZ] Et magnis dis parturient montes, nascetur
489+
ridiculus mus. Donec quam felis, ultricies
490+
nec, pellentesque eu, pretium quis, sem.
491+
492+
The program name is included in the default banner:
493+
<tt>Usage: #{program_name} [options]</tt>;
494+
you can change the program name.
495+
496+
:include: ruby/help_program_name.rb
497+
498+
Execution:
499+
500+
$ ruby help_program_name.rb --help
501+
Usage: help_program_name.rb [options]
502+
503+
You can also change the entire banner.
504+
505+
:include: ruby/help_banner.rb
506+
507+
Execution:
508+
509+
$ ruby help_banner.rb --help
510+
Usage: ruby help_banner.rb
511+
512+
By default, the option names are indented 4 spaces
513+
and the width of the option-names field is 32 spaces.
514+
515+
You can change these values, along with the banner,
516+
by passing parameters to OptionParser.new.
517+
518+
:include: ruby/help_format.rb
519+
520+
Execution:
521+
522+
$ ruby help_format.rb --help
523+
ruby help_format.rb [options]
524+
-x, --xxx Adipiscing elit. Aenean commodo ligula eget.
525+
Aenean massa. Cum sociis natoque penatibus
526+
-y, --yyy YYY Lorem ipsum dolor sit amet, consectetuer.
527+
-z, --zzz [ZZZ] Et magnis dis parturient montes, nascetur
528+
ridiculus mus. Donec quam felis, ultricies
529+
nec, pellentesque eu, pretium quis, sem.

0 commit comments

Comments
 (0)