File: Element.pm

package info (click to toggle)
libmoosex-app-perl 1.43-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 712 kB
  • sloc: perl: 4,011; makefile: 2
file content (163 lines) | stat: -rw-r--r-- 3,357 bytes parent folder | download | duplicates (4)
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
# ============================================================================
package MooseX::App::ParsedArgv::Element;
# ============================================================================

use 5.010;
use utf8;

use Moose;

has 'key' => (
    is              => 'ro',
    isa             => 'Str',
    required        => 1,
);

has 'value' => (
    is              => 'ro',
    isa             => 'ArrayRef[MooseX::App::ParsedArgv::Value]',
    traits          => ['Array'],
    default         => sub { [] },
    handles         => {
        push_value      => 'push',
        count_values    => 'count',
        list_values     => 'elements',
    }
);

has 'consumed' => (
    is              => 'rw',
    isa             => 'Bool',
    default         => sub {0},
);

has 'type' => (
    is              => 'ro',
    isa             => 'Str',
    required        => 1,
);


sub original {
    my ($self) = @_;

    # TODO fixthis
    if ($self->count_values && $self->value->[-1]->has_raw) {
        return $self->value->[-1]->has_raw;
    } else {
        return $self->key;
    }
}

sub add_value {
    my ($self,$value,$position,$raw) = @_;

    $self->push_value(MooseX::App::ParsedArgv::Value->new(
        value       => $value,
        (defined $position ? (position => $position):()),
        (defined $raw ? (raw => $raw):()),
    ));
    return $value;
}

sub all_scalar_values { ## no perlcritic(RequireArgUnpacking)
    return map { $_->value }
        $_[0]->all_values;
}

sub all_values { ## no perlcritic(RequireArgUnpacking)
    return sort { $a->position <=> $b->position }
        $_[0]->list_values;
}

sub last_value {
    my ($self) = @_;
    return ($self->all_values)[-1];
}

sub consume {
    my ($self,$attribute) = @_;

    Moose->throw_error('Element '.$self->type.' '.$self->key.' is already consumed')
        if $self->consumed;
    $self->consumed(1);

    return $self;
}

sub serialize {
    my ($self) = @_;
    my $type = $self->type;
    if ($type eq 'extra') {
        return $self->key;
    } elsif ($type eq 'parameter') {
        return $self->key;
    } elsif ($type eq 'option') {
        my $key = (length $self->key == 1 ? '-':'--').$self->key;
        return join(' ',map { $key.' '.$_->value } $self->all_values);
    }

    return;
}

__PACKAGE__->meta->make_immutable();
1;

=pod

=head1 NAME

MooseX::App::ParsedArgv::Element - Parsed logical element from @ARGV

=head1 DESCRIPTION

Every instance of this class represents a logical entity from @ARGV

=head1 METHODS

=head2 key

Parameter value or option key

=head2 value

Arrayref of values. A value is represented by a L<MooseX::App::ParsedArgv::Value>
object.

=head2 add_value

Append a value

=head2 all_scalar_values

All values as scalars (in supplied order)

=head2 all_values

All values as L<MooseX::App::ParsedArgv::Value> objects (in supplied order)

=head2 last_value

Last value as L<MooseX::App::ParsedArgv::Value> object (as given by supplied order)

=head2 type

Type of element. Can be 'option', 'parameter' or 'extra'

=head2 consumed

Flag that indicates if element was already consumed

=head2 consume

Consumes element. Dies if element is already consumed

=head2 serialize

Serializes element (Does not procuce output that is identical with original @ARGV)

=head2 original

Tries to re-create the original input

=cut