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