我想处理一个在我看来对程序来说几乎很自然的功能,但我不知道如何使用 Getopt perl 包(无论 Std ot Long)处理它。
我想要这样的东西:
./perlscript <main option> [some options like -h or --output-file some_name]
选项将用 - 或 -- 处理,但我希望能够让用户给我主要的和需要的选项而不用破折号。
Getopt 能够做到这一点,还是我必须手动处理?
最佳答案
听起来好像您在谈论非选项——基本的命令行参数。可以使用 @ARGV
访问它们。 Getopt
模块会将常规参数传递给您的脚本,不受干扰:
use strict;
use warnings;
use Getopt::Long;
GetOptions (
'foo' => \my $foo,
'bar=s' => \my $bar,
);
my @main_args = @ARGV;
# For example: perl script.pl --foo --bar XXX 1 2 3
# Produces: foo=1 bar=XXX main_args=1 2 3
print "foo=$foo bar=$bar main_args=@main_args\n";
关于perl - 如何使用 Getopt 处理主要选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2966320/