1Getopt::Std(3pm) Perl Programmers Reference Guide Getopt::Std(3pm)
2
3
4
6 Getopt::Std - Process single-character switches with switch clustering
7
9 use Getopt::Std;
10
11 getopts('oif:'); # -o & -i are boolean flags, -f takes an argument
12 # Sets $opt_* as a side effect.
13 getopts('oif:', \%opts); # options as above. Values in %opts
14 getopt('oDI'); # -o, -D & -I take arg.
15 # Sets $opt_* as a side effect.
16 getopt('oDI', \%opts); # -o, -D & -I take arg. Values in %opts
17
19 The getopts() function processes single-character switches with switch
20 clustering. Pass one argument which is a string containing all
21 switches to be recognized. For each switch found, if an argument is
22 expected and provided, getopts() sets $opt_x (where "x" is the switch
23 name) to the value of the argument. If an argument is expected but
24 none is provided, $opt_x is set to an undefined value. If a switch
25 does not take an argument, $opt_x is set to 1.
26
27 Switches which take an argument don't care whether there is a space
28 between the switch and the argument. If unspecified switches are found
29 on the command-line, the user will be warned that an unknown option was
30 given.
31
32 The getopts() function returns true unless an invalid option was found.
33
34 The getopt() function is similar, but its argument is a string
35 containing all switches that take an argument. If no argument is
36 provided for a switch, say, "y", the corresponding $opt_y will be set
37 to an undefined value. Unspecified switches are silently accepted.
38 Use of getopt() is not recommended.
39
40 Note that, if your code is running under the recommended "use strict
41 vars" pragma, you will need to declare these package variables with
42 "our":
43
44 our($opt_x, $opt_y);
45
46 For those of you who don't like additional global variables being
47 created, getopt() and getopts() will also accept a hash reference as an
48 optional second argument. Hash keys will be "x" (where "x" is the
49 switch name) with key values the value of the argument or 1 if no
50 argument is specified.
51
52 To allow programs to process arguments that look like switches, but
53 aren't, both functions will stop processing switches when they see the
54 argument "--". The "--" will be removed from @ARGV.
55
57 If "-" is not a recognized switch letter, getopts() supports arguments
58 "--help" and "--version". If main::HELP_MESSAGE() and/or
59 main::VERSION_MESSAGE() are defined, they are called; the arguments are
60 the output file handle, the name of option-processing package, its
61 version, and the switches string. If the subroutines are not defined,
62 an attempt is made to generate intelligent messages; for best results,
63 define $main::VERSION.
64
65 If embedded documentation (in pod format, see perlpod) is detected in
66 the script, "--help" will also show how to access the documentation.
67
68 Note that due to excessive paranoia, if
69 $Getopt::Std::STANDARD_HELP_VERSION isn't true (the default is false),
70 then the messages are printed on STDERR, and the processing continues
71 after the messages are printed. This being the opposite of the
72 standard-conforming behaviour, it is strongly recommended to set
73 $Getopt::Std::STANDARD_HELP_VERSION to true.
74
75 One can change the output file handle of the messages by setting
76 $Getopt::Std::OUTPUT_HELP_VERSION. One can print the messages of
77 "--help" (without the "Usage:" line) and "--version" by calling
78 functions help_mess() and version_mess() with the switches string as an
79 argument.
80
81
82
83perl v5.38.2 2023-11-30 Getopt::Std(3pm)