source: branches/2015/dev_r5021_UKMO1_CICE_coupling/NEMOGCM/EXTERNAL/fcm/lib/Fcm/CLI/Invoker.pm @ 5445

Last change on this file since 5445 was 5445, checked in by davestorkey, 5 years ago

Clear SVN keywords from 2015/dev_r5021_UKMO1_CICE_coupling branch.

File size: 3.6 KB
Line 
1# ------------------------------------------------------------------------------
2# (C) Crown copyright Met Office. All rights reserved.
3# For further details please refer to the file COPYRIGHT.txt
4# which you should have received as part of this distribution.
5# ------------------------------------------------------------------------------
6use strict;
7use warnings;
8
9package Fcm::CLI::Invoker;
10
11use Carp qw{croak};
12use Fcm::CLI::Exception;
13
14################################################################################
15# Constructor
16sub new {
17    my ($class, $args_ref) = @_;
18    return bless({%{$args_ref}}, $class);
19}
20
21################################################################################
22# Returns the name of the (sub)command as given by the user
23sub get_command {
24    my ($self) = @_;
25    return $self->{command};
26}
27
28################################################################################
29# Returns a reference to a hash containing the options
30sub get_options {
31    my ($self) = @_;
32    return (wantarray() ? %{$self->{options}} : $self->{options});
33}
34
35################################################################################
36# Returns a reference to an array containing the arguments
37sub get_arguments {
38    my ($self) = @_;
39    return (wantarray() ? @{$self->{arguments}} : $self->{arguments});
40}
41
42################################################################################
43# Invokes the sub-system
44sub invoke {
45    my ($self) = @_;
46    my $message = "command not implemented\n";
47    $message .= sprintf("opts:");
48    for my $key (sort keys(%{$self->get_options()})) {
49        my $value = $self->get_options()->{$key};
50        $message .= sprintf(
51            " [%s=%s]",
52            $key,
53            ($value && ref($value) eq 'ARRAY' ? join(q{, }, @{$value}) : $value)
54        );
55    }
56    $message .= sprintf("\n");
57    $message .= sprintf("args: [%s]\n", join(q{] [}, $self->get_arguments()));
58    croak(Fcm::CLI::Exception->new({message => $message}));
59}
60
611;
62__END__
63
64=head1 NAME
65
66Fcm::CLI::Invoker
67
68=head1 SYNOPSIS
69
70    use Fcm::CLI::Invoker;
71    $invoker = Fcm::CLI::Invoker->new({
72        command   => $command,
73        options   => \%options,
74        arguments => $arguments,
75    });
76    $invoker->invoke();
77
78=head1 DESCRIPTION
79
80This is the base class for an invoker of a FCM sub-system from the CLI.
81Sub-classes should override the invoke() method.
82
83=head1 METHODS
84
85=over 4
86
87=item new($args_ref)
88
89Constructor. It accepts a hash reference as an argument. The element I<command>
90should be set to the actual (sub)command as specified by the user. The element
91I<options> should be a reference to a hash containing the specified command line
92options. The element I<arguments> should be a reference to an array containing
93the remaining command line arguments.
94
95=item get_command()
96
97Returns the actual (sub)command as specified by the user.
98
99=item get_options()
100
101Returns a hash containing the specified command line options. In scalar context,
102returns a reference to the hash.
103
104=item get_arguments()
105
106Returns an array containing the (remaining) command line arguments. In scalar
107context, returns a reference to the array.
108
109=item invoke()
110
111Sub-classes should override this method. Calling the method in this base
112class causes the system to croak() with a
113L<Fcm::CLI::Exception|Fcm::CLI::Exception>.
114
115=back
116
117=head1 DIAGNOSTICS
118
119=over 4
120
121=item L<Fcm::CLI::Exception|Fcm::CLI::Exception>
122
123The C<invoke()> croak() with this exception.
124
125=back
126
127=head1 SEE ALSO
128
129L<Fcm::CLI::Exception|Fcm::CLI::Exception>,
130L<Fcm::CLI::Subcommand|Fcm::CLI::Subcommand>
131
132=head1 COPYRIGHT
133
134E<169> Crown copyright Met Office. All rights reserved.
135
136=cut
Note: See TracBrowser for help on using the repository browser.