2 package MooseX::Getopt::Session;
6 use MooseX::Getopt::Parser::Long;
7 use maybe 'MooseX::Getopt::Parser::Descriptive';
11 # Pluggined MooseX::Getopt::Parser parser
12 has 'getopt_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
67 has 'die_on_warning' => (
75 ### MooseX::Getopt::Session::BUILD : @_
76 my ($self, $args) = @_;
85 my @attrs = map { $_->_compute_getopt_attrs } $self->_compute_getopt_classes;
86 ### MooseX::Getopt::Session::build_options @attrs -> name : map { $_->name } @attrs
88 return $self->getopt_parser->build_options( $self, @attrs );
92 sub _compute_getopt_classes {
96 $self->classes_filter->()
98 $_->isa('Moose::Object') && $_->does('MooseX::Getopt')
99 } Class::MOP->get_all_metaclasses;
103 sub _get_cmd_flags_for_attr {
104 my ($self, $attr) = @_;
106 my $flag = $attr->name;
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;
115 return ($flag, @aliases);
119 sub _get_cmd_type_for_attr {
120 my ($self, $attr) = @_;
124 $type = $attr->type_constraint if $attr->has_type_constraint;
126 if ($attr->does('MooseX::Getopt::Meta::Attribute::Trait')) {
127 $type = $attr->cmd_type if $attr->has_cmd_type;
135 my ($self, $string) = @_;
137 return $self->warning( ($self->has_warning ? $self->warning : '') . $string );