File Coverage

blib/lib/MooX/Cmd/ChainedOptions.pm
Criterion Covered Total %
statement 38 38 100.0
branch 7 8 87.5
condition 1 2 50.0
subroutine 10 10 100.0
pod n/a
total 56 58 96.5


line stmt bran cond sub pod time code
1             package MooX::Cmd::ChainedOptions;
2              
3             # ABSTRACT: easily access options from higher up the command chain
4              
5 2     2   195436 use strict;
  2         6  
  2         63  
6 2     2   13 use warnings;
  2         5  
  2         92  
7              
8             our $VERSION = '0.04';
9              
10 2     2   1088 use Import::Into;
  2         3274  
  2         76  
11 2     2   515 use Moo::Role ();
  2         14282  
  2         42  
12 2     2   1165 use MooX::Options ();
  2         2862  
  2         46  
13              
14 2     2   917 use MooX::Cmd::ChainedOptions::Role ();
  2         12  
  2         79  
15 2     2   23 use List::Util qw/ first /;
  2         6  
  2         204  
16              
17 2     2   20 use namespace::clean;
  2         7  
  2         13  
18              
19             my %ROLE;
20              
21             sub import {
22              
23 7     7   458390 shift;
24 7         30 my $target = caller;
25              
26 7 100       63 unless ( $target->DOES( 'MooX::Cmd::Role' ) ) {
27 1         6 require Carp;
28 1         168 Carp::croak( "$target must use MooX::Cmd prior to using ",
29             __PACKAGE__, "\n" );
30             }
31              
32             # don't do this twice
33 6 50       320 return if $ROLE{$target};
34              
35             # load MooX::Options into target class.
36 6         100 MooX::Options->import::into( $target );
37              
38             # guess if an app or a command
39              
40             # if $target is a cmd, a parent class (app or cmd) must have
41             # been loaded. $target must be a direct descendant of a
42             # parent class' command_base. use the _build_command_base method
43             # as it can be used as a class method; command_base is an object method
44              
45 6         71324 my ( $base ) = $target =~ /^(.*)?::([^:]+)$/;
46 6   50     38 $base ||= '';
47 6     10   88 my $parent = first { $base eq $_->_build_command_base } keys %ROLE;
  10         246  
48              
49             $ROLE{$target}
50             = $parent
51             ? MooX::Cmd::ChainedOptions::Role->build_variant( $parent,
52 6 100       200 $ROLE{$parent} )
53             : __PACKAGE__ . '::Base';
54              
55             # need only apply role to commands & subcommands
56 6 100       10663 Moo::Role->apply_roles_to_package( $target, $ROLE{$target} )
57             if $parent;
58              
59 6         6705 return;
60             }
61              
62             1;
63              
64             #
65             # This file is part of MooX-Cmd-ChainedOptions
66             #
67             # This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory.
68             #
69             # This is free software, licensed under:
70             #
71             # The GNU General Public License, Version 3, June 2007
72             #
73              
74             =pod
75              
76             =head1 NAME
77              
78             MooX::Cmd::ChainedOptions - easily access options from higher up the command chain
79              
80             =head1 VERSION
81              
82             version 0.04
83              
84             =head1 SYNOPSIS
85              
86             # MyApp.pm : App Base Class
87             use Moo;
88             use MooX::Cmd;
89             use MooX::Cmd::ChainedOptions;
90              
91             option app_opt => ( is => 'ro', format => 's', default => 'BASE' );
92              
93             sub execute {
94             print $_[0]->app_opt, "\n";
95             }
96              
97             # MyApp/Cmd/cmd.pm : Command Class
98             package MyApp::Cmd::cmd;
99             use Moo;
100             use MooX::Cmd;
101             use MooX::Cmd::ChainedOptions;
102              
103             option cmd_opt => ( is => 'ro', format => 's', default => 'A' );
104              
105             sub execute {
106             print $_[0]->app_opt, "\n";
107             print $_[0]->cmd_opt, "\n";
108             }
109              
110             # MyApp/Cmd/cmd/Cmd/subcmd.pm : Sub-Command Class
111             package MyApp::Cmd::cmd::Cmd::subcmd;
112             use Moo;
113             use MooX::Cmd;
114             use MooX::Cmd::ChainedOptions;
115              
116             option subcmd_opt => ( is => 'ro', format => 's', default => 'B' );
117              
118             sub execute {
119             print $_[0]->app_opt, "\n";
120             print $_[0]->cmd_opt, "\n";
121             print $_[0]->subcmd_opt, "\n";
122             }
123              
124             =head1 DESCRIPTION
125              
126             For applications using L and L,
127             B transparently provides access to command
128             line options from further up the command chain.
129              
130             For example, if an application provides options at each level of the
131             command structure:
132              
133             app --app-opt cmd --cmd-opt subcmd --subcmd-opt
134              
135             The B object will have direct access to the C and
136             C options via object attributes:
137              
138             sub execute {
139             print $self->app_opt, "\n";
140             print $self->cmd_opt, "\n";
141             print $self->subcmd_opt, "\n";
142             }
143              
144             =head1 USAGE
145              
146             Simply
147              
148             use MooX::Cmd::ChainedOptions;
149              
150             instead of
151              
152             use MooX::Options;
153              
154             Every layer in the application hierarchy (application class, command
155             class, sub-command class) must use B. See
156             the L for an example.
157              
158             =head1 BUGS AND LIMITATIONS
159              
160             You can make new bug reports, and view existing ones, through the
161             web interface at L.
162              
163             =head1 AUTHOR
164              
165             Diab Jerius
166              
167             =head1 COPYRIGHT AND LICENSE
168              
169             This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory.
170              
171             This is free software, licensed under:
172              
173             The GNU General Public License, Version 3, June 2007
174              
175             =cut
176              
177             __END__