欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Perl GetOptions 简介

程序员文章站 2022-04-24 09:47:47
...

关于GetOptions的介绍,请参照官方文档(link

The Getopt::Long module implements an extended getopt function called GetOptions(). It parses the command line from @ARGV , recognizing and removing specified options and their possible values.

This function adheres to the POSIX syntax for command line options, with GNU extensions. In general, this means that options have long names instead of single letters, and are introduced with a double dash "--". Support for bundling of command line options, as was the case with the more traditional single-letter approach, is provided but not enabled by default.

简单讲,就是执行脚本给定参数为长参(--verbose)或短参(-v)都可以接受和识别。(--verbose和-v实际上作用相同)

举个例子:

use strict;

use Getopt::Long;

my $verbose = '';

GetOptions("verbose" => \$verbose) or die ("Invalid arguments.\n");
	
if ($verbose){
	print "\$verbose received\n";
} else {
	print "No expected arguments\n";
}

--v 可识别

Perl GetOptions 简介

Perl GetOptions 简介

--verbose 可识别

Perl GetOptions 简介

Perl GetOptions 简介

传其它参数,比如-balabala。(不可识别并报错)

Perl GetOptions 简介

Perl GetOptions 简介

相关标签: perl