使用Getopt :: Long解析格式化选项以传递给另一个程序

时间:2014-03-31 12:28:32

标签: perl getopt-long

我使用Getopt :: Long来解析传递给我程序的选项。我想格式化这些选项(修改后)以传递给另一个程序。

Getopt是否可以执行此操作,或者是否有可能为我执行此操作的其他模块?

示例:

use Getopt::Long qw(:config no_ignore_case );

# set defaults
my %ARG;
$ARG{config} = './default-config.ini';
$ARG{debug} = 0;

# process command line options
GetOptions( \%ARG, 'config|c=s', 'debug!');

# modify them as necessary
if ( if $ARG{debug} ) {
   $ARG{config} = './debug-config.ini' ;
   $ARG{debug} = 0;
   $ARG{verbal} = 1;
}

# format options string to pass to other command

# expecting something like this in options string:

# '-config=./debug-config.ini --verbal'

$options_string = some_method_or_module_to_format( %ARG, 'config=s', 'verbal' );


`some-other-script-1.pl $options_string`;

`some-other-script-2.pl $options_string`;

`some-other-script-3.pl $options_string`;

2 个答案:

答案 0 :(得分:1)

不,Getopt::Long只是"从@ARGV解析命令行,识别并删除指定的选项"。它没有对选项进行任何格式化。

如果要保留传递给程序的所有选项,可以在调用GetOptions之前复制原始数组:

my @opts = @ARGV;
GetOptions( ... )

答案 1 :(得分:0)

  

我想格式化这些选项(修改后)以传递给另一个程序。 Getopt是否可以执行此操作,或者是否有可以为我执行此操作的其他模块?

指示模块如何执行它所花费的时间与指示Perl如何执行它一样长。不需要这样的模块。

my @args;
push @args, "--config", $ARG{config} if defined($ARG{config});
push @args, "--verbal"               if $ARG{verbal};

my $args = shell_quote(@args);
相关问题