How can I parse command line arguments? - perl
文章推薦指數: 80 %
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
延伸文章資訊
- 1Processing command line arguments - @ARGV in Perl
Unfortunately there is not a lot we can do when parsing @ARGV "manually". In another article I'll...
- 2Parsing Program Arguments - Perl Cookbook [Book] - O'Reilly
Parsing Program Arguments Problem You want to let users change your program's behavior by giving ...
- 3Parsing your script's command line - PerlMonks
This tutorial describes how to deal with information passed in on the command line. Not the switc...
- 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...
- 5How can I parse command line arguments? - perl
I want to parse a list of arguments in a perl script, for example i have this situation :