Config.pm 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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::Config;
  9. use Fcm::CLI::Config::Default;
  10. use List::Util qw{first};
  11. use Scalar::Util qw{blessed};
  12. my $INSTANCE;
  13. ################################################################################
  14. # Class method: returns an instance of this class
  15. sub instance {
  16. my ($class, $args_ref) = @_;
  17. if ($args_ref || !$INSTANCE) {
  18. $INSTANCE = bless({
  19. core_subcommands => [@Fcm::CLI::Config::Default::CORE_SUBCOMMANDS],
  20. vc_subcommands => [@Fcm::CLI::Config::Default::VC_SUBCOMMANDS],
  21. (defined($args_ref) ? %{$args_ref} : ()),
  22. }, $class);
  23. }
  24. return $INSTANCE;
  25. }
  26. ################################################################################
  27. # Returns a subcommand matching $key
  28. sub get_subcommand_of {
  29. my ($self, $key) = @_;
  30. if (blessed($key) && $key->isa('Fcm::CLI::Subcommand')) {
  31. return first {"$_" eq "$key"} ($self->get_subcommands());
  32. }
  33. else {
  34. return first {$_->has_a_name($key)} ($self->get_subcommands());
  35. }
  36. }
  37. ################################################################################
  38. # Returns the subcommands
  39. sub get_subcommands {
  40. my ($self) = @_;
  41. my @return = ($self->get_core_subcommands(), $self->get_vc_subcommands());
  42. return (wantarray() ? @return : \@return);
  43. }
  44. ################################################################################
  45. # Returns the core subcommands
  46. sub get_core_subcommands {
  47. my ($self) = @_;
  48. return (
  49. wantarray() ? @{$self->{core_subcommands}} : $self->{core_subcommands}
  50. );
  51. }
  52. ################################################################################
  53. # Returns the subcommands that are relevant only with a VC system
  54. sub get_vc_subcommands {
  55. my ($self) = @_;
  56. return (wantarray() ? @{$self->{vc_subcommands}} : $self->{vc_subcommands});
  57. }
  58. 1;
  59. __END__
  60. =head1 NAME
  61. Fcm::CLI::Config
  62. =head1 SYNOPSIS
  63. use Fcm::CLI::Config;
  64. $cli_config = Fcm::CLI::Config->instance();
  65. $subcommand = $cli_config->get_subcommand_of($key);
  66. @subcommands = $cli_config->get_subcommands();
  67. @core_subcommands = $cli_config->get_core_subcommands();
  68. @vc_subcommands = $cli_config->get_vc_subcommands();
  69. =head1 DESCRIPTION
  70. This class provides the configuration of the FCM command line interface.
  71. =head1 METHODS
  72. =over 4
  73. =item instance($arg_ref)
  74. Returns an instance of this class.
  75. Creates the instance on first call, or replaces it with a new one if $args_ref
  76. is defined in subsequent call. $args_ref should be a reference to a hash. The
  77. hash can contain I<core_subcommands> and I<vc_subcommands>. Each of these
  78. settings should point to an array reference containing L<Fcm::CLI::Subcommand>
  79. objects. If the setting is unspecified, it uses the default from
  80. L<Fcm::CLI::Config::Default|Fcm::CLI::Config::Default>.
  81. =item get_subcommand_of($key)
  82. Returns a L<Fcm::CLI::Subcommand|Fcm::CLI::Subcommand> object matching the
  83. search $key. Returns undef if there is no match.
  84. =item get_subcommands()
  85. Short-hand for:
  86. ($self->get_core_subcommands(), $self->get_vc_subcommands())
  87. =item get_core_subcommands()
  88. Returns the core subcommands.
  89. =item get_vc_subcommands()
  90. Returns the subcommands that are relevant only in the presence of a VC system.
  91. =back
  92. =head1 SEE ALSO
  93. L<Fcm::CLI::Config::Default|Fcm::CLI::Config::Default>,
  94. L<Fcm::CLI::Invoker|Fcm::CLI::Invoker>,
  95. L<Fcm::CLI::Subcommand|Fcm::CLI::Subcommand>,
  96. L<Fcm::CLI::Option|Fcm::CLI::Option>
  97. =head1 COPYRIGHT
  98. E<169> Crown copyright Met Office. All rights reserved.
  99. =cut