在Perl中检查有效命令行参数的简便方法?

时间:2021-01-04 16:05:29

I'm looking for an easy way to check for a correct number of command line parameters, displaying a usage message if an error occurs and then immediately exit.

我正在寻找一种简单的方法来检查正确数量的命令行参数,如果发生错误则显示用法消息,然后立即退出。

I thought of something like

我想到了类似的东西

if (@ARGV < 3) {
  print STDERR "Usage: $0 PATTERN [FILE...]\n";
  exit 1;
}

Is this a valid pattern? Sry, I'm a total Perl noob :-).

这是一个有效的模式吗? Sry,我是一个完全Perl noob :-)。

5 个解决方案

#1


29  

Also, I would STRONGLY suggest using the idiomatic way of processing command line arguments in Perl, Getopt::Long module (and start using named parameters and not position-based ones).

另外,我强烈建议使用在Perl,Getopt :: Long模块中处理命令行参数的惯用方法(并开始使用命名参数而不是基于位置的参数)。

You don't really CARE if you have <3 parameters. You usually care if you have parameters a, b and C present.

如果你有<3个参数,你就没有真正关心。如果你有参数a,b和C,你通常会关心。

As far as command line interface design, 3 parameters is about where the cut-off is between positional parameters (cmd <arg1> <arg2>) vs. named parameters in any order (cmd -arg1 <arg1> -arg2 <arg2>).

就命令行界面设计而言,3个参数是关于位置参数(cmd )与任何顺序的命名参数之间的截止值(cmd -arg1 -arg2 ) 。

So you are better off doing:

所以你最好做的事情:

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};

#2


11  

Another common way to do that is to use die

另一种常见的方法是使用die

die "Usage: $0 PATTERN [FILE...]\n" if @ARGV < 3;

You can get more help on the @ARGV special variable at your command line:

您可以在命令行中获得有关@ARGV特殊变量的更多帮助:

perldoc -v @ARGV

#3


5  

Yes, it is fine. @ARGV contains the command-line arguments and evaluates in scalar context to their number.

是的,没关系。 @ARGV包含命令行参数,并在标量上下文中评估它们的编号。

(Though it looks like you meant @ARGV < 2 or < 1 from your error message.)

(虽然看起来你的错误信息中的@ARGV <2或<1)。

#4


1  

Use $#ARGV to get total number of passed argument to a perl script like so:

使用$#ARGV获取perl脚本的传递参数总数,如下所示:

if (@#ARGV < 4)

I've used before and worked as shown in http://www.cyberciti.biz/faq/howto-pass-perl-command-line-arguments/.

我之前使用过并按照http://www.cyberciti.biz/faq/howto-pass-perl-command-line-arguments/中的说明进行操作。

See the original documentation at http://perldoc.perl.org/perlvar.html, it states that:

请参阅http://perldoc.perl.org/perlvar.html上的原始文档,其中指出:

@ARGV

@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.

数组@ARGV包含用于脚本的命令行参数。 $#ARGV通常是参数的数量减1,因为$ ARGV [0]是第一个参数,而不是程序的命令名本身。请参阅$ 0以获取命令名称。

#5


-3  

You can compare with $#ARGV instead the array @ARGV

您可以与$#ARGV进行比较,而不是数组@ARGV

if ($#ARGV < 3) { ...

#1


29  

Also, I would STRONGLY suggest using the idiomatic way of processing command line arguments in Perl, Getopt::Long module (and start using named parameters and not position-based ones).

另外,我强烈建议使用在Perl,Getopt :: Long模块中处理命令行参数的惯用方法(并开始使用命名参数而不是基于位置的参数)。

You don't really CARE if you have <3 parameters. You usually care if you have parameters a, b and C present.

如果你有<3个参数,你就没有真正关心。如果你有参数a,b和C,你通常会关心。

As far as command line interface design, 3 parameters is about where the cut-off is between positional parameters (cmd <arg1> <arg2>) vs. named parameters in any order (cmd -arg1 <arg1> -arg2 <arg2>).

就命令行界面设计而言,3个参数是关于位置参数(cmd )与任何顺序的命名参数之间的截止值(cmd -arg1 -arg2 ) 。

So you are better off doing:

所以你最好做的事情:

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};

#2


11  

Another common way to do that is to use die

另一种常见的方法是使用die

die "Usage: $0 PATTERN [FILE...]\n" if @ARGV < 3;

You can get more help on the @ARGV special variable at your command line:

您可以在命令行中获得有关@ARGV特殊变量的更多帮助:

perldoc -v @ARGV

#3


5  

Yes, it is fine. @ARGV contains the command-line arguments and evaluates in scalar context to their number.

是的,没关系。 @ARGV包含命令行参数,并在标量上下文中评估它们的编号。

(Though it looks like you meant @ARGV < 2 or < 1 from your error message.)

(虽然看起来你的错误信息中的@ARGV <2或<1)。

#4


1  

Use $#ARGV to get total number of passed argument to a perl script like so:

使用$#ARGV获取perl脚本的传递参数总数,如下所示:

if (@#ARGV < 4)

I've used before and worked as shown in http://www.cyberciti.biz/faq/howto-pass-perl-command-line-arguments/.

我之前使用过并按照http://www.cyberciti.biz/faq/howto-pass-perl-command-line-arguments/中的说明进行操作。

See the original documentation at http://perldoc.perl.org/perlvar.html, it states that:

请参阅http://perldoc.perl.org/perlvar.html上的原始文档,其中指出:

@ARGV

@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.

数组@ARGV包含用于脚本的命令行参数。 $#ARGV通常是参数的数量减1,因为$ ARGV [0]是第一个参数,而不是程序的命令名本身。请参阅$ 0以获取命令名称。

#5


-3  

You can compare with $#ARGV instead the array @ARGV

您可以与$#ARGV进行比较,而不是数组@ARGV

if ($#ARGV < 3) { ...