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