简体   繁体   English

在perl的系统功能中使用命令行中的可选参数

[英]Using optional arguments from command line within the system function in perl

I am generalizing a pre-existing perl script... ie making it work on numerous instances simultaneously. 我正在概括一个预先存在的perl脚本...即使其同时在多个实例上工作。 Basically it is a parameter scan feature of a modelling script. 基本上,它是建模脚本的参数扫描功能。 The scan does a single parameter and I want it to do them all. 扫描只做一个参数,我希望它能全部完成。 The pre-existing script (called scan_var.pl from bionetgen if your interested) has a bunch of arguments - some of them optional. 预先存在的脚本(如果您有兴趣,可从bionetgen称为scan_var.pl)具有一堆参数-其中一些是可选的。

The following is successful in implementing the parameter scan with the default settings 以下成功使用默认设置实现了参数扫描

#execute the parameter scan with each variable individually

foreach $var_name (@var_names){

    my $param = shift @var_names;
    system ("perl", $scan_var_location, $model, $param, $min_value, $max_value, $NPTS);
}

But now I want to use the GetOpt::Long module to parse in the optional arguments. 但是现在我想使用GetOpt::Long模块来解析可选参数。 My code so far is: 到目前为止,我的代码是:

# some default parameters
my $log     = 0;
my $t_end   = 20;
my $n_steps = 1;
my $steady_state = 0;
my $method = "\"ode\"";
my $verbose = 0;
my $prefix;

my $options = GetOptions ( 
                           'verbose'      => \$verbose, #boolean
                           'log'          => \$log, #boolean
                           'n_steps:i'    => \$n_steps, #integer
                           'steady_state' => \$steady_state, #boolean
                           'method:s'     => \$method, #string
                           't_end:i'      => \$t_end, #integer
                           'prefix:s'     => \$prefix string
                         );

#execute the parameter scan with each variable individually

foreach $var_name (@var_names){ #iterates through a list stored in $var_names(not shown for concise-ness)

    my $param = shift @var_names;

    system ("perl", $scan_var_location, #required value, directory
                    $options, #optional command line arguments - corresponds to the list above
                    $model, #required command line value (directory)
                    $param, #list iterated over
                    $min_value, #requierd integer
                    $max_value, #required integer
                    $NPTS #required integer
           );
}

This however is somehow incorrect. 但是,这是不正确的。 Does anybody have any suggestions for corrections? 有人对纠正有任何建议吗?

Cheers 干杯

GetOptions() supports the storage of values in a hash. GetOptions()支持将值存储在哈希中。 This allows you to reduce variable clutter: 这使您可以减少混乱的情况:

use Getopt::Long;

my %options;
GetOptions( \%options,
                       'verbose',
                           'log',
                     'n_steps:i',
                  'steady_state',
                      'method:s',
                       't_end:i',
                      'prefix:s',
          );

my $stringified_options = join ' ', map "-$_ $options{$_}", keys %options;

foreach my $var_name ( @var_name ) {

    system ("perl", $scan_var_location,
                    $stringified_options,
                    $model,
                    $param, 
                    $min_value,
                    $max_value,
                    $NPTS
           );
}

One problem is that you probably don't want to use the return value of GetOptions in your system call. 一个问题是您可能不想在system调用中使用GetOptions返回值 $options has the return value: $options具有返回值:

GetOptions returns true to indicate success. GetOptions返回true表示成功。 It returns false when the function detected one or more errors during option parsing. 当函数在选项解析期间检测到一个或多个错误时,它将返回false。

The following is a tighter version of your code: 以下是您的代码的严格版本:

GetOptions ( 
    'verbose'      => \(my $verbose = 0),        #boolean
    'log'          => \(my $log     = 0),        #boolean
    'n_steps:i'    => \(my $n_steps = 1),        #integer
    'steady_state' => \(my $steady_state = 0),   #boolean
    'method:s'     => \(my $method = q{"ode"}),  #string
    't_end:i'      => \(my $t_end   = 20),       #integer
    'prefix:s'     => \my $prefix,               #string
);

#execute the parameter scan with each variable individually

#iterates through a list stored in $var_names(not shown for concise-ness)
foreach $var_name (@var_names) { 
    system("perl", 
        $scan_var_location,   #required value, directory
        $model,               #required command line value (directory)
        $var_name,            #list iterated over
        $min_value,           #requierd integer
        $max_value,           #required integer
        $NPTS,                #required integer
    );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM