【问题标题】:Can Perl's Getopt::Long parse arguments I don't define ahead of time?Perl 的 Getopt::Long 可以解析我没有提前定义的参数吗?
【发布时间】:2010-10-06 12:05:35
【问题描述】:

我知道如何使用 Perl 的 Getopt::Long,但我不确定如何将其配置为接受未明确定义的任何“--key=value”对并将其粘贴在哈希中。换句话说,我不提前知道用户可能想要什么选项,所以我无法定义所有选项,但我希望能够解析所有选项。

建议?提前谢谢。

【问题讨论】:

    标签: perl getopt-long


    【解决方案1】:

    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.
    

    解析常规选项后,您可以使用 provided by runrig 之类的代码来解析临时选项。

    【讨论】:

      【解决方案2】:

      Getopt::Long 不这样做。您可以自己解析选项...例如

      my %opt;
      my @OPTS = @ARGV;
      for ( @OPTS ) {
        if ( /^--(\w+)=(\w+)$/ ) {
          $opt{$1} = $2;
          shift @ARGV;
        } elsif ( /^--$/ ) {
          shift @ARGV;
          last;
        }
      }
      

      或者修改 Getopt::Long 来处理它(或者修改上面的代码来处理更多种类的选项,如果你需要的话)。

      【讨论】:

      • 我忘记了 Getopt::Long 的 pass_through 选项。这就是你想要的,然后像上面一样解析剩余的 args,如果你愿意,可以添加一个 'else { die "Unknown option: $_\n" }'。
      【解决方案3】:

      我有点偏心,但我过去曾使用 Getopt::Whatever 来解析未知参数。

      【讨论】:

      • 听起来很有趣。它有什么作用?可以和 Getopt::Long pass_through 选项一起使用吗?
      【解决方案4】:

      您可能会使用“Options with hash values”功能。

      例如,我希望允许用户在解析对象数组时设置任意过滤器。

      GetOptions(my $options = {}, 'foo=s', 'filter=s%')
      
      my $filters = $options->{filter};
      

      然后像这样称呼它

      perl ./script.pl --foo bar --filter baz=qux --filter hail=eris
      

      这会构建类似的东西..

      $options = {
                'filter' => {
                              'hail' => 'eris',
                              'baz' => 'qux'
                            },
                'foo' => 'bar'
              };
      

      当然 $filters 会有与 'filter' 相关的值

      祝你好运!我希望有人觉得这很有帮助。

      【讨论】:

        【解决方案5】:

        来自documentation

        Argument Callback

        特殊选项“名称”<> 可用于指定子例程来处理非选项参数。当GetOptions() 遇到一个看起来不像选项的参数时,它会立即调用这个子例程并传递一个参数:参数名称。

        好吧,实际上它是一个字符串化参数名称的对象。

        例如:

            my $width = 80;
            sub process { ... }
            GetOptions ('width=i' => \$width, '<>' => \&process);
        

        当应用于以下命令行时:

            arg1 --width=72 arg2 --width=60 arg3
        

        这将调用process("arg1")$width 是80,process("arg2")$width 是72,process("arg3")$width 是60。

        此功能需要配置选项 permute,请参阅部分 "Configuring Getopt::Long"

        【讨论】:

          【解决方案6】:

          现在是推出您自己的选项解析器的好时机。我在 CPAN 上看到的所有模块都没有提供这种类型的功能,您可以随时查看它们的实现以很好地了解如何处理解析的具体细节。

          顺便说一句,这种类型的代码让我讨厌 Getopt 变体:

          use Getopt::Long;
          &GetOptions(
              'name' => \$value
          );
          

          不一致的大小写是令人抓狂的,即使对于长期看过和使用这种代码风格的人来说也是如此。

          【讨论】:

            猜你喜欢
            • 2016-11-15
            • 1970-01-01
            • 2011-05-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多