English 中文(简体)
我如何利用格洛普特:坚持可能有空间的论点?
原标题:How do I use Getopt::Long to parse arguments that may have spaces?
  • 时间:2010-11-23 14:56:53
  •  标签:
  • perl

我有一只剪辑如下:

prog_name -options ...

例如:

www.un.org/Depts/DGACM/index_arabic.htm - 履历:arg +define BOOST +noconvtest +actuate-long - Disp_arg +define volume=40 res=30

请注意,上面没有引证,我没有控制修改票状。 [然而,我不敢肯定,我可以通过其他一些方案插入上述文yn。]

Now, I want to send the above to a Perl program to parse the options so that it associates +define BOOST +noconvtest +actuate-long to the option -filter_arg and +define size=40 res=30 to the option -disp_arg.

请允许我帮助我这样做吗?

问题回答

<代码>Getopt:Long只是按“Perl”方案提供的@ARGV清单。 如果参数是@ARGV list中的单独条目,Getopt:Long将按此方式分类。 你们的问题是,由于他们没有被援引,因此把每一论点作为单独的参数。

Your choice is to either munge the parameters before your Perl script runs, or by munging @ARGV itself to combine the parameters into the values they re suppose to be in.

在第一种选择中,您可使用sed,以取得您的方案成果并在缺失的引述中添加。 如果你重新规划,就总是在这样的领域:

prog_name -filter_arg +define BOOST +noconvtest +actuate-long -disp_arg +define size=40 res=30

可通过<条码>传送。 类似:

$ orig_prog | sed -e  s/filter_arg /filter_arg "/  -e  s/ -disp_arg /" -disp_arg "/  -e  s/$/"/ 

或者:

$ orig_prog | sed -e  s/^(.*) -filter_arg (.*) -disp_arg (.*)$/1 -filter_arg "2" -disp_arg "3"/ 

这将围绕您的参数进行引证,它照此办理:

prog_name -filter_arg "+define BOOST +noconvtest +actuate-long" -disp_arg "+define size=40 res=30"

That way, @ARGV will be setup correctly so the GetOptions function will work the way you want it to.

另一种方式是,一旦你在请上<代码>GetOptions<> 代码>之前重新实施“Perl”方案,即打上“munge @ARGV:

my $value;
my @newArgv;
foreach my $param (@ARGV) {
   if ($param =~ /^-/) {
   if ($value) {
    push (@newArgv, $value);
    $value = "";
   }
   push(@newArgv, $param);
   } else {
   $value = $value ? "$value $param" : "$param";
   }
}
push (@newArgv, $value) if ($value);
@ARGV = @newArgv;

在上述例子中,@ARGV将具有以下价值:

@ARGV[0] = -filter_arg
@ARGV[1] = +define BOOST +noconvtest +actuate-long
@ARGV[2] = -disp_arg
@ARGV[3] = +define size=40 res=30

Getopts:Long 我们现在应该设法做到这一点。 <><>>> 略微附加说明: 在新版本的<代码>Getopt:Long中,除@ARGV外,再使用其他阵列。 请简单地把你想要使用的阵列作为<代码>GetOptions/code>的第一个论点:

use Getopt::Long qw(GetOptionsFromArray);

GetOptionsFromArray (
    @newArgs,
    "filter_arg=s" => $filter_arg,
    "disp_arg=s"   => $disp_arg,
);

你们可以发出这样的呼吁:

use strict;
use warnings;
use Getopt::Long;

my $options = {};
@ARGV=qw<-filter_arg +define BOOST +noconvtest +actuate-long -disp_arg +define size=40 res=30>;
GetOptions( 
    $options # store in hash ref
    , qw<filter_arg define=s noconvtest actuate-long> 
    , disp_arg => sub { 
        # this will contain "+define size=40 res=30"
        $options->{disp_arg} = join(    , delete @ARGV[0..$#ARGV] );
      }
    );

我不是百分之百的,但我不认为你可以使用“公平”:至少是直接这样做。

您,我想做的是第一件大事。

my @filters;
my $filter = [];

foreach (@ARGV) {
  if ($_ eq  +filter ) {
    push @filters, $filter;
    $filter = [];
  } else {
    push @$filter, $_;
  }
}
push @filters, $filter if @$filter;

foreach (@filters) {
  Getopt::Long::GetOptionsFromArray(@$_, ...

  ...
}

选择:黎明可以按以下方式划分指挥线:

my (@filter_arg, @display_arg);
GetOptions( filter_arg=f{1,5}  => @filter_arg,  display_arg=i{1,5}  => @display_arg);

你们有各种各样的教区。

请参看, for a more verbose interpret of the multi- Valued para amount Hand that s have available.





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签