0.03
[gitmo/MooseX-Getopt.git] / lib / MooseX / Getopt / Meta / Attribute.pm
1
2 package ;
3 use Moose;
4 use Moose::Util::TypeConstraints;
5
6 our $VERSION   = '0.02';
7 our $AUTHORITY = 'cpan:STEVAN';
8
9 extends 'Moose::Meta::Attribute'; # << Moose extending Moose :)
10
11 has 'cmd_flag' => (
12     is        => 'rw',
13     isa       => 'Str',
14     predicate => 'has_cmd_flag',
15 );
16
17 # This subtype is to support scalar -> arrayref coercion
18 #  without polluting the built-in types
19 subtype '_MooseX_Getopt_CmdAliases'
20     => as 'ArrayRef'
21     => where { 1 };
22     
23 coerce '_MooseX_Getopt_CmdAliases'
24     => from 'Str'
25         => via { [$_] };
26
27 has 'cmd_aliases' => (
28     is        => 'rw',
29     isa       => '_MooseX_Getopt_CmdAliases',
30     predicate => 'has_cmd_aliases',
31     coerce    => 1,
32 );
33
34 no Moose;
35
36 # register this as a metaclass alias ...
37 package Moose::Meta::Attribute::Custom::Getopt;
38 sub register_implementation { 'MooseX::Getopt::Meta::Attribute' }
39
40 1;
41
42 __END__
43
44 =pod
45
46 =head1 NAME
47
48 MooseX::Getopt::Meta::Attribute - Optional meta attribute for custom option names
49
50 =head1 SYNOPSIS
51
52   package App;
53   use Moose;
54   
55   with 'MooseX::Getopt';
56   
57   has 'data' => (
58       metaclass => 'MooseX::Getopt::Meta::Attribute',     
59       is        => 'ro',
60       isa       => 'Str',
61       default   => 'file.dat',
62
63       # tells MooseX::Getopt to use --somedata as the 
64       # command line flag instead of the normal 
65       # autogenerated one (--data)
66       cmd_flag  => 'somedata',
67
68       # tells MooseX::Getopt to also allow --moosedata,
69       # -m, and -d as aliases for this same option on
70       # the commandline.
71       cmd_aliases => [qw/ moosedata m d /],
72
73       # Or, you can use a plain scalar for a single alias:
74       cmd_aliases => 'm',
75   );
76
77 =head1 DESCRIPTION
78
79 This is a custom attribute metaclass which can be used to specify a 
80 the specific command line flag to use instead of the default one 
81 which L<MooseX::Getopt> will create for you. 
82
83 This is certainly not the prettiest way to go about this, but for 
84 now it works for those who might need such a feature.
85
86 =head2 Custom Metaclass alias
87
88 This now takes advantage of the Moose 0.19 feature to support 
89 custom attribute metaclass aliases. This means you can also
90 use this as the B<Getopt> alias, like so:
91
92   has 'foo' => (metaclass => 'Getopt', cmd_flag => 'f');
93
94 =head1 METHODS
95
96 These methods are of little use to most users, they are used interally 
97 within L<MooseX::Getopt>.
98
99 =over 4
100
101 =item B<cmd_flag>
102
103 Changes the commandline flag to be this value, instead of the default,
104 which is the same as the attribute name.
105
106 =item B<cmd_aliases>
107
108 Adds more aliases for this commandline flag, useful for short options
109 and such.
110
111 =item B<has_cmd_flag>
112
113 =item B<has_cmd_aliases>
114
115 =item B<meta>
116
117 =back
118
119 =head1 BUGS
120
121 All complex software has bugs lurking in it, and this module is no 
122 exception. If you find a bug please either email me, or add the bug
123 to cpan-RT.
124
125 =head1 AUTHOR
126
127 Stevan Little E<lt>stevan@iinteractive.comE<gt>
128
129 Brandon L. Black, E<lt>blblack@gmail.comE<gt>
130
131 =head1 COPYRIGHT AND LICENSE
132
133 Copyright 2007 by Infinity Interactive, Inc.
134
135 L<http://www.iinteractive.com>
136
137 This library is free software; you can redistribute it and/or modify
138 it under the same terms as Perl itself.
139
140 =cut