Perl中的GetOptions()不会验证完整的参数名称 [英] GetOptions() in perl does not validate full argument names

查看:1069
本文介绍了Perl中的GetOptions()不会验证完整的参数名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我要输入2个命令行参数-源和目标. GetOptions通过仅检查参数名称的第一个字符而不是完整字符串来允许命令行. 如何验证完整的参数字符串,而不是仅允许其子字符串传递?

Suppose I want to enter 2 command line parameters - source and destination. GetOptions allows the command line by checking only the first character of the argument name instead of the full string. How do I validate for the full arguments strings instead of just allowing its substrings to be passed?

这是一个示例程序:

my ($source,$dest);
GetOptions(
'from=s' => \$source,
'to=s' => \$dest
) or die "Incorrect arguments\n";

它接受以下任何一项:

  • -from
  • -fro
  • -fr
  • -f

  • -from
  • -fro
  • -fr
  • -f

-to

但是,我只希望它接受

  • -from
  • -to
  • -from
  • -to

,如果没有通过这些完整的单词,则失败.

and fail if anything except those full words is passed.

如何禁止缩写选项?

推荐答案

默认情况下,启用缩写.禁用auto_abbrev.请参考 Getopt :: Long :

By default, abbreviations are enabled. Disable auto_abbrev. Refer to Getopt::Long:

use warnings;
use strict;
use Getopt::Long qw(:config no_auto_abbrev);

my ($source,$dest);
GetOptions(
'from=s' => \$source,
'to=s' => \$dest
) or die "Incorrect arguements\n";

例如,通过-fro时,该消息消失:

For example, when -fro is passed, this dies with the message:

Unknown option: fro
Incorrect arguements

这篇关于Perl中的GetOptions()不会验证完整的参数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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