0.04
[gitmo/MooseX-Getopt.git] / lib / MooseX / Getopt / OptionTypeMap.pm
CommitLineData
8034a232 1
2package MooseX::Getopt::OptionTypeMap;
3
4use Moose 'confess';
5use Moose::Util::TypeConstraints 'find_type_constraint';
6
1566a17b 7our $VERSION = '0.02';
8034a232 8our $AUTHORITY = 'cpan:STEVAN';
9
10my %option_type_map = (
11 'Bool' => '!',
12 'Str' => '=s',
13 'Int' => '=i',
14 'Float' => '=f',
15 'ArrayRef' => '=s@',
16 'HashRef' => '=s%',
17);
18
f63e6310 19sub has_option_type {
20 my (undef, $type_name) = @_;
21 return 1 if exists $option_type_map{$type_name};
22
23 my $current = find_type_constraint($type_name);
24 while (my $parent = $current->parent) {
25 return 1 if exists $option_type_map{$parent->name};
26 $current = $parent;
27 }
28
29 return 0;
30}
31
32sub get_option_type {
33 my (undef, $type_name) = @_;
34 return $option_type_map{$type_name}
35 if exists $option_type_map{$type_name};
36
37 my $current = find_type_constraint($type_name);
38 while (my $parent = $current->parent) {
39 return $option_type_map{$parent->name}
40 if exists $option_type_map{$parent->name};
41 $current = $parent;
42 }
43
44 return;
45}
46
8034a232 47sub add_option_type_to_map {
48 my (undef, $type_name, $option_string) = @_;
49 (defined $type_name && defined $option_string)
50 || confess "You must supply both a type name and an option string";
51 (find_type_constraint($type_name))
52 || confess "The type constraint '$type_name' does not exist";
53 $option_type_map{$type_name} = $option_string;
54}
55
56no Moose; no Moose::Util::TypeConstraints; 1;
57
58__END__
59
60
61=pod
62
63=head1 NAME
64
65MooseX::Getopt::OptionTypeMap - Storage for the option to type mappings
66
67=head1 DESCRIPTION
68
69See the I<Custom Type Constraints> section in the L<MooseX::Getopt> docs
70for more info about how to use this module.
71
72=head1 METHODS
73
74These are all class methods and should be called as such.
75
76=over 4
77
78=item B<has_option_type ($type_name)>
79
80=item B<get_option_type ($type_name)>
81
82=item B<add_option_type_to_map ($type_name, $option_spec)>
83
84=back
85
86=head1 BUGS
87
88All complex software has bugs lurking in it, and this module is no
89exception. If you find a bug please either email me, or add the bug
90to cpan-RT.
91
92=head1 AUTHOR
93
94Stevan Little E<lt>stevan@iinteractive.comE<gt>
95
96=head1 COPYRIGHT AND LICENSE
97
98Copyright 2007 by Infinity Interactive, Inc.
99
100L<http://www.iinteractive.com>
101
102This library is free software; you can redistribute it and/or modify
103it under the same terms as Perl itself.
104
f63e6310 105=cut