File Coverage

blib/lib/MooX/Commander/HasSubcommands.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package MooX::Commander::HasSubcommands;
2              
3 1     1   462586 use Moo::Role;
  1         15469  
  1         6  
4 1     1   700 use String::CamelSnakeKebab qw/upper_camel_case/;
  1         9945  
  1         7  
5 1     1   507 use Class::Load qw/load_class/;
  0            
  0            
6              
7             with 'MooX::Commander::HasOptions';
8              
9             has '+argv' => (is => 'rw', required => 1);
10              
11             sub go {
12             my ($self, @args) = @_;
13              
14             my $action = upper_camel_case shift @args || $self->usage;
15             my $class = ref($self) . "::" . $action;
16             eval { load_class($class) };
17             #die $@ if $@;
18             $self->usage if $@;
19              
20             $class->new(argv => $self->argv)->go(@args);
21             die $@ if $@;
22             }
23              
24             1;
25              
26             =encoding utf-8
27              
28             =head1 NAME
29              
30             MooX::Commander::HasSubcommands - Moo role to add subcommands to your command line app
31              
32             =head1 SYNOPSIS
33              
34             # inside lib/PieFactory/Cmd/Recipes.pm:
35             package PieFactory::Cmd::Recipes;
36             use Moo;
37             with 'MooX::Commander::HasSubcommands';
38              
39             usage {
40             return <
41             Subcommands for: piefactory recipes
42              
43             piefactory recipe list List pie recipes
44             piefactory recipe add Display a recipe
45             piefactory recipe delete Add a recipe
46             piefactory recipe show Delete a recipe
47              
48             EOF
49             }
50              
51             # Create these classes the same way you would build any command class.
52             # For details see MooX::Commander and MooX::Commander::HasOptions.
53             # lib/PieFactory/Cmd/Recipes/List.pm
54             # lib/PieFactory/Cmd/Recipes/Show.pm
55             # lib/PieFactory/Cmd/Recipes/Add.pm
56             # lib/PieFactory/Cmd/Recipes/Delete.pm
57              
58              
59             =head1 DESCRIPTION
60              
61             MooX::Commander::HasSubcommands is a simple Moo::Role thats subcommands to your
62             command line application. You can also create sub-subcommands and
63             sub-sub-subcommands, etc.
64              
65             It loads and instantiates the subcommand class the user requested
66             calls the C method on that object. C works the same
67             way here as it does in L -- it prints
68             the usage statement and exits the program unsuccessfuly.
69              
70             =head1 LICENSE
71              
72             Copyright (C) Eric Johnson.
73              
74             This library is free software; you can redistribute it and/or modify
75             it under the same terms as Perl itself.
76              
77             =head1 AUTHOR
78              
79             Eric Johnson Eeric.git@iijo.orgE
80              
81             =cut
82