2014-03-31 32 views
-1

我使用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

你能提供一个你想达到什么的简短代码示例吗? – hlovdal

+1

如果您希望我们知道您的意思,您需要更具体地了解“格式”和“修改”的含义。 – TLP

+0

我添加了一个例子。 –

回答

1

否,Getopt::Long简称为 “解析来自@ARGV命令行中,识别和去除指定的选项”。它不会执行任何格式的选项。

如果要保留所有的选项传递给你的程序,可以使原数组的副本调用GetOptions前:

my @opts = @ARGV; 
GetOptions(...) 
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); 
+0

不是。 Getopt :: Long提供了很多选项,只需遍历%ARG键和值,并且说'-key = val'不适用于除最基本配置外的所有配置。我在上面添加了一个例子。如果我错了,请纠正我,或者你能否给我提供你可以使用的代码,如果不是'foreach $'键(键%ARG)' –

+0

我提供的代码。 – ikegami

相关问题