Chain.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 88.4% 23 / 26 CC 2 SCAR 2.9
CC
2
Cov
85%
BEGIN
2.0
BEGIN
1.3
BEGIN
1.0
CRAP 1.3
« Chain/v005/Carp.pm ↑ Summary Chain/Builder.pm »
1 package Getopt::Chain;
2  
3 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 11 ▶use constant DEBUG => $ENV{GOC_TRACE} ? 1 : 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'GOC_TRACE'} ? : 0 11 11
7 our $DEBUG = DEBUG;
8  
9 =head1 NAME
10  
11 Getopt::Chain - Command-line processing like svn and git
12  
13 =head1 VERSION
14  
15 Version 0.013_1
16  
17 =cut
18  
19 our $VERSION = '0.013_1';
20  
21 =head1 SYNPOSIS
22  
23 package My::Command;
24  
25 use Getopt::Chain::Declare;
26  
27 start [qw/ verbose|v /]; # These are "global"
28 # my-command --verbose initialize ...
29  
30 # my-command ? initialize ... --> my-command help initialize ...
31 rewrite qr/^\?(.*)/ => sub { "help ".($1||'') };
32  
33 # NOTE: Rewriting applies to the command sequence, NOT options
34  
35 # my-command about ... --> my-command help about
36 rewrite [ ['about', 'copying'] ] => sub { "help $1" };
37  
38 # my-command initialize --dir=...
39 on initialize => [qw/ dir|d=s /], sub {
40 my $context = shift;
41  
42 my $dir = $context->option( 'dir' )
43  
44 # Do initialize stuff with $dir
45 };
46  
47 # my-command help
48 on help => undef, sub {
49 my $context = shift;
50  
51 # Do help stuff ...
52 # First argument is undef because help
53 # doesn't take any options
54 };
55  
56 under help => sub {
57  
58 # my-command help create
59 # my-command help initialize
60 on [ [ qw/create initialize/ ] ] => undef, sub {
61 my $context = shift;
62  
63 # Do help for create/initialize
64 # Both: "help create" and "help initialize" go here
65 };
66  
67 # my-command help about
68 on 'about' => undef, sub {
69 my $context = shift;
70  
71 # Help for about...
72 };
73  
74 # my-command help copying
75 on 'copying' => undef, sub {
76 my $context = shift;
77  
78 # Help for copying...
79 };
80  
81 # my-command help ...
82 on qr/^(\S+)$/ => undef, sub {
83 my $context = shift;
84 my $topic = $1;
85  
86 # Catch-all for anything not fitting into the above...
87  
88 warn "I don't know about \"$topic\"\n"
89 };
90 };
91  
92 # ... elsewhere ...
93  
94 My::Command->new->run( [ @arguments ] )
95 My::Command->new->run # Just run with @ARGV
96  
97 =head1 DESCRIPTION
98  
99 Getopt::Chain can be used to provide C<svn(1)>- and C<git(1)>-style option and command processing. Any option specification
100 covered by L<Getopt::Long> is fair game.
101  
102 This is a new version of Getopt::Chain that uses L<Path::Dispatcher>
103  
104 CAVEAT 1: This is pretty beta, so the sugar/interface above WILL be tweaked
105  
106 CAVEAT 2: Unfortunately, Getopt::Long slurps up the entire arguments array at once. Usually, this isn't a problem (as Getopt::Chain uses
107 pass_through). However, if a subcommand has an option with the same name or alias as an option for a parent, then that option won't be available
108 for the subcommand. For example:
109  
110 ./script --verbose --revision 36 edit --revision 48 --file xyzzy.c
111 # Getopt::Chain will not associate the second --revision with "edit"
112  
113 So, for now, try to use distinct option names/aliases :)
114  
115 DEBUG: You can get some extra information about what Getopt::Chain is doing by setting the environment variable C<GOC_TRACE> to 1
116  
117 =head1 LEGACY
118  
119 The old-style, non L<Path::Dispatcher> version is still available at L<Getopt::Chain::v005>
120  
121 =cut
122  
123 11 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
124 11 ▶use Getopt::Chain::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
125  
126 11 ▶use Getopt::Chain::Builder;
Subroutine BEGIN: called CC=1 SCAR=2.6
127 use Getopt::Chain::Context;
128  
129 has builder => qw/is ro lazy_build 1/, handles => [qw/ dispatcher /];
130 sub _build_builder {
131 return Getopt::Chain::Builder->new;
132 }
133  
134 has context_from => qw/is ro isa Str|CodeRef lazy_build 1/;
135 sub _build_context_from {
136 return 'Getopt::Chain::Context';
137 }
138  
139 sub process {
140 if (! ref $_[0] && $_[0] && $_[0] eq 'Getopt::Chain') {
141 shift;
142 require Getopt::Chain::v005;
143 carp "Deprecated: Use Getopt::Chain::v005->process( ... ) to avoid this warning (this method will be removed in a future version)";
144 return Getopt::Chain::v005->process( @_ );
145 }
146 }
147  
148 sub run {
149 if (! ref $_[0] ) {
150 croak "Can't call run on the package; use ->new first";
151 }
152 my $self = shift;
153 my $arguments = shift;
154  
155 $arguments = [ @ARGV ] unless $arguments;
156  
157 my $context = $self->new_context( dispatcher => $self->dispatcher, arguments => $arguments );
158 $context->run;
159 return $context->options;
160 }
161  
162 sub new_context {
163 my $self = shift;
164  
165 my $context_from = $self->context_from;
166 if (! ref $context_from) {
167 return $context_from->new( @_ );
168 }
169 else {
170 croak "Don't understand context source \"$context_from\"";
171 }
172 }
173  
174 use MooseX::MakeImmutable;
175 MooseX::MakeImmutable->lock_down;
176  
177 =head1 SEE ALSO
178  
179 L<Getopt::Long>
180  
181 L<App::Cmd>
182  
183 L<MooseX::App::Cmd>
184  
185 =head1 ACKNOWLEDGEMENTS
186  
187 Sartak for L<Path::Dispatcher>
188  
189 obra for inspiration on the CLI (via Prophet & Sd: L<http://syncwith.us/>)
190  
191 =head1 AUTHOR
192  
193 Robert Krimen, C<< <rkrimen at cpan.org> >>
194  
195 =head1 SOURCE
196  
197 You can contribute or fork this project via GitHub:
198  
199 L<http://github.com/robertkrimen/Getopt-Chain/tree/master>
200  
201 git clone git://github.com/robertkrimen/Getopt-Chain.git Getopt-Chain
202  
203 =head1 BUGS
204  
205 Please report any bugs or feature requests to C<bug-Getopt-Chain at rt.cpan.org>, or through
206 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Getopt-Chain>. I will be notified, and then you'll
207 automatically be notified of progress on your bug as I make changes.
208  
209  
210  
211  
212 =head1 SUPPORT
213  
214 You can find documentation for this module with the perldoc command.
215  
216 perldoc Getopt::Chain
217  
218  
219 You can also look for information at:
220  
221 =over 4
222  
223 =item * RT: CPAN's request tracker
224  
225 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Getopt-Chain>
226  
227 =item * AnnoCPAN: Annotated CPAN documentation
228  
229 L<http://annocpan.org/dist/Getopt-Chain>
230  
231 =item * CPAN Ratings
232  
233 L<http://cpanratings.perl.org/d/Getopt-Chain>
234  
235 =item * Search CPAN
236  
237 L<http://search.cpan.org/dist/Getopt-Chain>
238  
239 =back
240  
241  
242 =head1 COPYRIGHT & LICENSE
243  
244 Copyright 2008 Robert Krimen, all rights reserved.
245  
246 This program is free software; you can redistribute it and/or modify it
247 under the same terms as Perl itself.
248  
249  
250 =cut
251  
252 1; # End of Getopt::Chain
« Chain/v005/Carp.pm ↑ Summary Chain/Builder.pm »