source: LMDZ6/branches/Amaury_dev/tools/fcm/lib/FCM1/Interactive.pm @ 5407

Last change on this file since 5407 was 5129, checked in by abarral, 5 months ago

Re-add removed by mistake fcm

File size: 4.4 KB
Line 
1# ------------------------------------------------------------------------------
2# Copyright (C) 2006-2021 British Crown (Met Office) & Contributors.
3#
4# This file is part of FCM, tools for managing and building source code.
5#
6# FCM is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# FCM is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with FCM. If not, see <http://www.gnu.org/licenses/>.
18# ------------------------------------------------------------------------------
19use strict;
20use warnings;
21
22package FCM1::Interactive;
23use base qw{Exporter};
24
25our @EXPORT_OK = qw{get_input};
26
27use FCM1::Util::ClassLoader;
28
29my $DEFAULT_IMPL_CLASS = 'FCM1::Interactive::InputGetter::CLI';
30my %DEFAULT_IMPL_CLASS_OPTIONS = ();
31
32my $IMPL_CLASS = $DEFAULT_IMPL_CLASS;
33my %IMPL_CLASS_OPTIONS = %DEFAULT_IMPL_CLASS_OPTIONS;
34
35################################################################################
36# Returns the name of the current class/settings for getting input
37sub get_impl {
38    return (wantarray() ? ($IMPL_CLASS, \%IMPL_CLASS_OPTIONS) : $IMPL_CLASS);
39}
40
41################################################################################
42# Returns the name of the current class/settings for getting input
43sub get_default_impl {
44    return (
45        wantarray()
46        ? ($DEFAULT_IMPL_CLASS, \%DEFAULT_IMPL_CLASS_OPTIONS)
47        : $DEFAULT_IMPL_CLASS
48    );
49}
50
51################################################################################
52# Sets the name of the class/settings for getting input
53sub set_impl {
54    my ($impl_class, $impl_class_options_ref) = @_;
55    if ($impl_class) {
56        $IMPL_CLASS = $impl_class;
57        if ($impl_class_options_ref) {
58            %IMPL_CLASS_OPTIONS = (%{$impl_class_options_ref});
59        }
60        else {
61            %IMPL_CLASS_OPTIONS = ();
62        }
63    }
64}
65
66################################################################################
67# Gets an input from the user and returns it
68sub get_input {
69    my (%options) = @_;
70    my ($class_name, $class_options_ref) = get_impl();
71    FCM1::Util::ClassLoader::load($class_name);
72    %options = map {lc($_), $options{$_}} keys(%options);
73    return $class_name->new({%{$class_options_ref}, %options})->invoke();
74}
75
761;
77__END__
78
79=head1 NAME
80
81FCM1::Interactive
82
83=head1 SYNOPSIS
84
85    use FCM1::Interactive;
86    FCM1::Interactive::set_impl('My::InputGetter', {option1 => 'value1', ...});
87    $answer = FCM1::Interactive::get_input(
88        title   => 'My title',
89        message => 'Would you like to ...?',
90        type    => 'yn',
91        default => 'n',
92    );
93
94=head1 DESCRIPTION
95
96Common interface for getting an interactive user reply. The default is to use a
97L<FCM1::Interactive::InputGetter::CLI|FCM1::Interactive::InputGetter::CLI> object
98with no extra options.
99
100=head1 FUNCTIONS
101
102=over 4
103
104=item get_impl()
105
106Returns the class that implements the function for get_input(%options). In
107scalar context, returns the class name only. In list context, returns the class
108name and the extra hash options that would be passed to its constructor.
109
110=item get_default_impl()
111
112Returns the defaut values for get_impl().
113
114=item set_impl($impl_class,$impl_class_options_ref)
115
116Sets the class that implements the function for get_input(%options). The name
117of the class is given in $impl_class. Any extra options that should be given to
118the constructor should be set in the hash reference $impl_class_options_ref.
119
120=item get_input(%options)
121
122Calls the appropriate function to get an input string from the user, and
123returns it.
124
125Input options are: I<title>, for a short title of the prompt, I<message>, for
126the message prompt, I<type> for the prompt type, and I<default> for the default
127value of the return value.
128
129Prompt type can be YN (yes or no), YNA (yes, no or all) or input (for an input
130string).
131
132=back
133
134=head1 SEE ALSO
135
136L<FCM1::Interactive::InputGetter|FCM1::Interactive::InputGetter>,
137L<FCM1::Interactive::InputGetter::CLI|FCM1::Interactive::InputGetter::CLI>,
138L<FCM1::Interactive::InputGetter::GUI|FCM1::Interactive::InputGetter::GUI>
139
140=head1 COPYRIGHT
141
142Copyright (C) 2006-2021 British Crown (Met Office) & Contributors.
143
144=cut
Note: See TracBrowser for help on using the repository browser.