我知道如何使用Perl的Getopt :: Long,但我不知道如何配置它来接受任何未明确定义的"--key = value"对并将其粘贴在哈希中.换句话说,我不知道用户可能想要什么选项,所以我无法定义所有选项,但我希望能够解析所有这些选项.
建议?提前谢谢.
该Getopt::Long
文档提供了一个可能有用的配置选项:
pass_through (default: disabled) Options that are unknown, ambiguous or supplied with an invalid option value are passed through in @ARGV instead of being flagged as errors. This makes it possible to write wrapper scripts that process only part of the user supplied command line arguments, and pass the remaining options to some other program.
解析常规选项后,您可以使用runrig提供的代码来解析ad hoc选项.
Getopt :: Long不这样做.您可以自己解析选项...例如
my %opt; my @OPTS = @ARGV; for ( @OPTS ) { if ( /^--(\w+)=(\w+)$/ ) { $opt{$1} = $2; shift @ARGV; } elsif ( /^--$/ ) { shift @ARGV; last; } }
或者修改Getopt :: Long来处理它(或修改上面的代码以便在需要时处理更多种类的选项).