7 getopt, getopts - Process single-character switches with switch clustering
13 getopt('oDI'); # -o, -D & -I take arg. Sets $opt_* as a side effect.
14 getopt('oDI', \%opts); # -o, -D & -I take arg. Values in %opts
15 getopts('oif:'); # -o & -i are boolean flags, -f takes an argument
16 # Sets $opt_* as a side effect.
17 getopts('oif:', \%opts); # options as above. Values in %opts
21 The getopt() function processes single-character switches with switch
22 clustering. Pass one argument which is a string containing all switches
23 that take an argument. For each switch found, sets $opt_x (where x is the
24 switch name) to the value of the argument if an argument is expected,
25 or 1 otherwise. Switches which take an argument don't care whether
26 there is a space between the switch and the argument.
28 The getopts() function is similar, but you should pass to it the list of all
29 switches to be recognized. If unspecified switches are found on the
30 command-line, the user will be warned that an unknown option was given.
32 Note that, if your code is running under the recommended C<use strict
33 'vars'> pragma, you will need to declare these package variables
38 For those of you who don't like additional global variables being created, getopt()
39 and getopts() will also accept a hash reference as an optional second argument.
40 Hash keys will be x (where x is the switch name) with key values the value of
41 the argument or 1 if no argument is specified.
43 To allow programs to process arguments that look like switches, but aren't,
44 both functions will stop processing switches when they see the argument
45 C<-->. The C<--> will be removed from @ARGV.
47 =head1 C<--help> and C<--version>
49 If C<-> is not a recognized switch letter, getopts() supports arguments
50 C<--help> and C<--version>. If C<main::HELP_MESSAGE()> and/or
51 C<main::VERSION_MESSAGE()> are defined, they are called; the arguments are
52 the output file handle, the name of option-processing package, its version,
53 and the switches string. If the subroutines are not defined, an attempt is
54 made to generate intelligent messages; for best results, define $main::VERSION.
56 If embedded documentation (in pod format, see L<perlpod>) is detected
57 in the script, C<--help> will also show how to access the documentation.
59 Note that due to excessive paranoia, if $Getopt::Std::STANDARD_HELP_VERSION
60 isn't true (the default is false), then the messages are printed on STDERR,
61 and the processing continues after the messages are printed. This being
62 the opposite of the standard-conforming behaviour, it is strongly recommended
63 to set $Getopt::Std::STANDARD_HELP_VERSION to true.
65 One can change the output file handle of the messages by setting
66 $Getopt::Std::OUTPUT_HELP_VERSION. One can print the messages of C<--help>
67 (without the C<Usage:> line) and C<--version> by calling functions help_mess()
68 and version_mess() with the switches string as an argument.
73 @EXPORT = qw(getopt getopts);
75 # uncomment the next line to disable 1.03-backward compatibility paranoia
76 # $STANDARD_HELP_VERSION = 1;
78 # Process single-character switches with switch clustering. Pass one argument
79 # which is a string containing all switches that take an argument. For each
80 # switch found, sets $opt_x (where x is the switch name) to the value of the
81 # argument, or 1 if no argument. Switches which take an argument don't care
82 # whether there is a space between the switch and the argument.
85 # getopt('oDI'); # -o, -D & -I take arg. Sets opt_* as a side effect.
88 my ($argumentative, $hash) = @_;
89 $argumentative = '' if !defined $argumentative;
94 while (@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
95 ($first,$rest) = ($1,$2);
96 if (/^--$/) { # early exit if --
100 if (index($argumentative,$first) >= 0) {
106 $rest = shift(@ARGV);
109 $$hash{$first} = $rest;
112 ${"opt_$first"} = $rest;
113 push( @EXPORT, "\$opt_$first" );
122 push( @EXPORT, "\$opt_$first" );
133 local $Exporter::ExportLevel
= 1;
139 return $OUTPUT_HELP_VERSION if defined $OUTPUT_HELP_VERSION;
140 return \
*STDOUT
if $STANDARD_HELP_VERSION;
145 exit 0 if $STANDARD_HELP_VERSION;
147 print {output_h
()} <<EOM;
148 [Now continuing due to backward compatibility and excessive paranoia.
149 See ``perldoc $p'' about \$$p\::STANDARD_HELP_VERSION.]
153 sub version_mess
($;$) {
156 if (@_ and defined &main
::VERSION_MESSAGE
) {
157 main
::VERSION_MESSAGE
($h, __PACKAGE__
, $VERSION, $args);
159 my $v = $main::VERSION
;
160 $v = '[unknown]' unless defined $v;
162 $myv .= ' [paranoid]' unless $STANDARD_HELP_VERSION;
164 $perlv = sprintf "%vd", $^V
if $] >= 5.006;
166 $0 version $v calling Getopt::Std::getopts (version $myv),
167 running under Perl version $perlv.
172 sub help_mess
($;$) {
175 if (@_ and defined &main
::HELP_MESSAGE
) {
176 main
::HELP_MESSAGE
($h, __PACKAGE__
, $VERSION, $args);
178 my (@witharg) = ($args =~ /(\S)\s*:/g);
179 my (@rest) = ($args =~ /([^\s:])(?!\s*:)/g);
180 my ($help, $arg) = ('', '');
182 $help .= "\n\tWith arguments: -" . join " -", @witharg;
183 $arg = "\nSpace is not required between options and their arguments.";
186 $help .= "\n\tBoolean (without arguments): -" . join " -", @rest;
188 my ($scr) = ($0 =~ m
,([^/\\]+)$,);
189 print $h <<EOH if @_; # Let the script override this
191 Usage: $scr [-OPTIONS [-MORE_OPTIONS]] [--] [PROGRAM_ARG1 ...]
195 The following single-character options are accepted:$help
197 Options may be merged together. -- stops processing of options.$arg
200 if ( defined $0 and $0 ne '-e' and -f
$0 and -r
$0
201 and open my $script, '<', $0 ) {
203 $has_pod = 1, last if /^=(pod|head1)/;
206 print $h <<EOH if $has_pod;
215 # getopts('a:bc'); # -a takes arg. -b & -c not. Sets opt_* as a
219 my ($argumentative, $hash) = @_;
220 my (@args,$first,$rest,$exit);
225 @args = split( / */, $argumentative );
226 while(@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/s) {
227 ($first,$rest) = ($1,$2);
228 if (/^--$/) { # early exit if --
232 my $pos = index($argumentative,$first);
234 if (defined($args[$pos+1]) and ($args[$pos+1] eq ':')) {
237 ++$errs unless @ARGV;
238 $rest = shift(@ARGV);
241 $$hash{$first} = $rest;
244 ${"opt_$first"} = $rest;
245 push( @EXPORT, "\$opt_$first" );
254 push( @EXPORT, "\$opt_$first" );
265 if ($first eq '-' and $rest eq 'help') {
266 version_mess
($argumentative, 'main');
267 help_mess
($argumentative, 'main');
271 } elsif ($first eq '-' and $rest eq 'version') {
272 version_mess
($argumentative, 'main');
277 warn "Unknown option: $first\n";
288 local $Exporter::ExportLevel
= 1;