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