我正在寻找一种简单的方法来检查命令行参数的正确数量,如果出现错误,显示使用消息,然后立即退出。
我想了一些类似的事情
if (@ARGV < 3) {
print STDERR "Usage: $0 PATTERN [FILE...]
";
exit 1;
}
这是一个有效的模式吗?
我正在寻找一种简单的方法来检查命令行参数的正确数量,如果出现错误,显示使用消息,然后立即退出。
我想了一些类似的事情
if (@ARGV < 3) {
print STDERR "Usage: $0 PATTERN [FILE...]
";
exit 1;
}
这是一个有效的模式吗?
此外,我强烈建议使用Perl中处理命令行参数的惯用方法,Getopt::Long
模块(并开始使用命名参数,而不是基于位置的参数)。
如果你有<;3个参数。您通常关心是否存在参数a、b和C。
就命令行接口设计而言,3个参数是关于位置参数(cmd<;arg1>;<;arg2>;
)与任何顺序的命名参数(cmd-arg1<;arg1>;-arg2<;arg2>;
)之间的界限。
所以你最好做:
use Getopt::Long;
my %args;
GetOptions(\%args,
"arg1=s",
"arg2=s",
"arg3=s",
) or die "Invalid arguments!";
die "Missing -arg1!" unless $args{arg1};
die "Missing -arg2!" unless $args{arg2};
die "Missing -arg3!" unless $args{arg3};
另一种常见的方法是使用死亡
die "Usage: $0 PATTERN [FILE...]
" if @ARGV < 3;
您可以在命令行中获得有关@ARGV
特殊变量的更多帮助:
perldoc -v @ARGV
是的,很好@ARGV
包含命令行参数,并在标量上下文中计算它们的数字。
(尽管看起来您的意思是错误消息中的@ARGV<;2
或<;1
。)
使用$#ARGV获取传递给perl脚本的参数总数,如下所示:
if (@#ARGV < 4)
我以前使用过,并按照http://www.cyberciti.biz/faq/howto-pass-perl-command-line-arguments/。
请参阅http://perldoc.perl.org/perlvar.html,其中规定:
@ARGV公司
The array @ARGV公司 contains the command-line arguments intended for the script. $#ARGV is generally the number of arguments minus one, because $ARGV[0] is the first argument, not the program s command name itself. See $0 for the command name.
I am building a Web interface to monitor an embedded system. I have built a Perl script which runs remote commands and gathers output from that system. Now what I need is a Web interface which makes ...
How do I tell what type of value is in a Perl variable? $x might be a scalar, a ref to an array or a ref to a hash (or maybe other things).
When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...
I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...
I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...
I would like to submit a form to a CGI script localy (w3c-markup-validator), but it is too slow using curl and apache, I want to use this CGI script more than 5,000 times in an another script. and ...
So I m running perl 5.10 on a core 2 duo macbook pro compiled with threading support: usethreads=define, useithreads=define. I ve got a simple script to read 4 gzipped files containing aroud 750000 ...
A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...