perl - 如何使用 Getopt 处理主要选项

标签 perl getopt

我想处理一个在我看来对程序来说几乎很自然的功能,但我不知道如何使用 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/

相关文章:

Perl: "system"什么时候真正返回?

performance - 为了提高性能,建议安装 Perl 的方法是什么?

c - 使用 getopt_long 检测没有参数

ruby - OptionParser 可以跳过未知选项,以便稍后在 Ruby 程序中处理吗?

c - C语言中的optopt和getopt

linux - 如何比较Linux中特定格式的日期

perl - 如何在模板工具包中轻松创建表 View in perl/Catalyst

linux - 如何让一个shell脚本每天自动运行?

c++ - 将负数传递给 Getopt

c - 为什么从 getopt_long() 中省略这一行会导致段错误?