如何将强制和可选命令行参数都传递给perl脚本? [英] How to pass both mandatory and optional command line arguments to perl script?

查看:94
本文介绍了如何将强制和可选命令行参数都传递给perl脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Getopt :: Long将选项传递给我的Perl脚本.

I am using Getopt::Long to pass options to my Perl script.

但是我想做这样的事情:

But I want to do something like this :

perl myScript mandatoryArgument1 -optionalArgument1=someValue

如果缺少必要的Argument1,我希望脚本抛出错误.该怎么办?

I want the script to throw an error if mandatoryArgument1 is missing. How can this be done?

推荐答案

好的Getopt::Long没有为此的机制.它专门处理选项 .

The good Getopt::Long does not have a mechanism for that. It specifically processes options.

但是,由于其工作原理,它会从@ARGV中删除这些选项,因此一旦完成,您就可以检查期望的参数是否存在.请参阅第二部分,但我想首先提出另一种方法:命名这些参数,然后Getopt将对其进行处理.

However, as it does its work it removes those options from @ARGV so once it's finished you can check whether the expected arguments are there. See the second part for this but I would like to first suggest another way: Make those arguments named and then Getopt will process them.

然后很容易检查它们是否已提交.例如

Then it is easy to check whether they were submitted. For example

use warnings;
use strict;
use feature 'say';
use Getopt::Long;

my $mandatoryArg;
my $opt;

# Read command-line arguments, exit with usage message in case of error
GetOptions( 'name=s' => \$mandatoryArg, 'flag' => \$opt )
    or usage(); 

if (not defined $mandatoryArg) {
    say STDERR "Argument 'name' is mandatory";
    usage();
}

# The program goes now. Value for $opt may or may have not been supplied

sub usage {
    say STDERR "Usage: $0 ...";   # full usage message
    exit;
}

因此,如果在命令行上未提供--name string,则$mandatoryArg保持未定义状态,程序退出.该变量不需要默认值,因为它是强制性的,并且它不应该具有默认值才能进行此检查.

So if --name string isn't given on the command line the $mandatoryArg stays undefined and the program exits. That variable doesn't need a default value since it is mandatory, and it shouldn't have one for this check to work.

参数检查和处理通常要涉及得多,这是Getopt亮起的时候.

Argument checking and processing is often far more involved, and this is when Getopt shines.

提供的问题中的mandatoryArgument1没有名称.虽然可以使Getopt对非选项输入进行操作,它无法检测到期望的对象在那里.

The mandatoryArgument1 in the question is supplied without a name. While Getopt can be made to act on a non-option input, it cannot detect that an expected one is not there.

该模块确实允许在命令行中的任何位置混合参数和命名选项.请参见带有其他参数的选项在文档中.因此,您可以将程序调用为

The module does allow to mix arguments with named options, anywhere on the command line. See Option with other arguments in docs. So you can invoke the program as

script.pl --opt1 value1 unnamed_arg --opt2 value2

但是我建议用户在命名选项之后提供它们.

but I'd suggest to the user to supply them after named options.

然后,在GetOptions完成其工作后,@ARGV将包含字符串unnamed_arg,您可以获取它(或发现它不存在). GetOptions对命名选项的处理与上面相同.

Then, after GetOptions does its job, @ARGV will contain the string unnamed_arg and you can get it (or find out that it isn't there). Processing of named options by GetOptions is the same as above.

my ($var1, $var2, $flag);

GetOptions('opt1=s' => \$var1, 'opt2=i' => \$var2, 'f' => \$flag)
    or usage(); 

# All supplied named options have been collected, all else left in @ARGV
# Read the remaining argument(s) from @ARGV, or exit with message

# This can get far more complicated if more than one is expected
my $mandatoryArg1 = shift @ARGV || do {
    say STDERR "Mandatory argument (description) is missing";
    usage();
};

上面,一旦Getopt拾取了命名参数,您必须手工处理@ARGV.如果有多个这样的参数,则必须严格遵守它们在命令行上的相对位置,并且可能很难检查用户是否混淆了他们的命令.

Above you have to process @ARGV by hand once Getopt picked up the named arguments. If there are more than one such argument you have to strictly respect their relative position on the command line, and it may be hard to check whether the user may have confused their order.

Getopt之类的所有可能的模块都精确存在,因此我们不必这样做.

While all that is possible modules like Getopt exist precisely so that we don't have to do it.

 使用'<>'

Getoptions( 'opt=s' => \$var, ..., '<>' => \&arg_cb );

sub arg_cb { say "Doesn't look like an option: $_[0]" }

仅当看到非选项参数 时才调用sub arg_cb.

where the sub arg_cb is invoked only if a non-option-looking argument is seen.

这篇关于如何将强制和可选命令行参数都传递给perl脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆