【问题标题】:Perl Getopt::Long Dynamic ParametersPerl Getopt::Long 动态参数
【发布时间】:2011-04-22 23:45:29
【问题描述】:

使用 GetOpt::Long,是否可以创建动态参数列表?

myprog.pl --dir /tmp --force --releaes 1.2.3

 
my %options = (); 
my @options = qw(dir force release );
#note dir and release take argument, and force is a flag my $result = GetOptions(\%options, \@optons); #or something like that print "dir $options{dir} \n"; #produces say /tmp print "force $options{force} \n"; # produces 1 or 0 print "release $options{release} \n"; # and so on

谢谢

【问题讨论】:

  • 当然可以动态构建有效参数列表;所有列表都是动态的,除非您在源代码中修复它们。

标签: perl


【解决方案1】:

这应该可以解决问题:

my @options = qw(dir=s force release=s); 
...
my $result = GetOptions(\%options, @options);

【讨论】:

    【解决方案2】:

    Subs 获取参数的标量列表。该列表可以从任何表达式*生成,包括数组。

    my @options;
    if (condition()) { # Dynamic
       @options = qw( dir=s force release=s );
    } else {
       @options = ...;
    }
    
    GetOptions(\%options, @optons);
    

    * — 原型可以改变允许的表达式以及表达式的计算方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      相关资源
      最近更新 更多