简体   繁体   English

阅读Perl中的可选命令行参数

[英]Read optional command-line arguments in Perl

I am new to Perl and I'm confused with its handling of optional arguments. 我是Perl的新手,对它对可选参数的处理感到困惑。

If I have a perl script that's invoked with something along the lines of: 如果我有一个Perl脚本,该脚本使用类似于以下内容的方式调用:

plgrep [-f] < perl regular expression > < file/directory list > 

How would I determine whether or not the -f operator is given or not on the command line? 如何确定是否在命令行上给-f运算符?

Use Getopt::Long . 使用Getopt :: Long You could, of course, parse @ARGV by hand (which contains command line arguments), but there is no reason to do that with the existence of good modules for the job. 当然,您可以手动解析@ARGV (包含命令行参数),但是没有理由使用适用于此工作的良好模块来进行解析。

use warnings;
use strict;

use Getopt::Long;   

# Set up defaults here if you wish
my ($flag, $integer, $float, $string); 

usage(), exit  if not GetOptions(
    'f|flag!'   => \$flag, 
    'integer:i' => \$integer, 
    'float:f'   => \$float,
    'string:s'  => \$string
); 

# The script now goes. Has the flag been supplied?
if (defined($flag)) { print "Got flag: $flag\n" } # it's 1 
else {
    # $flag variable is 'undef'
}

sub usage {
    print "Usage: $0 [options]\n";  # -f or -flag, etc
}

The $flag can simply be tested for truth as well, if that is sufficient. 如果足够,也可以简单地测试$flag的真实性。 To only check whether -f is there or not, need just: GetOptions('f' => \\$flag); if ($flag) { }; 只检查-f是否存在,只需要: GetOptions('f' => \\$flag); if ($flag) { }; GetOptions('f' => \\$flag); if ($flag) { }; .

The module checks whether the invocation specifies arguments as they are expected. 模块检查调用是否按预期指定了参数。 These need not be entered, they are "options." 这些不需要输入,它们是“选项”。 However, for an unexpected invocation a die or warn message is printed (and in the above code our usage message is also printed and the script exits). 但是,如果发生意外调用,则会打印一条diewarn消息(并且在上面的代码中,还会打印我们的用法消息,并且脚本退出)。 So for script.pl -a the script exits with messages (from module and sub). 因此,对于script.pl -a ,脚本将退出并显示消息(来自模块和子程序)。

Abbreviations of option names are OK, if unambiguous; 选项名称的缩写如果没有歧义,则可以; script.pl -fl 0.5 exits with messages ( -flag or -float ?) while script.pl -i 5 is OK and $integer is set to 5. On the other hand, if an integer is not supplied after -i that is an error, since that option is defined to take one. script.pl -fl 0.5退出并显示消息( -flag-float ?),而script.pl -i 5正常且$integer设置为5。另一方面,如果-i之后没有提供整数,则为错误,因为该选项被定义为采用一个。 Multiple names for options can be specified, like f|flag . 可以指定选项的多个名称,例如f|flag Etc. There is far more. 等等,还有更多。

All of the parameters passed to your program appear in the array @ARGV , so you can simply check whether any of the array elements contain the string -f 传递给程序的所有参数都出现在数组@ARGV ,因此您可以简单地检查任何数组元素是否包含字符串-f

But if you are writing a program that uses many different options in combination, you may find it simpler to use the Getopt::Long module, which allows you to specify which parameters are optional, which take values, whether there are multiple synonynms for an option etc. 但是,如果您编写的程序结合使用了许多不同的选项,则可能会发现使用Getopt::Long模块更加简单,该模块可让您指定哪些参数是可选的,哪些参数带有值,以及是否有多个同义词。选项等

A call to GetOptions allows you to specify the parameters that your program expects, and will remove from @ARGV any that appear in the command line, saving indicators in simple Perl variables that reflect which were provided and what values, if any, they had 调用GetOptions允许您指定程序期望的参数,并将从@ARGV删除命令行中出现的所有参数,并将指示符保存在简单的Perl变量中,以反映所提供的内容以及所提供的值(如果有)

For instance, in the simple case that you describe, you could write your code like this 例如,在您描述的简单情况下,您可以像这样编写代码

use strict;
use warnings 'all';
use feature 'say';

use Getopt::Long;
use Data::Dump;

say "\nBefore GetOptions";
dd \@ARGV;

GetOptions( f => \my $f_option);

say "\nAfter GetOptions";
dd $f_option;
dd \@ARGV;

output 产量

Before GetOptions
["-f", "regexp", "file"]

After GetOptions
1
["regexp", "file"]

So you can see that before the call to GetOptions , @ARGV contains all of the data in the command line. 因此,您可以看到在调用GetOptions之前,@ @ARGV包含命令行中的所有数据。 But afterwards, the -f has been removed and variable $f_option is set to 1 to indicate that the option was specified 但是之后, -f已被删除,变量$f_option设置为1表示已指定该选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM