File Coverage

blib/lib/Sietima/CmdLine.pm
Criterion Covered Total %
statement 43 43 100.0
branch n/a
condition 5 7 71.4
subroutine 11 11 100.0
pod 0 1 0.0
total 59 62 95.1


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