New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
Interactive.pm in branches/UKMO/dev_r5518_FOAM_local/NEMOGCM/EXTERNAL/fcm/lib/Fcm – NEMO

source: branches/UKMO/dev_r5518_FOAM_local/NEMOGCM/EXTERNAL/fcm/lib/Fcm/Interactive.pm @ 5615

Last change on this file since 5615 was 5615, checked in by cguiavarch, 9 years ago

Clear svn keywords

File size: 3.9 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::Interactive;
10use base qw{Exporter};
11
12our @EXPORT_OK = qw{get_input};
13
14use Fcm::Util::ClassLoader;
15
16my $DEFAULT_IMPL_CLASS = 'Fcm::Interactive::InputGetter::CLI';
17my %DEFAULT_IMPL_CLASS_OPTIONS = ();
18
19my $IMPL_CLASS = $DEFAULT_IMPL_CLASS;
20my %IMPL_CLASS_OPTIONS = %DEFAULT_IMPL_CLASS_OPTIONS;
21
22################################################################################
23# Returns the name of the current class/settings for getting input
24sub get_impl {
25    return (wantarray() ? ($IMPL_CLASS, \%IMPL_CLASS_OPTIONS) : $IMPL_CLASS);
26}
27
28################################################################################
29# Returns the name of the current class/settings for getting input
30sub get_default_impl {
31    return (
32        wantarray()
33        ? ($DEFAULT_IMPL_CLASS, \%DEFAULT_IMPL_CLASS_OPTIONS)
34        : $DEFAULT_IMPL_CLASS
35    );
36}
37
38################################################################################
39# Sets the name of the class/settings for getting input
40sub set_impl {
41    my ($impl_class, $impl_class_options_ref) = @_;
42    if ($impl_class) {
43        $IMPL_CLASS = $impl_class;
44        if ($impl_class_options_ref) {
45            %IMPL_CLASS_OPTIONS = (%{$impl_class_options_ref});
46        }
47        else {
48            %IMPL_CLASS_OPTIONS = ();
49        }
50    }
51}
52
53################################################################################
54# Gets an input from the user and returns it
55sub get_input {
56    my (%options) = @_;
57    my ($class_name, $class_options_ref) = get_impl();
58    Fcm::Util::ClassLoader::load($class_name);
59    %options = map {lc($_), $options{$_}} keys(%options);
60    return $class_name->new({%{$class_options_ref}, %options})->invoke();
61}
62
631;
64__END__
65
66=head1 NAME
67
68Fcm::Interactive
69
70=head1 SYNOPSIS
71
72    use Fcm::Interactive;
73    Fcm::Interactive::set_impl('My::InputGetter', {option1 => 'value1', ...});
74    $answer = Fcm::Interactive::get_input(
75        title   => 'My title',
76        message => 'Would you like to ...?',
77        type    => 'yn',
78        default => 'n',
79    );
80
81=head1 DESCRIPTION
82
83Common interface for getting an interactive user reply. The default is to use a
84L<Fcm::Interactive::InputGetter::CLI|Fcm::Interactive::InputGetter::CLI> object
85with no extra options.
86
87=head1 FUNCTIONS
88
89=over 4
90
91=item get_impl()
92
93Returns the class that implements the function for get_input(%options). In
94scalar context, returns the class name only. In list context, returns the class
95name and the extra hash options that would be passed to its constructor.
96
97=item get_default_impl()
98
99Returns the defaut values for get_impl().
100
101=item set_impl($impl_class,$impl_class_options_ref)
102
103Sets the class that implements the function for get_input(%options). The name
104of the class is given in $impl_class. Any extra options that should be given to
105the constructor should be set in the hash reference $impl_class_options_ref.
106
107=item get_input(%options)
108
109Calls the appropriate function to get an input string from the user, and
110returns it.
111
112Input options are: I<title>, for a short title of the prompt, I<message>, for
113the message prompt, I<type> for the prompt type, and I<default> for the default
114value of the return value.
115
116Prompt type can be YN (yes or no), YNA (yes, no or all) or input (for an input
117string).
118
119=back
120
121=head1 SEE ALSO
122
123L<Fcm::Interactive::InputGetter|Fcm::Interactive::InputGetter>,
124L<Fcm::Interactive::InputGetter::CLI|Fcm::Interactive::InputGetter::CLI>,
125L<Fcm::Interactive::InputGetter::GUI|Fcm::Interactive::InputGetter::GUI>
126
127=head1 COPYRIGHT
128
129E<169> Crown copyright Met Office. All rights reserved.
130
131=cut
Note: See TracBrowser for help on using the repository browser.