perl5.001 patch.1f
[p5sagit/p5-mst-13.2.git] / pod / modpods / Getopt.pod
CommitLineData
a0d0e21e 1=head1 NAME
2
3getopt - Process single-character switches with switch clustering
4
5getopts - Process single-character switches with switch clustering
6
7GetOptions - extended getopt processing
8
9=head1 SYNOPSIS
10
11 use Getopt::Std;
12 getopt('oDI'); # -o, -D & -I take arg. Sets opt_* as a side effect.
748a9306 13 getopts('oif:'); # -o & -i are boolean flags, -f takes an argument
14 # Sets opt_* as a side effect.
a0d0e21e 15
16 use Getopt::Long;
17 $result = GetOptions (...option-descriptions...);
18
19=head1 DESCRIPTION
20
21The getopt() functions processes single-character switches with switch
22clustering. Pass one argument which is a string containing all switches
23that take an argument. For each switch found, sets $opt_x (where x is the
24switch name) to the value of the argument, or 1 if no argument. Switches
25which take an argument don't care whether there is a space between the
26switch and the argument.
27
28The Getopt::Long module implements an extended getopt function called
29GetOptions(). This function adheres to the new syntax (long option names,
30no bundling). It tries to implement the better functionality of
31traditional, GNU and POSIX getopt() functions.
32
33Each description should designate a valid Perl identifier, optionally
34followed by an argument specifier.
35
36Values for argument specifiers are:
37
38 <none> option does not take an argument
39 ! option does not take an argument and may be negated
40 =s :s option takes a mandatory (=) or optional (:) string argument
41 =i :i option takes a mandatory (=) or optional (:) integer argument
42 =f :f option takes a mandatory (=) or optional (:) real number argument
43
44If option "name" is set, it will cause the Perl variable $opt_name to
45be set to the specified value. The calling program can use this
46variable to detect whether the option has been set. Options that do
47not take an argument will be set to 1 (one).
48
49Options that take an optional argument will be defined, but set to ''
50if no actual argument has been supplied.
51
52If an "@" sign is appended to the argument specifier, the option is
53treated as an array. Value(s) are not set, but pushed into array
54@opt_name.
55
56Options that do not take a value may have an "!" argument specifier to
57indicate that they may be negated. E.g. "foo!" will allow B<-foo> (which
58sets $opt_foo to 1) and B<-nofoo> (which will set $opt_foo to 0).
59
60The option name may actually be a list of option names, separated by
61'|'s, e.g. B<"foo|bar|blech=s". In this example, options 'bar' and
62'blech' will set $opt_foo instead.
63
64Option names may be abbreviated to uniqueness, depending on
65configuration variable $autoabbrev.
66
67Dashes in option names are allowed (e.g. pcc-struct-return) and will
68be translated to underscores in the corresponding Perl variable (e.g.
69$opt_pcc_struct_return). Note that a lone dash "-" is considered an
70option, corresponding Perl identifier is $opt_ .
71
72A double dash "--" signals end of the options list.
73
74If the first option of the list consists of non-alphanumeric
75characters only, it is interpreted as a generic option starter.
76Everything starting with one of the characters from the starter will
77be considered an option.
78
79The default values for the option starters are "-" (traditional), "--"
80(POSIX) and "+" (GNU, being phased out).
81
82Options that start with "--" may have an argument appended, separated
83with an "=", e.g. "--foo=bar".
84
85If configuration variable $getopt_compat is set to a non-zero value,
86options that start with "+" may also include their arguments,
87e.g. "+foo=bar".
88
89A return status of 0 (false) indicates that the function detected
90one or more errors.
91
92=head1 EXAMPLES
93
94If option "one:i" (i.e. takes an optional integer argument), then
95the following situations are handled:
96
97 -one -two -> $opt_one = '', -two is next option
98 -one -2 -> $opt_one = -2
99
100Also, assume "foo=s" and "bar:s" :
101
102 -bar -xxx -> $opt_bar = '', '-xxx' is next option
103 -foo -bar -> $opt_foo = '-bar'
104 -foo -- -> $opt_foo = '--'
105
106In GNU or POSIX format, option names and values can be combined:
107
108 +foo=blech -> $opt_foo = 'blech'
109 --bar= -> $opt_bar = ''
110 --bar=-- -> $opt_bar = '--'
111
112=over 12
113
114=item $autoabbrev
115
116Allow option names to be abbreviated to uniqueness.
117Default is 1 unless environment variable
118POSIXLY_CORRECT has been set.
119
120=item $getopt_compat
121
122Allow '+' to start options.
123Default is 1 unless environment variable
124POSIXLY_CORRECT has been set.
125
126=item $option_start
127
128Regexp with option starters.
129Default is (--|-) if environment variable
130POSIXLY_CORRECT has been set, (--|-|\+) otherwise.
131
132=item $order
133
134Whether non-options are allowed to be mixed with
135options.
136Default is $REQUIRE_ORDER if environment variable
137POSIXLY_CORRECT has been set, $PERMUTE otherwise.
138
139=item $ignorecase
140
141Ignore case when matching options. Default is 1.
142
143=item $debug
144
145Enable debugging output. Default is 0.
146
147=back
148
149=head1 NOTE
150
151Does not yet use the Exporter--or even packages!!
152Thus, it's not a real module.
153