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
20 switch 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
33 found.
34
35 The "getopt()" function is similar, but its argument is a string
36 containing all switches that take an argument. If no argument is
37 provided for a switch, say, "y", the corresponding $opt_y will be set
38 to an undefined value. Unspecified switches are silently accepted.
39 Use of "getopt()" is not recommended.
40
41 Note that, if your code is running under the recommended "use strict
42 vars" pragma, you will need to declare these package variables with
43 "our":
44
45 our($opt_x, $opt_y);
46
47 For those of you who don't like additional global variables being
48 created, "getopt()" and "getopts()" will also accept a hash reference
49 as an optional second argument. Hash keys will be "x" (where "x" is
50 the switch name) with key values the value of the argument or 1 if no
51 argument is specified.
52
53 To allow programs to process arguments that look like switches, but
54 aren't, both functions will stop processing switches when they see the
55 argument "--". The "--" will be removed from @ARGV.
56
58 If "-" is not a recognized switch letter, getopts() supports arguments
59 "--help" and "--version". If "main::HELP_MESSAGE()" and/or
60 "main::VERSION_MESSAGE()" are defined, they are called; the arguments
61 are the output file handle, the name of option-processing package, its
62 version, and the switches string. If the subroutines are not defined,
63 an attempt is made to generate intelligent messages; for best results,
64 define $main::VERSION.
65
66 If embedded documentation (in pod format, see perlpod) is detected in
67 the script, "--help" will also show how to access the documentation.
68
69 Note that due to excessive paranoia, if
70 $Getopt::Std::STANDARD_HELP_VERSION isn't true (the default is false),
71 then the messages are printed on STDERR, and the processing continues
72 after the messages are printed. This being the opposite of the
73 standard-conforming behaviour, it is strongly recommended to set
74 $Getopt::Std::STANDARD_HELP_VERSION to true.
75
76 One can change the output file handle of the messages by setting
77 $Getopt::Std::OUTPUT_HELP_VERSION. One can print the messages of
78 "--help" (without the "Usage:" line) and "--version" by calling
79 functions help_mess() and version_mess() with the switches string as an
80 argument.
81
82
83
84perl v5.26.3 2018-03-23 Getopt::Std(3pm)