7 getopt, getopts - Process single-character switches with switch clustering
13 getopts('oif:'); # -o & -i are boolean flags, -f takes an argument
14 # Sets $opt_* as a side effect.
15 getopts('oif:', \%opts); # options as above. Values in %opts
16 getopt('oDI'); # -o, -D & -I take arg.
17 # Sets $opt_* as a side effect.
18 getopt('oDI', \%opts); # -o, -D & -I take arg. Values in %opts
22 The C<getopts()> function processes single-character switches with switch
23 clustering. Pass one argument which is a string containing all switches to be
24 recognized. For each switch found, if an argument is expected and provided,
25 C<getopts()> sets C<$opt_x> (where C<x> is the switch name) to the value of
26 the argument. If an argument is expected but none is provided, C<$opt_x> is
27 set to an undefined value. If a switch does not take an argument, C<$opt_x>
30 Switches which take an argument don't care whether there is a space between
31 the switch and the argument. If unspecified switches are found on the
32 command-line, the user will be warned that an unknown option was given.
34 The C<getopts()> function returns true unless an invalid option was found.
36 The C<getopt()> function is similar, but its argument is a string containing
37 all switches that take an argument. If no argument is provided for a switch,
38 say, C<y>, the corresponding C<$opt_y> will be set to an undefined value.
39 Unspecified switches are silently accepted. Use of C<getopt()> is not
42 Note that, if your code is running under the recommended C<use strict
43 vars> pragma, you will need to declare these package variables
48 For those of you who don't like additional global variables being created,
49 C<getopt()> and C<getopts()> will also accept a hash reference as an optional
50 second argument. Hash keys will be C<x> (where C<x> is the switch name) with
51 key values the value of the argument or C<1> if no argument is specified.
53 To allow programs to process arguments that look like switches, but aren't,
54 both functions will stop processing switches when they see the argument
55 C<-->. The C<--> will be removed from @ARGV.
57 =head1 C<--help> and C<--version>
59 If C<-> is not a recognized switch letter, getopts() supports arguments
60 C<--help> and C<--version>. If C<main::HELP_MESSAGE()> and/or
61 C<main::VERSION_MESSAGE()> are defined, they are called; the arguments are
62 the output file handle, the name of option-processing package, its version,
63 and the switches string. If the subroutines are not defined, an attempt is
64 made to generate intelligent messages; for best results, define $main::VERSION.
66 If embedded documentation (in pod format, see L<perlpod>) is detected
67 in the script, C<--help> will also show how to access the documentation.
69 Note that due to excessive paranoia, if $Getopt::Std::STANDARD_HELP_VERSION
70 isn't true (the default is false), then the messages are printed on STDERR,
71 and the processing continues after the messages are printed. This being
72 the opposite of the standard-conforming behaviour, it is strongly recommended
73 to set $Getopt::Std::STANDARD_HELP_VERSION to true.
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 C<--help>
77 (without the C<Usage:> line) and C<--version> by calling functions help_mess()
78 and version_mess() with the switches string as an argument.
83 @EXPORT = qw(getopt getopts);
85 # uncomment the next line to disable 1.03-backward compatibility paranoia
86 # $STANDARD_HELP_VERSION = 1;
88 # Process single-character switches with switch clustering. Pass one argument
89 # which is a string containing all switches that take an argument. For each
90 # switch found, sets $opt_x (where x is the switch name) to the value of the
91 # argument, or 1 if no argument. Switches which take an argument don't care
92 # whether there is a space between the switch and the argument.
95 # getopt('oDI'); # -o, -D & -I take arg. Sets opt_* as a side effect.
98 my ($argumentative, $hash) = @_;
99 $argumentative = '' if !defined $argumentative;
104 while (@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
105 ($first,$rest) = ($1,$2);
106 if (/^--$/) { # early exit if --
110 if (index($argumentative,$first) >= 0) {
116 $rest = shift(@ARGV);
119 $$hash{$first} = $rest;
122 ${"opt_$first"} = $rest;
123 push( @EXPORT, "\$opt_$first" );
132 push( @EXPORT, "\$opt_$first" );
143 local $Exporter::ExportLevel = 1;
149 return $OUTPUT_HELP_VERSION if defined $OUTPUT_HELP_VERSION;
150 return \*STDOUT if $STANDARD_HELP_VERSION;
155 exit 0 if $STANDARD_HELP_VERSION;
157 print {output_h()} <<EOM;
158 [Now continuing due to backward compatibility and excessive paranoia.
159 See 'perldoc $p' about \$$p\::STANDARD_HELP_VERSION.]
163 sub version_mess ($;$) {
166 if (@_ and defined &main::VERSION_MESSAGE) {
167 main::VERSION_MESSAGE($h, __PACKAGE__, $VERSION, $args);
169 my $v = $main::VERSION;
170 $v = '[unknown]' unless defined $v;
172 $myv .= ' [paranoid]' unless $STANDARD_HELP_VERSION;
174 $perlv = sprintf "%vd", $^V if $] >= 5.006;
176 $0 version $v calling Getopt::Std::getopts (version $myv),
177 running under Perl version $perlv.
182 sub help_mess ($;$) {
185 if (@_ and defined &main::HELP_MESSAGE) {
186 main::HELP_MESSAGE($h, __PACKAGE__, $VERSION, $args);
188 my (@witharg) = ($args =~ /(\S)\s*:/g);
189 my (@rest) = ($args =~ /([^\s:])(?!\s*:)/g);
190 my ($help, $arg) = ('', '');
192 $help .= "\n\tWith arguments: -" . join " -", @witharg;
193 $arg = "\nSpace is not required between options and their arguments.";
196 $help .= "\n\tBoolean (without arguments): -" . join " -", @rest;
198 my ($scr) = ($0 =~ m,([^/\\]+)$,);
199 print $h <<EOH if @_; # Let the script override this
201 Usage: $scr [-OPTIONS [-MORE_OPTIONS]] [--] [PROGRAM_ARG1 ...]
205 The following single-character options are accepted:$help
207 Options may be merged together. -- stops processing of options.$arg
210 if ( defined $0 and $0 ne '-e' and -f $0 and -r $0
211 and open my $script, '<', $0 ) {
213 $has_pod = 1, last if /^=(pod|head1)/;
216 print $h <<EOH if $has_pod;
225 # getopts('a:bc'); # -a takes arg. -b & -c not. Sets opt_* as a
229 my ($argumentative, $hash) = @_;
230 my (@args,$first,$rest,$exit);
235 @args = split( / */, $argumentative );
236 while(@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/s) {
237 ($first,$rest) = ($1,$2);
238 if (/^--$/) { # early exit if --
242 my $pos = index($argumentative,$first);
244 if (defined($args[$pos+1]) and ($args[$pos+1] eq ':')) {
247 ++$errs unless @ARGV;
248 $rest = shift(@ARGV);
251 $$hash{$first} = $rest;
254 ${"opt_$first"} = $rest;
255 push( @EXPORT, "\$opt_$first" );
264 push( @EXPORT, "\$opt_$first" );
275 if ($first eq '-' and $rest eq 'help') {
276 version_mess($argumentative, 'main');
277 help_mess($argumentative, 'main');
281 } elsif ($first eq '-' and $rest eq 'version') {
282 version_mess($argumentative, 'main');
287 warn "Unknown option: $first\n";
298 local $Exporter::ExportLevel = 1;