gpt4 book ai didi

linux - MooseX::Getopt 将未处理的 token 放在哪里

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:00:14 24 4
gpt4 key购买 nike

我有这个片段

package AppOpt;
use Moose;

use namespace::autoclean;

with 'MooseX::Getopt';

has opt1 => (is => 'ro', isa => 'Str', required => 1);
has opt2 => (is => 'ro', isa => 'Bool', required => 1);

__PACKAGE__->meta->make_immutable;
1;

package main;
use strict;
use Data::Dumper;

my $cl = [qw( --opt1 OP1 --opt2 -- xxx yyy zzz)];
my $left = [];

my $opt = AppOpt->new_with_options( argv=>$cl, extra_argv => $left);

print Dumper(opt1 => $opt->opt1, opt2 => $opt->opt2, left => $left, cl => $cl);

这给了我这个

$VAR1 = 'opt1';
$VAR2 = 'OP1';
$VAR3 = 'opt2';
$VAR4 = 1;
$VAR5 = 'left';
$VAR6 = [];
$VAR7 = 'cl';
$VAR8 = [
'--opt1',
'OP1',
'--opt2',
'--',
'xxx',
'yyy',
'zzz'
];

如何在不重新处理数组的情况下获取未处理的非基于选项的项,例如 xxx、yyy、zzz?

最佳答案

不需要为 extra_argv 分配一个 arrayref。额外的参数将自动分配给那里的 arrayref。

use Data::Dumper;

my $opt = AppOpt->new_with_options(
argv => [qw( --opt1 OP1 --opt2 -- xxx yyy zzz)]
);

print Dumper $opt->extra_argv;

关于linux - MooseX::Getopt 将未处理的 token 放在哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21037525/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com