Question

J'ai ce fragment

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);

Ce qui me donne cette

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

Comment puis-je obtenir la transformée de non-option en fonction des éléments tels que xxx, yyy, zzz, sans avoir à retraiter le tableau?

Était-ce utile?

La solution

Il n'est pas nécessaire d'attribuer une arrayref à extra_argv.L'extra args sera affecté à un arrayref, il y a automatiquement.

use Data::Dumper;

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

print Dumper $opt->extra_argv;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top