Invoker.pm 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. package Fcm::CLI::Invoker;
  9. use Carp qw{croak};
  10. use Fcm::CLI::Exception;
  11. ################################################################################
  12. # Constructor
  13. sub new {
  14. my ($class, $args_ref) = @_;
  15. return bless({%{$args_ref}}, $class);
  16. }
  17. ################################################################################
  18. # Returns the name of the (sub)command as given by the user
  19. sub get_command {
  20. my ($self) = @_;
  21. return $self->{command};
  22. }
  23. ################################################################################
  24. # Returns a reference to a hash containing the options
  25. sub get_options {
  26. my ($self) = @_;
  27. return (wantarray() ? %{$self->{options}} : $self->{options});
  28. }
  29. ################################################################################
  30. # Returns a reference to an array containing the arguments
  31. sub get_arguments {
  32. my ($self) = @_;
  33. return (wantarray() ? @{$self->{arguments}} : $self->{arguments});
  34. }
  35. ################################################################################
  36. # Invokes the sub-system
  37. sub invoke {
  38. my ($self) = @_;
  39. my $message = "command not implemented\n";
  40. $message .= sprintf("opts:");
  41. for my $key (sort keys(%{$self->get_options()})) {
  42. my $value = $self->get_options()->{$key};
  43. $message .= sprintf(
  44. " [%s=%s]",
  45. $key,
  46. ($value && ref($value) eq 'ARRAY' ? join(q{, }, @{$value}) : $value)
  47. );
  48. }
  49. $message .= sprintf("\n");
  50. $message .= sprintf("args: [%s]\n", join(q{] [}, $self->get_arguments()));
  51. croak(Fcm::CLI::Exception->new({message => $message}));
  52. }
  53. 1;
  54. __END__
  55. =head1 NAME
  56. Fcm::CLI::Invoker
  57. =head1 SYNOPSIS
  58. use Fcm::CLI::Invoker;
  59. $invoker = Fcm::CLI::Invoker->new({
  60. command => $command,
  61. options => \%options,
  62. arguments => $arguments,
  63. });
  64. $invoker->invoke();
  65. =head1 DESCRIPTION
  66. This is the base class for an invoker of a FCM sub-system from the CLI.
  67. Sub-classes should override the invoke() method.
  68. =head1 METHODS
  69. =over 4
  70. =item new($args_ref)
  71. Constructor. It accepts a hash reference as an argument. The element I<command>
  72. should be set to the actual (sub)command as specified by the user. The element
  73. I<options> should be a reference to a hash containing the specified command line
  74. options. The element I<arguments> should be a reference to an array containing
  75. the remaining command line arguments.
  76. =item get_command()
  77. Returns the actual (sub)command as specified by the user.
  78. =item get_options()
  79. Returns a hash containing the specified command line options. In scalar context,
  80. returns a reference to the hash.
  81. =item get_arguments()
  82. Returns an array containing the (remaining) command line arguments. In scalar
  83. context, returns a reference to the array.
  84. =item invoke()
  85. Sub-classes should override this method. Calling the method in this base
  86. class causes the system to croak() with a
  87. L<Fcm::CLI::Exception|Fcm::CLI::Exception>.
  88. =back
  89. =head1 DIAGNOSTICS
  90. =over 4
  91. =item L<Fcm::CLI::Exception|Fcm::CLI::Exception>
  92. The C<invoke()> croak() with this exception.
  93. =back
  94. =head1 SEE ALSO
  95. L<Fcm::CLI::Exception|Fcm::CLI::Exception>,
  96. L<Fcm::CLI::Subcommand|Fcm::CLI::Subcommand>
  97. =head1 COPYRIGHT
  98. E<169> Crown copyright Met Office. All rights reserved.
  99. =cut