Parsing Program Arguments - Perl Cookbook [Book] - O'Reilly
文章推薦指數: 80 %
Parsing Program Arguments Problem You want to let users change your program's behavior by giving options on the command line. For instance, you want to ... Skiptomaincontent PerlCookbookby GetfullaccesstoPerlCookbookand60K+othertitles,withfree10-daytrialofO'Reilly. There'salsoliveonlineevents,interactivecontent,certificationprepmaterials,andmore. Startyourfreetrial ParsingProgramArgumentsProblem Youwanttoletuserschangeyourprogram’sbehaviorbygiving optionsonthecommandline.Forinstance,youwanttoallowtheuser tocontrolthelevelofoutputthatyourprogramproduceswitha -v(verbose)option. SolutionUsethestandardGetopt::Stdmoduletopermitsingle-character options:useGetopt::Std; #-vARG,-DARG,-oARG,sets$opt_v,$opt_D,$opt_o getopt("vDo"); #-vARG,-DARG,-oARG,sets$args{v},$args{D},$args{o} getopt("vDo",\%args); getopts("vDo:");#-v,-D,-oARG,sets$opt_v,$opt_D,$opt_o getopts("vDo:",\%args);#-v,-D,-oARG,sets$args{v},$args{D},$args{o}Or,usethestandardGetopt::Longmoduletopermitnamed arguments:useGetopt::Long; GetOptions("verbose"=>\$verbose,#--verbose "Debug"=>\$debug,#--Debug "output=s"=>\$output);#--output=stringor--outputstringDiscussionMosttraditionalprogramslikelsand rmtakesingle-characteroptions(alsoknownas flagsorswitches),suchas-land -r.Inthecaseofls -landrm-r,theargumentis Boolean:eitheritispresentoritisn’t.Contrastthiswith gcc-ocompiledfilesource.c,where compiledfileisavalueassociatedwiththe option-o.WecancombineBoolean optionsintoasingleoptioninanyorder.Forexample:%rm-r-f/tmp/testdirAnotherwayofsayingthisis:%rm-rf/tmp/testdirTheGetopt::Stdmodule,partofthestandardPerldistribution, parsesthesetypesoftraditionaloptions.Its getopt functiontakesasinglestring... GetPerlCookbooknowwiththeO’Reillylearningplatform. O’Reillymembersexperienceliveonlinetraining,plusbooks,videos,anddigitalcontentfromnearly200publishers. Startyourfreetrial Don’tleaveempty-handed GetMarkRichards’sSoftwareArchitecturePatternsebooktobetterunderstandhowtodesigncomponents—andhowtheyshouldinteract. It’syours,free. Getitnow Close
延伸文章資訊
- 1Parsing your script's command line - PerlMonks
This tutorial describes how to deal with information passed in on the command line. Not the switc...
- 2Recipe 15.1. Parsing Program Arguments
Parsing Program Arguments. Problem. You want to let users change your program's behavior by givin...
- 3Getopt::Long - Extended processing of command line options
The Getopt::Long module implements an extended getopt function called GetOptions(). It parses the...
- 4How Can I Parse Command Line Arguments in Perl?
Perl passes command line arguments to a script using the built-in @ARGV array. This variable does...
- 5Parsing Command Line Options - The Perl Journal, Summer ...
Parsing Command Line Options. Johan Vromans. PACKAGES USED. Package, Version. Perl, 5. Getopt::St...