[1980] | 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 | # ------------------------------------------------------------------------------ |
---|
| 6 | use strict; |
---|
| 7 | use warnings; |
---|
| 8 | |
---|
| 9 | package Fcm::CLI::Invoker; |
---|
| 10 | |
---|
| 11 | use Carp qw{croak}; |
---|
| 12 | use Fcm::CLI::Exception; |
---|
| 13 | |
---|
| 14 | ################################################################################ |
---|
| 15 | # Constructor |
---|
| 16 | sub 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 |
---|
| 23 | sub get_command { |
---|
| 24 | my ($self) = @_; |
---|
| 25 | return $self->{command}; |
---|
| 26 | } |
---|
| 27 | |
---|
| 28 | ################################################################################ |
---|
| 29 | # Returns a reference to a hash containing the options |
---|
| 30 | sub 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 |
---|
| 37 | sub get_arguments { |
---|
| 38 | my ($self) = @_; |
---|
| 39 | return (wantarray() ? @{$self->{arguments}} : $self->{arguments}); |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | ################################################################################ |
---|
| 43 | # Invokes the sub-system |
---|
| 44 | sub 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 | |
---|
| 61 | 1; |
---|
| 62 | __END__ |
---|
| 63 | |
---|
| 64 | =head1 NAME |
---|
| 65 | |
---|
| 66 | Fcm::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 | |
---|
| 80 | This is the base class for an invoker of a FCM sub-system from the CLI. |
---|
| 81 | Sub-classes should override the invoke() method. |
---|
| 82 | |
---|
| 83 | =head1 METHODS |
---|
| 84 | |
---|
| 85 | =over 4 |
---|
| 86 | |
---|
| 87 | =item new($args_ref) |
---|
| 88 | |
---|
| 89 | Constructor. It accepts a hash reference as an argument. The element I<command> |
---|
| 90 | should be set to the actual (sub)command as specified by the user. The element |
---|
| 91 | I<options> should be a reference to a hash containing the specified command line |
---|
| 92 | options. The element I<arguments> should be a reference to an array containing |
---|
| 93 | the remaining command line arguments. |
---|
| 94 | |
---|
| 95 | =item get_command() |
---|
| 96 | |
---|
| 97 | Returns the actual (sub)command as specified by the user. |
---|
| 98 | |
---|
| 99 | =item get_options() |
---|
| 100 | |
---|
| 101 | Returns a hash containing the specified command line options. In scalar context, |
---|
| 102 | returns a reference to the hash. |
---|
| 103 | |
---|
| 104 | =item get_arguments() |
---|
| 105 | |
---|
| 106 | Returns an array containing the (remaining) command line arguments. In scalar |
---|
| 107 | context, returns a reference to the array. |
---|
| 108 | |
---|
| 109 | =item invoke() |
---|
| 110 | |
---|
| 111 | Sub-classes should override this method. Calling the method in this base |
---|
| 112 | class causes the system to croak() with a |
---|
| 113 | L<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 | |
---|
| 123 | The C<invoke()> croak() with this exception. |
---|
| 124 | |
---|
| 125 | =back |
---|
| 126 | |
---|
| 127 | =head1 SEE ALSO |
---|
| 128 | |
---|
| 129 | L<Fcm::CLI::Exception|Fcm::CLI::Exception>, |
---|
| 130 | L<Fcm::CLI::Subcommand|Fcm::CLI::Subcommand> |
---|
| 131 | |
---|
| 132 | =head1 COPYRIGHT |
---|
| 133 | |
---|
| 134 | E<169> Crown copyright Met Office. All rights reserved. |
---|
| 135 | |
---|
| 136 | =cut |
---|