[Perl] Getopt 函數來接收用戶參數的使用


我們在linux常常用到一個程序需要加入參數,現在了解一下perl中的有關控制參數的函數.getopt.在linux有的參數有二種形式.一種是–help,另一種是-h.也就是-和–的分別.–表示完整參數.-表示簡化參數.

在perl中也分這二種.

Getopt::Std模塊的功能: 初始化perl命令行中所接受的參數,簡化了命令行參數的解析。

簡化參數例子:

1 2 3 4 5 6 7 8 9 10
#!/usr/bin/perl -w 
use strict; 
use Getopt::Std;   
use vars qw($opt_a $opt_b $opt_c);
getopts('a:b:c:');   
print "\$opt_a =>; $opt_a\n" 
if $opt_a; print "\$opt_b =>; $opt_b\n" 
if $opt_b; print "\$opt_c =>; $opt_c\n" 
if $opt_c;

輸出如下:

[root@mail test]# ./getopt.pl -a aa -b bb -c cc

$opt_a =>; aa

$opt_b =>; bb $opt_c =>; cc

 

完整參數

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
  #!/usr/bin/perl 
use Getopt::Long; 
Getopt::Long::GetOptions( 'page=i' => \$page,'onoff!' => \$onoff, 'help' => \$wants_help, 'name=s' => \$name, 'number:i' =>\$number);   }   
if(defined($page))
{
   
    
  print "page flag set to $page "
}
if(defined($onoff))

          
  print "onoff flag set to $onoff "; 

if(defined($wants_help))

         
  print "help flag set to $wants_help "; 

if(defined($name))
{
          
  print "name flag set to $name "; 
}
if(defined($number))

          
  print "number flag set to $number "; 
}

./getlong.pl -name AAA
name flag set to AAA

 

帶值參數
※參數類型:整數, 浮點數, 字串

GetOptions( ‘tag=s’ => \$tag );
‘=’表示此參數一定要有參數值, 若改用’:'代替表示參數不一定要有參數值
‘s’表示傳遞字串參數, 若為’i'表傳遞整數參數, 若為’f'表傳遞浮點數
example:
test.pl –tag=string
or
test.pl –tag string

 

多參數值的參數

GetOptions ("library=s" => \@libfiles);
參數傳到 @tag
or
GetOptions ("library=s@" => \$libfiles);
參數傳到 @$tag
example:
test.pl –library lib/stdlib –library lib/extlib

 

參數別名
GetOptions (‘length|height=f’ => \$length);
第一個名稱為primary name, 其他的名稱為alias(可有多個alias名稱)
當使用hash參數時, 使用primary name作為key值

參數的簡稱及大小寫
GetOptions (‘length|height=f’ => \$length, "head" => \$head);
若沒有特別設定, Getopt會忽略參數的大小寫, 也就是 -l or -L 指的都
是同一個參數(–length)

對於不傳遞參數的選項,也就是一些開關類型,可以在第一部分后接“!”,這表示該選項不接收自變量,但是可以通過在前面加上no變成負的(例如,“more”選項的-nomore)。如果不是用“!”,而是“+”,這表示它會在每次出現的時候增加一個變量。如果選項出現在命令行里,那么相關的變量被設置為1;如果負的選項出現了,那么相關的變量就被設置為0。

 

hash參數值(有名稱及參數值)
GetOptions ("define=s" => \�fines);
or
GetOptions ("define=s%" => \$defines);
example:
test.pl –define os=linux –define vendor=redhat

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

http://www.cnblogs.com/xianghang123/archive/2011/07/14/2106586.html

======================================================================================

示例程序:

getopt.pl;

 
1 #!/usr/bin/perl -w
2 #use strict;
3 use Getopt::Std;
4 use vars qw($opt_a $opt_b $opt_c);
5 getopts('a:b:c');
6 print "opt_a =>;$opt_a\n" if $opt_a;
7 print "opt_b =>;$opt_b\n" if $opt_b;
8 print "opt_c =>;$opt_c\n" if $opt_c;

注釋:'a:b:c',a和b后有冒號,表示-a,-b后面要跟參數.c后面沒有冒號,表示-c后面不帶參數.

而且-a,-b后所跟的參數分別賦給變量$opt_a和$opt_b,對於變量$opt_c,若命令行加了-c,則$opt_c=1,否則為0.

如:

perl getopt.pl -a aa -b bb -c cc

顯示:

opt_a =>;aa
opt_b =>;bb
opt_c =>;1

如:

perl getopt.pl -a aa -b bb
opt_a =>;aa
opt_b =>;bb

(因為加了if判斷,所以$opt_c沒有顯示);

上面的例子,用Getopt::Long可以這樣實現

 

 

上面的例子,用Getopt::Long可以這樣實現

getoptions.pl

 
1 #!/usr/bin/perl
2 use Getopt::Long;
3 use vars qw($opt_a $opt_b $opt_c);
4 GetOptions("a=s"=>\$opt_a,"b|opt_b:i"=>\$opt_b,"c"=>\$opt_c);
5 print "opt_a =>;$opt_a\n" if $opt_a;
6 print "opt_b =>;$opt_b\n" if $opt_b;
7 print "opt_c =>;$opt_c\n" if $opt_c;

a=s表示可以用-a aa的形式,即變量-a 類型是字符串
b|opt_b:i表示可以用-b或者-opt_b來取得變量,類型為整型
c表示如果有-c的參數值則$opt_c等於1,否則等於0

其中
s表示字符串,i表整型,f表示浮點型
=表示要有參數值或者沒有這個參數如:perl getoptions.pl -a aa -b bb或者perl getoptions.pl -b bb
:表示可有參數值也可以沒有參數值
如:
perl getoptions.pl -a aa -b 2 -c cc
opt_a =>;aa
opt_b =>;2
opt_c =>;1
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
http://www.cnblogs.com/zjking99/articles/2117258.html
=================================================================================

Perl Getopt and GetOptions

 


Two Perl modules (Getopt and Getoptions::Long) work to extract program flags and arguments much like Getopt and Getopts do for shell programming. The Perl modules, especially GetOptions::Long, are much more powerful and flexible.
Simple scripts show the power of these:
#!/usr/bin/perl # script is "./g" use Getopt::Std; %options=(); getopts("od:fF",\%options); # like the shell getopt, "d:" means d takes an argument print "-o $options{o}\n" if defined $options{o}; print "-d $options{d}\n" if defined $options{d}; print "-f $options{f}\n" if defined $options{f}; print "-F $options{F}\n" if defined $options{F}; print "Unprocessed by Getopt::Std:\n" if $ARGV[0]; foreach (@ARGV) { print "$_\n"; }

Trying it out:
bash-2.05a$ ./g -f -d F -o -F -o 1 -d F -f 1 -F 1 bash-2.05a$ ./g -o -d foo -o 1 -d foo bash-2.05a$ ./g -o rough -d foo -o 1 Unprocessed by Getopt::Std: rough -d foo

Processing of arguments stops when it saw "rough".
If you leave off a required argument, it just gets swallowed:
bash-2.05a$ ./g -d bash-2.05a$ ./g -d foo -d foo

But it's easily confused:
bash-2.05a$ ./g -d -o -f -d -o -f 1

It thinks that -o is the argument of -d.
bash-2.05a$ ./g -k Unknown option: k %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
http://aplawrence.com/Unix/perlgetopts.html
===========================================================================
另外,見:http://www.perlmonks.org/?node_id=892257
以及http://perldoc.perl.org/Pod/Usage.html
中的解釋,推薦使用 Getopt::Long
還有一種方法是在程序開始時添加代碼:

require "getopt.pl";
然后使用Getopt(abc);即可
便可使用 
$opt_a和$opt_b,$opt_c,他們以及分別指向了各自后面跟着的文件。
例如命令輸入為: 


perl myprogram.pl -a filename1 -b filename2 -c filename3


則$opt_a=>filename1 


$opt_b


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM