Commit | Line | Data |
adbe3e57 |
1 | package MooseX::Getopt::Meta::Attribute::Trait; |
669588e2 |
2 | # ABSTRACT: Optional meta attribute trait for custom option names |
3 | |
adbe3e57 |
4 | use Moose::Role; |
5 | use Moose::Util::TypeConstraints; |
6 | |
adbe3e57 |
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'; |
669588e2 |
16 | |
adbe3e57 |
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 | |
669588e2 |
28 | no Moose::Util::TypeConstraints; |
adbe3e57 |
29 | no Moose::Role; |
30 | |
31 | # register this as a metaclass alias ... |
669588e2 |
32 | package # stop confusing PAUSE |
adbe3e57 |
33 | Moose::Meta::Attribute::Custom::Trait::Getopt; |
34 | sub register_implementation { 'MooseX::Getopt::Meta::Attribute::Trait' } |
35 | |
36 | 1; |
37 | |
adbe3e57 |
38 | =head1 SYNOPSIS |
39 | |
40 | package App; |
41 | use Moose; |
669588e2 |
42 | |
adbe3e57 |
43 | with 'MooseX::Getopt'; |
669588e2 |
44 | |
adbe3e57 |
45 | has 'data' => ( |
669588e2 |
46 | traits => [ 'Getopt' ], |
adbe3e57 |
47 | is => 'ro', |
48 | isa => 'Str', |
49 | default => 'file.dat', |
50 | |
669588e2 |
51 | # tells MooseX::Getopt to use --somedata as the |
52 | # command line flag instead of the normal |
adbe3e57 |
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 | |
669588e2 |
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. |
adbe3e57 |
70 | |
669588e2 |
71 | =method B<cmd_flag> |
adbe3e57 |
72 | |
73 | Changes the commandline flag to be this value, instead of the default, |
74 | which is the same as the attribute name. |
75 | |
669588e2 |
76 | =method B<cmd_aliases> |
adbe3e57 |
77 | |
78 | Adds more aliases for this commandline flag, useful for short options |
79 | and such. |
80 | |
669588e2 |
81 | =method B<has_cmd_flag> |
adbe3e57 |
82 | |
669588e2 |
83 | =method B<has_cmd_aliases> |
adbe3e57 |
84 | |
85 | =cut |