Commit | Line | Data |
c6c1f628 |
1 | |
2 | package MooseX::Getopt::Session; |
3 | |
4 | use Moose; |
5 | |
6 | use MooseX::Getopt::Parser::Long; |
7 | use maybe 'MooseX::Getopt::Parser::Descriptive'; |
8 | |
9 | #use Smart::Comments; |
10 | |
11 | # Pluggined MooseX::Getopt::Parser parser |
19b87ede |
12 | has 'parser' => ( |
c6c1f628 |
13 | is => 'rw', |
14 | does => 'MooseX::Getopt::Parser', |
15 | default => sub { |
16 | maybe::HAVE_MOOSEX_GETOPT_PARSER_DESCRIPTIVE |
17 | ? MooseX::Getopt::Parser::Descriptive->new |
18 | : MooseX::Getopt::Parser::Long->new |
19 | }, |
20 | ); |
21 | |
22 | # Filter for classes which are searched for getopt trait |
23 | has 'classes_filter' => ( |
24 | is => 'rw', |
25 | isa => 'CodeRef', |
26 | default => sub { sub { 1 } }, |
27 | ); |
28 | |
053fa19e |
29 | # Explicite parameters for new_with_options |
30 | has 'params' => ( |
31 | is => 'rw', |
32 | isa => 'HashRef', |
33 | default => sub { {} }, |
34 | ); |
35 | |
c6c1f628 |
36 | # Original @ARGV values |
37 | has 'argv' => ( |
38 | is => 'rw', |
39 | isa => 'ArrayRef[Str]', |
40 | auto_deref => 1, |
41 | default => sub { [ @ARGV ] }, |
42 | ); |
43 | |
44 | # Unrecognized @ARGV values |
45 | has 'extra_argv' => ( |
46 | is => 'rw', |
47 | isa => 'ArrayRef[Str]', |
48 | auto_deref => 1, |
49 | default => sub { [] }, |
50 | ); |
51 | |
52 | # Hash with options parsed from argv |
53 | has 'options' => ( |
54 | is => 'rw', |
55 | isa => 'HashRef', |
56 | auto_deref => 1, |
57 | default => sub { {} }, |
58 | ); |
59 | |
60 | # Status of parser |
61 | has 'status' => ( |
62 | is => 'rw', |
63 | isa => 'Bool', |
64 | ); |
65 | |
66 | # Warnings collected by parser |
67 | has 'warning' => ( |
68 | is => 'rw', |
69 | isa => 'Str', |
70 | predicate => 'has_warning', |
71 | ); |
72 | |
73 | # Die if warnings was occured |
c6c1f628 |
74 | sub BUILD { |
75 | ### MooseX::Getopt::Session::BUILD : @_ |
76 | my ($self, $args) = @_; |
77 | |
78 | $self->build_options; |
79 | }; |
80 | |
81 | |
82 | sub build_options { |
83 | my ($self) = @_; |
84 | |
85 | my @attrs = map { $_->_compute_getopt_attrs } $self->_compute_getopt_classes; |
86 | ### MooseX::Getopt::Session::build_options @attrs -> name : map { $_->name } @attrs |
87 | |
19b87ede |
88 | return $self->parser->build_options( $self, @attrs ); |
c6c1f628 |
89 | } |
90 | |
91 | |
92 | sub _compute_getopt_classes { |
93 | my $self = shift; |
94 | |
95 | return grep { |
96 | $self->classes_filter->() |
97 | } grep { |
98 | $_->isa('Moose::Object') && $_->does('MooseX::Getopt') |
99 | } Class::MOP->get_all_metaclasses; |
100 | }; |
101 | |
102 | |
5b582f22 |
103 | sub _get_cmd_flags_for_attr { |
104 | my ($self, $attr) = @_; |
105 | |
106 | my $flag = $attr->name; |
107 | |
108 | my @aliases; |
109 | |
110 | if ($attr->does('MooseX::Getopt::Meta::Attribute::Trait')) { |
111 | $flag = $attr->cmd_flag if $attr->has_cmd_flag; |
112 | @aliases = @{ $attr->cmd_aliases } if $attr->has_cmd_aliases; |
113 | }; |
114 | |
115 | return ($flag, @aliases); |
116 | }; |
117 | |
118 | |
119 | sub _get_cmd_type_for_attr { |
120 | my ($self, $attr) = @_; |
121 | |
122 | my $type; |
123 | |
124 | $type = $attr->type_constraint if $attr->has_type_constraint; |
125 | |
126 | if ($attr->does('MooseX::Getopt::Meta::Attribute::Trait')) { |
127 | $type = $attr->cmd_type if $attr->has_cmd_type; |
128 | }; |
129 | |
130 | return $type; |
131 | }; |
132 | |
133 | |
dd012666 |
134 | sub strcat_warning { |
135 | my ($self, $string) = @_; |
136 | |
137 | return $self->warning( ($self->has_warning ? $self->warning : '') . $string ); |
138 | }; |
139 | |
140 | |
c6c1f628 |
141 | 1; |