9f978817ac077306717133d3a094ff32a0ab23a3
[gitmo/MooseX-Getopt.git] / lib / MooseX / Getopt / Meta / Attribute / Trait.pm
1 package MooseX::Getopt::Meta::Attribute::Trait;
2 # ABSTRACT: Optional meta attribute trait for custom option names
3
4 use Moose::Role;
5 use Moose::Util::TypeConstraints;
6
7 has 'cmd_flag' => (
8     is        => 'rw',
9     isa       => 'Str',
10     predicate => 'has_cmd_flag',
11 );
12
13 # This subtype is to support scalar -> arrayref coercion
14 #  without polluting the built-in types
15 subtype '_MooseX_Getopt_CmdAliases' => as 'ArrayRef';
16
17 coerce '_MooseX_Getopt_CmdAliases'
18     => from 'Str'
19         => via { [$_] };
20
21 has 'cmd_aliases' => (
22     is        => 'rw',
23     isa       => '_MooseX_Getopt_CmdAliases',
24     predicate => 'has_cmd_aliases',
25     coerce    => 1,
26 );
27
28 no Moose::Util::TypeConstraints;
29 no Moose::Role;
30
31 # register this as a metaclass alias ...
32 package # stop confusing PAUSE
33     Moose::Meta::Attribute::Custom::Trait::Getopt;
34 sub register_implementation { 'MooseX::Getopt::Meta::Attribute::Trait' }
35
36 1;
37
38 =head1 SYNOPSIS
39
40   package App;
41   use Moose;
42
43   with 'MooseX::Getopt';
44
45   has 'data' => (
46       traits    => [ 'Getopt' ],
47       is        => 'ro',
48       isa       => 'Str',
49       default   => 'file.dat',
50
51       # tells MooseX::Getopt to use --somedata as the
52       # command line flag instead of the normal
53       # autogenerated one (--data)
54       cmd_flag  => 'somedata',
55
56       # tells MooseX::Getopt to also allow --moosedata,
57       # -m, and -d as aliases for this same option on
58       # the commandline.
59       cmd_aliases => [qw/ moosedata m d /],
60
61       # Or, you can use a plain scalar for a single alias:
62       cmd_aliases => 'm',
63   );
64
65 =head1 DESCRIPTION
66
67 This is a custom attribute metaclass trait which can be used to
68 specify a the specific command line flag to use instead of the
69 default one which L<MooseX::Getopt> will create for you.
70
71 =method B<cmd_flag>
72
73 Changes the commandline flag to be this value, instead of the default,
74 which is the same as the attribute name.
75
76 =method B<cmd_aliases>
77
78 Adds more aliases for this commandline flag, useful for short options
79 and such.
80
81 =method B<has_cmd_flag>
82
83 =method B<has_cmd_aliases>
84
85 =cut