How can I parse command line arguments? - perl

文章推薦指數: 80 %
投票人數:10人

I want to parse a list of arguments in a perl script, for example i have this situation : Home Public Questions Tags Users Companies Collectives ExploreCollectives Teams StackOverflowforTeams –Startcollaboratingandsharingorganizationalknowledge. CreateafreeTeam WhyTeams? Teams CreatefreeTeam StackOverflowforTeamsismovingtoitsowndomain!Whenthemigrationiscomplete,youwillaccessyourTeamsatstackoverflowteams.com,andtheywillnolongerappearintheleftsidebaronstackoverflow.com. Checkyouremailforupdates. Collectives™onStackOverflow Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost. LearnmoreaboutCollectives Teams Q&Aforwork Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch. LearnmoreaboutTeams HowcanIparsecommandlinearguments? AskQuestion Asked 12yearsago Modified 4years,2monthsago Viewed 7ktimes 8 New!Savequestionsoranswersandorganizeyourfavoritecontent.Learnmore. Iwanttoparsealistofargumentsinaperlscript,forexampleihavethissituation: script.pl-h127.0.0.1-uuser-ppassarg1arg2arg3 Howcanidoforparsethelistofargumentthataren'toptioninanarray,andtheoptionparameterinscalarvalue? Thanks. perlargumentscommand-line-arguments Share Follow editedOct19,2010at22:03 Axeman 29.5k22goldbadges4747silverbadges101101bronzebadges askedOct19,2010at21:30 alerootaleroot 69.6k3030goldbadges172172silverbadges209209bronzebadges 1 3 InMasteringPerl,Italkaboutthe90+Perlmodulesthatletyoudothisinalmostanywaythatyoulike.:) – briandfoy Oct19,2010at22:23 Addacomment  |  2Answers 2 Sortedby: Resettodefault Highestscore(default) Trending(recentvotescountmore) Datemodified(newestfirst) Datecreated(oldestfirst) 16 Well,iftheyaretheonlythingsonthecommandlinethataren'tgivenasoptions,[email protected]@ARGV. useGetopt::Long; #saveargumentsfollowing-hor--hostinthescalar$host #the'=s'meansthatanargumentfollowstheoption #theycanfollowbyaspaceor'='(--host=127.0.0.1) GetOptions('host=s'=>\my$host ,'user=s'=>\my$user#samefor--useror-u ,'pass=s'=>\my$pass#samefor--passor-p ); #@ARGV:[qw] seeGetopt::Long Share Follow editedOct19,2010at22:01 answeredOct19,2010at21:42 AxemanAxeman 29.5k22goldbadges4747silverbadges101101bronzebadges Addacomment  |  13 Theargumentstoyourprogramarestoredin@ARGV. Ifyouwanttodoanyhandlingofswitchesatall,pleaseuseGetopt::Long,whichisastandardPerlmodule. Evenifyouthink"Oh,IcanjustpickthestuffIwantoutofthelist",pleaseuseGetopt::Long,becauseIguaranteeyouthetimewillcomethatyouwilladdmoreswitches,andyouwillwindupsaying"Man,IwishI'dstartedusingGetopt::Longfromthebeginning." Pleasealsoseemyblogpost"UseGetopt::Longevenifyouthinkyoudon'tneedit". Share Follow editedAug3,2018at21:12 answeredOct19,2010at21:48 AndyLesterAndyLester 88.8k1313goldbadges100100silverbadges150150bronzebadges 3 Thatlinkisbroken(404) – Bogolt Aug2,2018at20:51 1 Thanks,@Bogolt.IdugitupoutoftheWaybackMachine,republishedit,andupdatedthelink. – AndyLester Aug3,2018at21:12 Actually,betteruseargparse,thatsupportpositionnalargumentsandhaveawaycleannerinterfaceinmyopinion – ninjaconcombre May21,2021at8:44 Addacomment  |  YourAnswer ThanksforcontributingananswertoStackOverflow!Pleasebesuretoanswerthequestion.Providedetailsandshareyourresearch!Butavoid…Askingforhelp,clarification,orrespondingtootheranswers.Makingstatementsbasedonopinion;backthemupwithreferencesorpersonalexperience.Tolearnmore,seeourtipsonwritinggreatanswers. Draftsaved Draftdiscarded Signuporlogin SignupusingGoogle SignupusingFacebook SignupusingEmailandPassword Submit Postasaguest Name Email Required,butnevershown PostYourAnswer Discard Byclicking“PostYourAnswer”,youagreetoourtermsofservice,privacypolicyandcookiepolicy Nottheansweryou'relookingfor?Browseotherquestionstaggedperlargumentscommand-line-argumentsoraskyourownquestion. TheOverflowBlog IntroducingtheOverflowOfflineproject Hehelpedbuild.NETandVSCode—Now’sheworkingonWeb3(Ep.499) FeaturedonMeta The2022Community-a-thonhasbegun! Mobileappinfrastructurebeingdecommissioned Linked 2 PerlpassargumentsintofilefromBash Related 1348 HowcanIpassargumentstoabatchfile? 2432 HowdoIparsecommandlineargumentsinBash? 666 HowdoIparsecommandlineargumentsinJava? 1181 Howtopasscommandlineargumentstoaraketask 744 Howtoread/processcommandlinearguments? 575 Howtohandlecommand-lineargumentsinPowerShell 4969 Reference—WhatdoesthissymbolmeaninPHP? 2842 HowdoIpasscommandlineargumentstoaNode.jsprogram? 1145 PropagateallargumentsinaBashshellscript HotNetworkQuestions IsthereaVFRmobileappthatshowsotheraircraftinrealtime? Whyarefangsofgirlssocommoninanime? Misuseofmixedeffectsmodel Meaningof"Allthemcreepsinthatstoretostealfrom,Arnie'sgottopickoutaladybulltoclip!" Whyaretheredollaramountslistednearthecopyrightinformationforjournalarticles? Linuxfilesystemsforcode-compilation,whichperformbest? Zeropaddingversuszerostuffing Bootablefloppydisk DidtheDudeeverhaveanoccupationotherthanunemployed? Istriplebranchingnecessaryinmakingasyntaxtreefor'thegirlintheroomwavedtome'? DoAustraliancitizensneedaletterofinvitationtovisitaSchengenandEUcountryfortourism? UnderwhatauthoritycantheUSmakefederalreservenoteslegaltender? WhereisthisparkinMontreal,withautumncolortreesandpond? WhywasThor'shammerneverusedagain? Howisitpossibletofiltereitherhighorlowfrequenciesofasinglesinewave? HowcanIcurateingredientsformychildtodofoolproofself-directedsensoryplay+baking? Changingnumberformatsinstrings Nomatchingfunctionforcallto'DS3231(constuint8_t&,constuint8_t&) WhatisaScrumMastertodowhenmanagementdonotsupportAgileandScrum? Howtoobtainaminor'spassportwithanabsentparent HowdoProtestantsReceiveforgivenessforpost-BaptismalSins? Plotasequenceoffunctionsinoneplot WhyistheUSvowingtoprotectTaiwanwhentheywon'tprotectUkraine? Whattodoafterapuremathacademicpath? morehotquestions Questionfeed SubscribetoRSS Questionfeed TosubscribetothisRSSfeed,copyandpastethisURLintoyourRSSreader. lang-perl Yourprivacy Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy. Acceptallcookies Customizesettings  



請為這篇文章評分?