Commit | Line | Data |
33edcaa4 |
1 | package MooseX::Getopt::GLD; |
669588e2 |
2 | # ABSTRACT: A Moose role for processing command line options with Getopt::Long::Descriptive |
3 | |
0611312e |
4 | use MooseX::Role::Parameterized; |
ef47fe44 |
5 | |
669588e2 |
6 | use Getopt::Long::Descriptive 0.081; |
ef47fe44 |
7 | |
33edcaa4 |
8 | with 'MooseX::Getopt::Basic'; |
ef47fe44 |
9 | |
0611312e |
10 | parameter getopt_conf => ( |
11 | isa => 'ArrayRef[Str]', |
12 | default => sub { [] }, |
81b19ed8 |
13 | ); |
14 | |
0611312e |
15 | role { |
16 | |
17 | my $p = shift; |
18 | my $getopt_conf = $p->getopt_conf; |
19 | |
20 | has usage => ( |
21 | is => 'rw', isa => 'Getopt::Long::Descriptive::Usage', |
22 | traits => ['NoGetopt'], |
23 | ); |
24 | |
8d396d8a |
25 | # captures the options: --help --usage --? -? -h |
0611312e |
26 | has help_flag => ( |
27 | is => 'ro', isa => 'Bool', |
28 | traits => ['Getopt'], |
29 | cmd_flag => 'help', |
8d396d8a |
30 | cmd_aliases => [ qw(usage ? h) ], |
0611312e |
31 | documentation => 'Prints this usage information.', |
32 | ); |
33 | |
34 | around _getopt_spec => sub { |
35 | shift; |
36 | shift->_gld_spec(@_); |
37 | }; |
38 | |
39 | around _getopt_get_options => sub { |
40 | shift; |
41 | my ($class, $params, $opt_spec) = @_; |
42 | # Check if a special args hash were already passed, or create a new one |
43 | my $args = ref($opt_spec->[-1]) eq 'HASH' ? pop @$opt_spec : {}; |
44 | unshift @{$args->{getopt_conf}}, @$getopt_conf; |
45 | push @$opt_spec, $args; |
46 | return Getopt::Long::Descriptive::describe_options($class->_usage_format(%$params), @$opt_spec); |
47 | }; |
48 | |
49 | method _gld_spec => sub { |
50 | my ( $class, %params ) = @_; |
51 | |
52 | my ( @options, %name_to_init_arg ); |
53 | |
54 | my $constructor_params = $params{params}; |
55 | |
56 | foreach my $opt ( @{ $params{options} } ) { |
57 | push @options, [ |
58 | $opt->{opt_string}, |
59 | $opt->{doc} || ' ', # FIXME new GLD shouldn't need this hack |
60 | { |
61 | ( ( $opt->{required} && !exists($constructor_params->{$opt->{init_arg}}) ) ? (required => $opt->{required}) : () ), |
62 | # NOTE: |
63 | # remove this 'feature' because it didn't work |
64 | # all the time, and so is better to not bother |
65 | # since Moose will handle the defaults just |
66 | # fine anyway. |
67 | # - SL |
68 | #( exists $opt->{default} ? (default => $opt->{default}) : () ), |
69 | }, |
70 | ]; |
71 | |
72 | my $identifier = lc($opt->{name}); |
73 | $identifier =~ s/\W/_/g; # Getopt::Long does this to all option names |
74 | |
75 | $name_to_init_arg{$identifier} = $opt->{init_arg}; |
76 | } |
77 | |
78 | return ( \@options, \%name_to_init_arg ); |
a2099669 |
79 | } |
0611312e |
80 | }; |
a2099669 |
81 | |
ef47fe44 |
82 | |
669588e2 |
83 | 1; |
ef47fe44 |
84 | |
85 | =head1 SYNOPSIS |
ef47fe44 |
86 | |
33edcaa4 |
87 | ## In your class |
88 | package My::App; |
89 | use Moose; |
ef47fe44 |
90 | |
33edcaa4 |
91 | with 'MooseX::Getopt::GLD'; |
ef47fe44 |
92 | |
a4db695b |
93 | # or |
94 | |
95 | with 'MooseX::Getopt::GLD' => { getopt_conf => [ 'pass_through', ... ] }; |
96 | |
33edcaa4 |
97 | has 'out' => (is => 'rw', isa => 'Str', required => 1); |
98 | has 'in' => (is => 'rw', isa => 'Str', required => 1); |
ef47fe44 |
99 | |
33edcaa4 |
100 | # ... rest of the class here |
ef47fe44 |
101 | |
33edcaa4 |
102 | ## in your script |
103 | #!/usr/bin/perl |
ef47fe44 |
104 | |
33edcaa4 |
105 | use My::App; |
ef47fe44 |
106 | |
33edcaa4 |
107 | my $app = My::App->new_with_options(); |
108 | # ... rest of the script here |
ef47fe44 |
109 | |
33edcaa4 |
110 | ## on the command line |
111 | % perl my_app_script.pl -in file.input -out file.dump |
ef47fe44 |
112 | |
a4db695b |
113 | =head1 OPTIONS |
114 | |
115 | This role is a parameterized role. It accepts one configuration parameter, |
116 | C<getopt_conf>. This parameter is an ArrayRef of strings, which are |
17cdcd8a |
117 | L<Getopt::Long> configuration options (see "Configuring Getopt::Long" in |
a4db695b |
118 | L<Getopt::Long>) |
119 | |
ef47fe44 |
120 | =cut |