15.1. Parsing Program ArgumentsProblemYou want to let users change your program's behavior by giving options on the command line. For instance, you want to allow the user to control the level of output that your program produces with a -v (verbose) option. SolutionUse the standard Getopt::Std module to permit single-character options: use Getopt::Std; # -v ARG, -D ARG, -o ARG, sets $opt_v, $opt_D, $opt_o getopt("vDo"); # -v ARG, -D ARG, -o ARG, sets $args{v}, $args{D}, $args{o} getopt("vDo", \%args); getopts("vDo:"); # -v, -D, -o ARG, sets $opt_v, $opt_D, $opt_o getopts("vDo:", \%args); # -v, -D, -o ARG, sets $args{v}, $args{D}, $args{o} Or, use the standard Getopt::Long module to permit named arguments: use Getopt::Long; GetOptions( "verbose" => \$verbose, # --verbose "Debug" => \$debug, # --Debug "output=s" => \$output ); # --output=string or --output=string DiscussionMost traditional programs like ls and rm take single-character options (also known as flags or switches), such as -l and -r . In the case of ls -l and rm -r , the argument is Boolean: either it is present or it isn't. Contrast this with gcc -o compiledfile source.c , where compiledfile is a value associated with the option -o . We can combine Boolean options into a single option in any order. For example: % rm -r -f /tmp/testdir Another way of saying this is: % rm -rf /tmp/testdir
The Getopt::Std module, part of the standard Perl distribution, parses these types of traditional options. Its
Getopt::Std also provides the use Getopt::Std; getopts("o:"); if ($opt_o) { print "Writing output to $opt_o"; }
Both use Getopt::Std; %option = (); getopts("Do:", \%option); if ($option{D}) { print "Debugging mode enabled.\n"; } # if not set, set output to "-". opening "-" for writing # means STDOUT $option{o} = "-" unless defined $option{o}; print "Writing output to file $option{o}\n" unless $option{o} eq "-"; open(STDOUT, "> $option{o}") or die "Can't open $option{o} for output: $!\n"; You can specify some programs' options using full words instead of single characters. These options are (usually) indicated with two dashes instead of one: % gnutar --extract --file latest.tar The value for the - -file option could also be given with an equals sign: % gnutar --extract --file=latest.tar
The Getopt::Long module's use Getopt::Long; GetOptions( "extract" => \$extract, "file=s" => \$file ); if ($extract) { print "I'm extracting.\n"; } die "I wish I had a file" unless defined $file; print "Working on the file $file\n";
If a key in the hash is just an option name, it's a Boolean option. The corresponding variable will be set to false if the option wasn't given, or to See AlsoThe documentation for the standard Getopt::Long and Getopt::Std modules; examples of argument parsing by hand can be found in Recipe 1.5 , Recipe 1.17 , Recipe 6.22 , Recipe 7.7 , Recipe 8.19 , and Recipe 15.12 Copyright © 2001 O'Reilly & Associates. All rights reserved. |
|