如何使用 Getopt::Long 从命令提示符中检索参数?

How to retrieve the arguments from command prompt using Getopt::Long?

要求是

perl prg.pl --product --param Product1 --from FilePath

其中 --product 指定对必须接受参数 Product1 的函数的引用,FilePath 是要搜索产品的文件路径。

GetOptions('product=s'=>\&getproduct, 'param=s'=>$param,'from=s'=>$from);

尽管如此,当 运行 Perl 程序时,它给出了一个错误,即值 product 和 from 未初始化。 你能帮我解决这个问题吗?

您指出 product 选项需要一个值 (=s),因此

             Value for --product. (Not an option.)
             |
             |       Doesn't start with `-`, so option parsing ends.
             |       |
          vvvvvvv vvvvvvvv
--product --param Product1 --from FilePath
                  ^^^^^^^^^^^^^^^^^^^^^^^^
                             |
                             Not options, so found in @ARGV.

修复:

GetOptions(
   'help|h|?' => \&help,
   'product'  => $product,
   'param=s'  => $param,
   'from=s'   => $from,
)
   or usage();

@ARGV == 0
   or usage("Too many arguments.");

有关 usagehelp 的实施示例,请参见