7 getopt - Process single-character switches with switch clustering
9 getopts - Process single-character switches with switch clustering
15 getopt('oDI'); # -o, -D & -I take arg. Sets $opt_* as a side effect.
16 getopt('oDI', \%opts); # -o, -D & -I take arg. Values in %opts
17 getopts('oif:'); # -o & -i are boolean flags, -f takes an argument
18 # Sets $opt_* as a side effect.
19 getopts('oif:', \%opts); # options as above. Values in %opts
23 The getopt() function processes single-character switches with switch
24 clustering. Pass one argument which is a string containing all switches
25 that take an argument. For each switch found, sets $opt_x (where x is the
26 switch name) to the value of the argument if an argument is expected,
27 or 1 otherwise. Switches which take an argument don't care whether
28 there is a space between the switch and the argument.
30 The getopts() function is similar, but you should pass to it the list of all
31 switches to be recognized. If unspecified switches are found on the
32 command-line, the user will be warned that an unknown option was given.
34 Note that, if your code is running under the recommended C<use strict
35 'vars'> pragma, you will need to declare these package variables
40 For those of you who don't like additional global variables being created, getopt()
41 and getopts() will also accept a hash reference as an optional second argument.
42 Hash keys will be x (where x is the switch name) with key values the value of
43 the argument or 1 if no argument is specified.
45 To allow programs to process arguments that look like switches, but aren't,
46 both functions will stop processing switches when they see the argument
47 C<-->. The C<--> will be removed from @ARGV.
49 =head1 C<--help> and C<--version>
51 If C<-> is not a recognized switch letter, getopts() supports arguments
52 C<--help> and C<--version>. If C<main::HELP_MESSAGE()> and/or
53 C<main::VERSION_MESSAGE()> are defined, they are called; the arguments are
54 the output file handle, the name of option-processing package, its version,
55 and the switches string. If the subroutines are not defined, an attempt is
56 made to generate intelligent messages; for best results, define $main::VERSION.
58 If embedded documentation (in pod format, see L<perlpod>) is detected
59 in the script, C<--help> will also show how to access the documentation.
61 Note that due to excessive paranoia, if $Getopt::Std::STANDARD_HELP_VERSION
62 isn't true (the default is false), then the messages are printed on STDERR,
63 and the processing continues after the messages are printed. This being
64 the opposite of the standard-conforming behaviour, it is strongly recommended
65 to set $Getopt::Std::STANDARD_HELP_VERSION to true.
67 One can change the output file handle of the messages by setting
68 $Getopt::Std::OUTPUT_HELP_VERSION. One can print the messages of C<--help>
69 (without the C<Usage:> line) and C<--version> by calling functions help_mess()
70 and version_mess() with the switches string as an argument.
75 @EXPORT = qw(getopt getopts);
77 # uncomment the next line to disable 1.03-backward compatibility paranoia
78 # $STANDARD_HELP_VERSION = 1;
80 # Process single-character switches with switch clustering. Pass one argument
81 # which is a string containing all switches that take an argument. For each
82 # switch found, sets $opt_x (where x is the switch name) to the value of the
83 # argument, or 1 if no argument. Switches which take an argument don't care
84 # whether there is a space between the switch and the argument.
87 # getopt('oDI'); # -o, -D & -I take arg. Sets opt_* as a side effect.
90 my ($argumentative, $hash) = @_;
91 $argumentative = '' if !defined $argumentative;
96 while (@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
97 ($first,$rest) = ($1,$2);
98 if (/^--$/) { # early exit if --
102 if (index($argumentative,$first) >= 0) {
108 $rest = shift(@ARGV);
111 $$hash{$first} = $rest;
114 ${"opt_$first"} = $rest;
115 push( @EXPORT, "\$opt_$first" );
124 push( @EXPORT, "\$opt_$first" );
135 local $Exporter::ExportLevel = 1;
141 return $OUTPUT_HELP_VERSION if defined $OUTPUT_HELP_VERSION;
142 return \*STDOUT if $STANDARD_HELP_VERSION;
147 exit 0 if $STANDARD_HELP_VERSION;
149 print {output_h()} <<EOM;
150 [Now continuing due to backward compatibility and excessive paranoia.
151 See ``perldoc $p'' about \$$p\::STANDARD_HELP_VERSION.]
155 sub version_mess ($;$) {
158 if (@_ and defined &main::VERSION_MESSAGE) {
159 main::VERSION_MESSAGE($h, __PACKAGE__, $VERSION, $args);
161 my $v = $main::VERSION;
162 $v = '[unknown]' unless defined $v;
164 $myv .= ' [paranoid]' unless $STANDARD_HELP_VERSION;
166 $perlv = sprintf "%vd", $^V if $] >= 5.006;
168 $0 version $v calling Getopt::Std::getopts (version $myv),
169 running under Perl version $perlv.
174 sub help_mess ($;$) {
177 if (@_ and defined &main::HELP_MESSAGE) {
178 main::HELP_MESSAGE($h, __PACKAGE__, $VERSION, $args);
180 my (@witharg) = ($args =~ /(\S)\s*:/g);
181 my (@rest) = ($args =~ /([^\s:])(?!\s*:)/g);
182 my ($help, $arg) = ('', '');
184 $help .= "\n\tWith arguments: -" . join " -", @witharg;
185 $arg = "\nSpace is not required between options and their arguments.";
188 $help .= "\n\tBoolean (without arguments): -" . join " -", @rest;
190 my ($scr) = ($0 =~ m,([^/\\]+)$,);
191 print $h <<EOH if @_; # Let the script override this
193 Usage: $scr [-OPTIONS [-MORE_OPTIONS]] [--] [PROGRAM_ARG1 ...]
197 The following single-character options are accepted:$help
199 Options may be merged together. -- stops processing of options.$arg
202 if ( defined $0 and $0 ne '-e' and -f $0 and -r $0
203 and open my $script, '<', $0 ) {
205 $has_pod = 1, last if /^=(pod|head1)/;
208 print $h <<EOH if $has_pod;
217 # getopts('a:bc'); # -a takes arg. -b & -c not. Sets opt_* as a
221 my ($argumentative, $hash) = @_;
222 my (@args,$first,$rest,$exit);
227 @args = split( / */, $argumentative );
228 while(@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/s) {
229 ($first,$rest) = ($1,$2);
230 if (/^--$/) { # early exit if --
234 my $pos = index($argumentative,$first);
236 if (defined($args[$pos+1]) and ($args[$pos+1] eq ':')) {
239 ++$errs unless @ARGV;
240 $rest = shift(@ARGV);
243 $$hash{$first} = $rest;
246 ${"opt_$first"} = $rest;
247 push( @EXPORT, "\$opt_$first" );
256 push( @EXPORT, "\$opt_$first" );
267 if ($first eq '-' and $rest eq 'help') {
268 version_mess($argumentative, 'main');
269 help_mess($argumentative, 'main');
273 } elsif ($first eq '-' and $rest eq 'version') {
274 version_mess($argumentative, 'main');
279 warn "Unknown option: $first\n";
290 local $Exporter::ExportLevel = 1;