aboutsummaryrefslogtreecommitdiff
path: root/lib/Sietima/CmdLine.pm
blob: b636533fccb5e815554730d135cbbeee46afbad0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package Sietima::CmdLine; 
use Moo;
use Sietima::Policy;
use Sietima::Types qw(SietimaObj);
use Types::Standard qw(HashRef);
use Sietima;
use App::Spec;
use Sietima::Runner;
use namespace::clean;
 
# VERSION 
# ABSTRACT: run Sietima as a command-line application 
 
=head1 SYNOPSIS
 
  use Sietima::CmdLine;
 
  Sietima::CmdLine->new({
    traits => [qw(SubjectTag)],
    args => {
      return_path => 'list@example.net',
      subject_tag => 'Test',
      subscribers => \@addresses,
  })->run;
 
=head1 DESCRIPTION
 
This class simplifies the creation of a L<< C<Sietima> >> object, and
uses L<< C<App::Spec> >> to provide a command-line interface to it.
 
=attr C<sietima>
 
Required, an instance of L<< C<Sietima> >>. You can either construct
it yourself, or use the L<simplified building provided by the
constructor|/new>.
 
=cut
 
has sietima => (
    is => 'ro',
    required => 1,
    isa => SietimaObj,
);
 
=attr C<extra_spec>
 
Optional hashref. Used inside L<< /C<app_spec> >>. If you're not
familiar with L<< C<App::Spec> >>, you probably don't want to touch
this.
 
=cut
 
has extra_spec => (
    is => 'ro',
    isa => HashRef,
    default => sub { +{} },
);
 
=method C<new>
 
  my $cmdline = Sietima::CmdLine->new({
    sietima => Sietima->with_traits(qw(SubjectTag))->new({
      return_path => 'list@example.net',
      subject_tag => 'Test',
      subscribers => \@addresses,
    }),
  });
 
  my $cmdline = Sietima::CmdLine->new({
    traits => [qw(SubjectTag)],
    args => {
      return_path => 'list@example.net',
      subject_tag => 'Test',
      subscribers => \@addresses,
  });
 
The constructor. In alternative to passing a L<< C<Sietima> >>
instance, you can pass C<traits> and C<args>, and the instance will be
built for you. The two calls above are equivalent.
 
=for Pod::Coverage BUILDARGS
 
=cut
 
sub BUILDARGS($class,@args) {
    my $args = $class->next::method(@args);
    $args->{sietima//do {
        my $traits = delete $args->{traits// [];
        my $constructor_args = delete $args->{args// {};
        Sietima->with_traits($traits->@*)->new($constructor_args);
    };
    return $args;
}
 
=method C<app_spec>
 
Returns an instance of L<< C<App::Spec> >>, built from the
specification returned by calling L<<
C<command_line_spec>|Sietima/command_line_spec >> on the L<<
/C<sietima> >> object, modified by the L<< /C<extra_spec> >>. This
method, and the C<extra_spec> attribute, are probably only interesting
to people who are doing weird extensions.
 
=cut
 
has app_spec => (
    is => 'lazy',
    init_arg => undef,
);
 
sub _build_app_spec($self) {
    my $spec_data = $self->sietima->command_line_spec();
 
    return App::Spec->read({
        $spec_data->%*,
        $self->extra_spec->%*,
    });
}
 
=method C<runner>
 
Returns an instance of L<< C<Sietima::Runner> >>, built from the L<<
/C<app_spec> >>.
 
=method C<run>
 
Delegates to the L<< /C<runner> >>'s L<< C<run>|App::Spec::Run/run >> method.
 
Parser the command line arguments from C<@ARGV> and executes the
appropriate action.
 
=cut
 
has runner => (
    is => 'lazy',
    init_arg => undef,
    handles => [qw(run)],
);
 
sub _build_runner($self) {
    return Sietima::Runner->new({
        spec => $self->app_spec,
        cmd => $self->sietima,
        # App::Spec 0.005 really wants a class name 
        class => ref($self->sietima),
    });
}
 
1;