File Coverage

blib/lib/Sietima/CmdLine.pm
Criterion Covered Total %
statement 45 45 100.0
branch 5 10 50.0
condition 5 7 71.4
subroutine 11 11 100.0
pod 0 1 0.0
total 66 74 89.1


line stmt bran cond sub pod time code
1             package Sietima::CmdLine;
2 1     1   16020 use Moo;
  1         5  
  1         15  
3 1     1   563 use Sietima::Policy;
  1         5  
  1         15  
4 1     1   11 use Sietima::Types qw(SietimaObj);
  1         4  
  1         15  
5 1     1   785 use Types::Standard qw(HashRef);
  1         4  
  1         10  
6 1     1   1105 use Sietima;
  1         4  
  1         37  
7 1     1   763 use App::Spec;
  1         38474  
  1         119  
8 1     1   735 use Sietima::Runner;
  1         8  
  1         64  
9 1     1   15 use namespace::clean;
  1         4  
  1         15  
10              
11             our $VERSION = '1.0.3'; # VERSION
12             # ABSTRACT: run Sietima as a command-line application
13              
14              
15             has sietima => (
16             is => 'ro',
17             required => 1,
18             isa => SietimaObj,
19             );
20              
21              
22             has extra_spec => (
23             is => 'ro',
24             isa => HashRef,
25             default => sub { +{} },
26             );
27              
28              
29 4 50   4 0 30694 sub BUILDARGS($class,@args) {
  4         13  
  4         20  
  4         11  
30 4         73 my $args = $class->next::method(@args);
31 4   66     135 $args->{sietima} //= do {
32 3   100     20 my $traits = delete $args->{traits} // [];
33 3   50     12 my $constructor_args = delete $args->{args} // {};
34 3         21 Sietima->with_traits($traits->@*)->new($constructor_args);
35             };
36 4         7624 return $args;
37             }
38              
39              
40             has app_spec => (
41             is => 'lazy',
42             init_arg => undef,
43             );
44              
45 2 50   2   1553 sub _build_app_spec($self) {
  2 50       9  
  2         5  
  2         4  
46 2         18 my $spec_data = $self->sietima->command_line_spec();
47              
48 2         34 return App::Spec->read({
49             $spec_data->%*,
50             $self->extra_spec->%*,
51             });
52             }
53              
54              
55             has runner => (
56             is => 'lazy',
57             init_arg => undef,
58             handles => [qw(run)],
59             );
60              
61 1 50   1   9790 sub _build_runner($self) {
  1 50       4  
  1         2  
  1         3  
62 1         18 return Sietima::Runner->new({
63             spec => $self->app_spec,
64             cmd => $self->sietima,
65             });
66             }
67              
68             1;
69              
70             __END__
71              
72             =pod
73              
74             =encoding UTF-8
75              
76             =head1 NAME
77              
78             Sietima::CmdLine - run Sietima as a command-line application
79              
80             =head1 VERSION
81              
82             version 1.0.3
83              
84             =head1 SYNOPSIS
85              
86             use Sietima::CmdLine;
87              
88             Sietima::CmdLine->new({
89             traits => [qw(SubjectTag)],
90             args => {
91             return_path => 'list@example.net',
92             subject_tag => 'Test',
93             subscribers => \@addresses,
94             })->run;
95              
96             =head1 DESCRIPTION
97              
98             This class simplifies the creation of a L<< C<Sietima> >> object, and
99             uses L<< C<App::Spec> >> to provide a command-line interface to it.
100              
101             =head1 ATTRIBUTES
102              
103             =head2 C<sietima>
104              
105             Required, an instance of L<< C<Sietima> >>. You can either construct
106             it yourself, or use the L<simplified building provided by the
107             constructor|/new>.
108              
109             =head2 C<extra_spec>
110              
111             Optional hashref. Used inside L<< /C<app_spec> >>. If you're not
112             familiar with L<< C<App::Spec> >>, you probably don't want to touch
113             this.
114              
115             =head1 METHODS
116              
117             =head2 C<new>
118              
119             my $cmdline = Sietima::CmdLine->new({
120             sietima => Sietima->with_traits(qw(SubjectTag))->new({
121             return_path => 'list@example.net',
122             subject_tag => 'Test',
123             subscribers => \@addresses,
124             }),
125             });
126              
127             my $cmdline = Sietima::CmdLine->new({
128             traits => [qw(SubjectTag)],
129             args => {
130             return_path => 'list@example.net',
131             subject_tag => 'Test',
132             subscribers => \@addresses,
133             });
134              
135             The constructor. In alternative to passing a L<< C<Sietima> >>
136             instance, you can pass C<traits> and C<args>, and the instance will be
137             built for you. The two calls above are equivalent.
138              
139             =head2 C<app_spec>
140              
141             Returns an instance of L<< C<App::Spec> >>, built from the
142             specification returned by calling L<<
143             C<command_line_spec>|Sietima/command_line_spec >> on the L<<
144             /C<sietima> >> object, modified by the L<< /C<extra_spec> >>. This
145             method, and the C<extra_spec> attribute, are probably only interesting
146             to people who are doing weird extensions.
147              
148             =head2 C<runner>
149              
150             Returns an instance of L<< C<Sietima::Runner> >>, built from the L<<
151             /C<app_spec> >>.
152              
153             =head2 C<run>
154              
155             Delegates to the L<< /C<runner> >>'s L<< C<run>|App::Spec::Run/run >> method.
156              
157             Parser the command line arguments from C<@ARGV> and executes the
158             appropriate action.
159              
160             =for Pod::Coverage BUILDARGS
161              
162             =head1 AUTHOR
163              
164             Gianni Ceccarelli <dakkar@thenautilus.net>
165              
166             =head1 COPYRIGHT AND LICENSE
167              
168             This software is copyright (c) 2017 by Gianni Ceccarelli <dakkar@thenautilus.net>.
169              
170             This is free software; you can redistribute it and/or modify it under
171             the same terms as the Perl 5 programming language system itself.
172              
173             =cut