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