这是我编写的用于测试命令行参数处理的简单脚本:
use Getopt::Long; my $help = 0; GetOptions( 'help|h|?' => \$help,) or die "Error!"; print "OK\n";
我得到的结果如下:
D:\>perl test.pl --help OK D:\>perl test.pl --hell Unknown option: hell Error! at test.pl line 10. D:\>perl test.pl --he OK D:\>perl test.pl --hel OK
有没有人注意过这个?行为(接受他和hel而不是帮助)是一个潜在的错误吗?
解决方法
不,这是故意的.它接受选项的最短非模糊版本,所以如果你有另一个选项“–hex”,它将不接受“–he”但它会接受“–hel”.