/usr/share/perl5/MooseX/App/ParsedArgv.pm is in libmoosex-app-perl 1.37-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | # ============================================================================
package MooseX::App::ParsedArgv;
# ============================================================================
use 5.010;
use utf8;
use Moose;
use Encode qw(decode);
use MooseX::App::ParsedArgv::Element;
use MooseX::App::ParsedArgv::Value;
no if $] >= 5.018000, warnings => qw(experimental::smartmatch);
my $SINGLETON;
has 'argv' => (
is => 'ro',
isa => 'ArrayRef[Str]',
default => sub {
my @argv;
@argv = eval {
require I18N::Langinfo;
I18N::Langinfo->import(qw(langinfo CODESET));
my $codeset = langinfo(CODESET());
# TODO Not sure if this is the right place?
if ($codeset =~ m/^UTF-?8$/i) {
binmode(STDOUT, ":encoding(UTF-8)");
binmode(STDERR, ":encoding(UTF-8)");
}
return map { decode($codeset,$_) } @ARGV;
};
# Fallback to standard
if ($@) {
@argv = @ARGV;
}
return \@argv;
},
);
has 'hints_novalue' => (
is => 'rw',
isa => 'ArrayRef[Str]',
default => sub { [] },
); # No value hints for the parser (such as for flags)
has 'hints_permute' => (
is => 'rw',
isa => 'ArrayRef[Str]',
default => sub { [] },
); # Permute hints for the parser
has 'hints_fixedvalue' => (
is => 'rw',
isa => 'HashRef[Str]',
default => sub { {} },
); # fixed value hints for the parser
has 'elements' => (
is => 'ro',
isa => 'ArrayRef[MooseX::App::ParsedArgv::Element]',
lazy => 1,
builder => '_build_elements',
clearer => 'reset_elements',
);
sub BUILD {
my ($self) = @_;
# Register singleton
$SINGLETON = $self;
return $self;
}
sub DEMOLISH {
my ($self) = @_;
# Unregister singleton if it is stll the same
$SINGLETON = undef
if defined $SINGLETON
&& $SINGLETON == $self;
}
sub instance {
my ($class) = @_;
unless (defined $SINGLETON) {
return $class->new();
}
return $SINGLETON;
}
sub first_argv {
my ($self) = @_;
$self->reset_elements;
return shift(@{$self->argv});
}
sub _build_elements {
my ($self) = @_;
my (@elements);
my %options;
my $lastkey;
my $lastelement;
my $stopprocessing = 0; # Flag that is set after ' -- ' and inticated end of processing
my $position = 0; # Argument position
my $expecting = 0; # Flag that indicates that a value is expected
# Loop all elements of our ARGV copy
foreach my $element (@{$self->argv}) {
# We are behind first ' -- ' occurrence: Do not process further
if ($stopprocessing) {
push (@elements,MooseX::App::ParsedArgv::Element->new(
key => $element,
type => 'extra',
));
# Process element
} else {
given ($element) {
# Flags with only one leading dash (-h or -vh)
when (m/^-([^-][[:alnum:]]*)$/) {
undef $lastkey;
undef $lastelement;
$expecting = 0;
# Split into single letter flags
foreach my $flag (split(//,$1)) {
unless (defined $options{$flag}) {
$options{$flag} = MooseX::App::ParsedArgv::Element->new(
key => $flag,
type => 'option',
raw => $element,
);
push(@elements,$options{$flag});
}
$options{$flag}->add_value(
1,
$position,
$element,
);
$lastkey = $options{$flag};
$lastelement = $element;
}
}
# Key-value combined (--key=value)
when (m/^--([^-=][^=]*)=(.+)$/) {
undef $lastkey;
undef $lastelement;
$expecting = 0;
my ($key,$value) = ($1,$2);
unless (defined $options{$key}) {
$options{$key} = MooseX::App::ParsedArgv::Element->new(
key => $key,
type => 'option',
raw => $element,
);
push(@elements,$options{$key});
}
$options{$key}->add_value(
$value,
$position,
$element,
);
}
# Ordinary key
when (m/^--?([^-].*)/) {
my $key = $1;
unless (defined $options{$key} ) {
$options{$key} = MooseX::App::ParsedArgv::Element->new(
key => $key,
type => 'option',
raw => $element,
);
push(@elements,$options{$key});
}
# This is a boolean or counter key that does not expect a value
if ($key ~~ $self->hints_novalue) {
$options{$key}->add_value(
($self->hints_fixedvalue->{$key} // 1),
$position,
$element
);
$expecting = 0;
# We are expecting a value
} else {
$expecting = 1;
$lastelement = $element;
$lastkey = $options{$key};
}
}
# Extra values - stop processing after this token
when ('--') {
undef $lastkey;
undef $lastelement;
$stopprocessing = 1;
$expecting = 0;
}
# Value
default {
if (defined $lastkey) {
# This is a parameter - last key was a flag
if ($lastkey->key ~~ $self->hints_novalue) {
push(@elements,MooseX::App::ParsedArgv::Element->new( key => $element, type => 'parameter' ));
undef $lastkey;
undef $lastelement;
$expecting = 0;
# Permute values
} elsif ($lastkey->key ~~ $self->hints_permute) {
$expecting = 0;
$lastkey->add_value(
$element,
$position,
$lastelement
);
# Has value
} else {
$expecting = 0;
$lastkey->add_value($element,$position);
undef $lastkey;
undef $lastelement;
}
} else {
push(@elements,MooseX::App::ParsedArgv::Element->new( key => $element, type => 'parameter' ));
}
}
}
}
$position++;
}
# Fill up last value
if (defined $lastkey
&& $expecting) {
$lastkey->add_value(undef,$position,$lastelement);
$position++;
}
return \@elements;
}
sub available {
my ($self,$type) = @_;
my @elements;
foreach my $element (@{$self->elements}) {
next
if $element->consumed;
next
if defined $type
&& $element->type ne $type;
push(@elements,$element);
}
return @elements;
}
sub consume {
my ($self,$type) = @_;
foreach my $element (@{$self->elements}) {
next
if $element->consumed;
next
if defined $type
&& $element->type ne $type;
$element->consume;
return $element;
}
return;
}
sub extra {
my ($self) = @_;
my @extra;
foreach my $element (@{$self->elements}) {
next
if $element->consumed;
next
unless $element->type eq 'parameter'
|| $element->type eq 'extra';
push(@extra,$element->key);
}
return @extra;
}
__PACKAGE__->meta->make_immutable;
1;
__END__
=pod
=head1 NAME
MooseX::App::ParsedArgv - Parses @ARGV
=head1 SYNOPSIS
use MooseX::App::ParsedArgv;
my $argv = MooseX::App::ParsedArgv->instance;
foreach my $option ($argv->available('option')) {
say "Parsed ".$option->key;
}
=head1 DESCRIPTION
This is a helper class that holds all options parsed from @ARGV. It is
implemented as a singleton. Unless you are developing a MooseX::App plugin
you usually do not need to interact with this class.
=head1 METHODS
=head2 new
Create a new MooseX::App::ParsedArgv instance. Needs to be called as soon
as possible.
=head2 instance
Get the current MooseX::App::ParsedArgv instance. If there is no instance
a new one will be created.
=head2 argv
Accessor for the initinal @ARGV.
=head2 hints
ArrayRef of attributes that tells the parser which attributes should be
regarded as flags without values.
=head2 first_argv
Shifts the current first element from @ARGV.
=head2 available
my @options = $self->available($type);
OR
my @options = $self->available();
Returns an array of all parsed options or parameters that have not yet been consumed.
The array elements will be L<MooseX::App::ParsedArgv::Element> objects.
=head2 consume
my $option = $self->consume($type);
OR
my $option = $self->consume();
Returns the first option/parameter of the local @ARGV that has not yet been
consumed as a L<MooseX::App::ParsedArgv::Element> object.
=head2 elements
Returns all parsed options and parameters.
=head2 extra
Returns an array reference of unconsumed positional parameters and
extra values.
=cut
|