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