perl – 如何使用GetOpt :: Long :: GetOptions解析字符串?

我有一个带有可能的命令行参数的字符串(使用Read-Eval-Print-Loop程序),我希望它在传递给Getopt :: Long时被解析为类似于命令行参数.

详细说明:

我有一个字符串

$str = '--infile /tmp/infile_location --outfile /tmp/outfile'

我希望它由Getoptions解析,以便我更容易添加新选项.

我能想到的一个解决方法是将字符串拆分为空格,并用新数组替换@ARGV,然后调用Getoptions.就像是 …

my @arg_arr = split (/\s/,$input_line);

# This is done so that Getoptions reads these new arguments
@ARGV = @arg_arr;
print "ARGV is : @ARGV\n";
Getoptions (
            'infile=s'  => \$infile,'outfile=s' => \$outfile
           );

有没有好/更好的方法

解决方法

查看 man page for Getopt::Long中的 parsing options from an arbitrary string部分,我认为它完全符合您的要求.

相关文章

1. 如何去重 #!/usr/bin/perl use strict; my %hash; while(...
最近写了一个perl脚本,实现的功能是将表格中其中两列的数据...
表的数据字典格式如下:如果手动写MySQL建表语句,确认麻烦,...
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了...
在实际生产环境中,常常需要从后台日志中截取报文,报文的形...
最近写的一个perl程序,通过关键词匹配统计其出现的频率,让...