2 package MooseX::Getopt::Session;
6 use MooseX::Getopt::Parser::Long;
7 use maybe 'MooseX::Getopt::Parser::Descriptive';
11 # Pluggined MooseX::Getopt::Parser parser
14 does => 'MooseX::Getopt::Parser',
16 maybe::HAVE_MOOSEX_GETOPT_PARSER_DESCRIPTIVE
17 ? MooseX::Getopt::Parser::Descriptive->new
18 : MooseX::Getopt::Parser::Long->new
22 # Filter for classes which are searched for getopt trait
23 has 'classes_filter' => (
26 default => sub { sub { 1 } },
29 # Original @ARGV values
32 isa => 'ArrayRef[Str]',
34 default => sub { [ @ARGV ] },
37 # Unrecognized @ARGV values
40 isa => 'ArrayRef[Str]',
42 default => sub { [] },
45 # Hash with options parsed from argv
50 default => sub { {} },
59 # Warnings collected by parser
63 predicate => 'has_warning',
66 # Die if warnings was occured
68 ### MooseX::Getopt::Session::BUILD : @_
69 my ($self, $args) = @_;
78 my @attrs = map { $_->_compute_getopt_attrs } $self->_compute_getopt_classes;
79 ### MooseX::Getopt::Session::build_options @attrs -> name : map { $_->name } @attrs
81 return $self->parser->build_options( $self, @attrs );
85 sub _compute_getopt_classes {
89 $self->classes_filter->()
91 $_->isa('Moose::Object') && $_->does('MooseX::Getopt')
92 } Class::MOP->get_all_metaclasses;
96 sub _get_cmd_flags_for_attr {
97 my ($self, $attr) = @_;
99 my $flag = $attr->name;
103 if ($attr->does('MooseX::Getopt::Meta::Attribute::Trait')) {
104 $flag = $attr->cmd_flag if $attr->has_cmd_flag;
105 @aliases = @{ $attr->cmd_aliases } if $attr->has_cmd_aliases;
108 return ($flag, @aliases);
112 sub _get_cmd_type_for_attr {
113 my ($self, $attr) = @_;
117 $type = $attr->type_constraint if $attr->has_type_constraint;
119 if ($attr->does('MooseX::Getopt::Meta::Attribute::Trait')) {
120 $type = $attr->cmd_type if $attr->has_cmd_type;
128 my ($self, $string) = @_;
130 return $self->warning( ($self->has_warning ? $self->warning : '') . $string );