@@ -50,6 +50,43 @@ The class also has:
50
50
- {Checking for Missing Options}[#label-Checking+for+Missing+Options]
51
51
- {Default Values for Options}[#label-Default+Values+for+Options]
52
52
- {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]
53
90
54
91
=== To Begin With
55
92
@@ -422,3 +459,71 @@ Executions:
422
459
You can also define custom converters.
423
460
See {Argument Converters}[./argument_converters_rdoc.html]
424
461
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