File: getopts.test.en

package info (click to toggle)
liboptparse-ruby 0.8.4-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 428 kB
  • ctags: 644
  • sloc: ruby: 1,819; makefile: 47
file content (135 lines) | stat: -rw-r--r-- 4,481 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# -*- rd -*-
=begin
= sample for optparse
This script is comes from "sample/getopts.rb" contained in Ruby
standard distribution and modified in order to show usage of
((<optparse>)).
=end
=begin undocumented
== Global constant
: Version
  It's not mandatory, however, (({--version})) option gets available if defined.
=end
=begin
== Default values
Defining options' default values.

Since scope of local variables appear in a block is restricted within the block,
they must be initialized before.

Otherwise, global or instance variables don't require initialization
cause they aren't restricted by block, however, I suggest
initialization even in such case.
((- Also recommended as "initialize even if it's just nil" in the Ruby book. -))
=end
=begin
== Script name
Trimming script name to just base name.
=end
=begin
== Options block
If given a block, (({ARGV.options})) passes the OptionParser
instance(creating if needed). It's return value is the value returned
from the block itself.

Also, in the block, (({OptionParser::ParseError}))'s would be
(({rescue}))d and only error message would be printed to (({STDERR})).
In this case, (({nil})) is returned.
=end
=begin
=== Banner
The banner message on the top of help message. It's used as-is.
=end
=begin
=== No argument option
--- -d --debug debug message
    String starts with (({-})) means short, one with (({--})) means long style option switch,
    or mean argument specifications with (({=})).
    Any other strings are descriptions shown in the help message.
    There is no features folding long lines, however, since multiple
    description are available, cut too long lines.
    
    In this case, (({true})) is assigned to ((|debug|)).
    
    As block parameter, since it's required just assignable, not only
    local variable, global or outer variable, instance variable or
    even accessor are available.
=end
=begin
=== Required argument option & argument conversion
--- --x
--- --y
    When switch string contains '=', the option requires an argument.
    Also, a (({Class})) given, the argument would be passed to the
    block with converting to desired object((({Integer})) in the
    case).
    If no argument present or it doesn't match the class, an exception
    will be caused.
    
    Convertible(as default) classes are:
    * Object	  arbitrary string(default class)
    * String	  non-empty string
    * Integer	  integral number
    * Float	  floating point number
    * Numeric	  Integer or Float
    * TrueClass	  boolean((({true})) if present)
    * FalseClass  boolean((({false})) if present)
=end
=begin
=== Optional argument option
--- -f
    When argument spec is surrounded with (({[]})), the argument is
    optional, that is, the option may take an argument or not.
=end
=begin
=== Argument conversion
--- --geometry
    When Regexp(object responds to (({match}))) passed to (({on})),
    it is assigned as the pattern the argument must match to it.
    
    The argument string to the option is passed to (({match})), and
    the result (({nil})) or (({false})) causes invalid argument
    exception.
    
    The result of (({match})) passed to the block with converted to array.
    Typically, using Regexp, you want to refer substrings within the block,
    note that they're passed as strings and shoud be converted.
=end
=begin alternative
    You can reparse the separated strings by the OptionParser object.
      q.on('--geometry=GEOM', 'geometry XxY', /(\d+)x(\d+)/) {|geom, x, y|
        q.parse('--x', x, '--y', y)
      }
    or
      q.on('--geometry=GEOM', 'geometry XxY', /(\d+)x(\d+)/) {|geom, x, y|
        ARGV[0, 0] = ['--x', x, '--y', y]
      }
    This provides the way to make an option equivalent to some options.
=end
=begin
== Parse
Here, command line arguments, ARGV, are parsed.
=end
=begin
== Check for options
Checking whether minimal options specified, and since the expression
is the last one of the block, this results ARGV.options.

Otherwise, you may want to abort with message when number of
none-option argument is not correct, use
  ARGV.empty?
or
  ARGV.size == 2

((-You can check illegal combination of options in each handlers.-))
=end
=begin
Aborting here with help message, if any errors occurred while parsing or mandatory option is missing.

Note that the script does ((*NOT*)) abort when (({parse!}))d within ARGV.options block,
just error message would be displayed.
=end
=begin
== Miscellaneous
Showing the result of parsing.
=end