Perl 使用Getopt::Long解析的格式选项将传递给另一个程序
我正在使用Getopt::Long解析传递给程序的选项。我想格式化这些选项(修改后)以传递给另一个程序 Getopt是否可以这样做,或者是否有其他模块可以为我这样做 例如:Perl 使用Getopt::Long解析的格式选项将传递给另一个程序,perl,getopt-long,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 GetO
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`;
不,只需“从@ARGV
解析命令行,识别并删除指定的选项”。它不会对选项进行任何格式化
如果要保留传递给程序的所有选项,可以在调用GetOptions
之前复制原始数组:
my @opts = @ARGV;
GetOptions( ... )
我想格式化这些选项(修改后)以传递给另一个程序。Getopt是否可以这样做,或者是否有其他模块可以为我这样做
指示一个模块如何操作所需的时间与指示Perl如何操作所需的时间一样长。不需要这样的模块
my @args;
push @args, "--config", $ARG{config} if defined($ARG{config});
push @args, "--verbal" if $ARG{verbal};
my $args = shell_quote(@args);
您能提供一个简短的代码示例来说明您想要实现的目标吗?如果您想让我们知道您的意思,您必须更具体地说明“格式”和“修改”的含义。将参数解析为字符串并将其提供给另一个Perl脚本是相当麻烦和脆弱的。更好的选择是将脚本模块化,并将代码导入主脚本中。Getopt::Long提供了很多选项,只需迭代%ARG键和值并说“-key=val”对所有配置都不起作用,但最基本的配置除外。我在上面添加了一个示例。如果我错了,请纠正我,或者如果不只是一个
foreach$key-in(keys%ARG)