[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 " 
if $opt_a; print "$opt_b =>; $opt_b " 
if $opt_b; print "$opt_c =>; $opt_c " 
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 " if $opt_a;
7 print "opt_b =>;$opt_b " if $opt_b;
8 print "opt_c =>;$opt_c " 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 " if $opt_a;
6 print "opt_b =>;$opt_b " if $opt_b;
7 print "opt_c =>;$opt_c " 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}
" if defined $options{o}; print "-d $options{d}
" if defined $options{d}; print "-f $options{f}
" if defined $options{f}; print "-F $options{F}
" if defined $options{F}; print "Unprocessed by Getopt::Std:
" if $ARGV[0]; foreach (@ARGV) { print "$_
"; }

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
原文地址:https://www.cnblogs.com/jiangzhaowei/p/5675923.html