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.
CLI.pm in vendors/lib/FCM1/Interactive/InputGetter – NEMO

source: vendors/lib/FCM1/Interactive/InputGetter/CLI.pm @ 10669

Last change on this file since 10669 was 10669, checked in by nicolasmartin, 5 years ago

Import latest FCM release from Github into the repository for testing

File size: 2.8 KB
Line 
1# ------------------------------------------------------------------------------
2# (C) British Crown Copyright 2006-17 Met Office.
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::InputGetter::CLI;
23use base qw{FCM1::Interactive::InputGetter};
24
25my $DEF_MSG = q{ (or just press <return> for "%s")};
26my %EXTRA_MSG_FOR = (
27    yn  => qq{\nEnter "y" or "n"},
28    yna => qq{\nEnter "y", "n" or "a"},
29);
30my %CHECKER_FOR = (
31    yn  => sub {$_[0] eq 'y' || $_[0] eq 'n'},
32    yna => sub {$_[0] eq 'y' || $_[0] eq 'n' || $_[0] eq 'a'},
33);
34
35sub invoke {
36    my ($self) = @_;
37    my $type = $self->get_type() ? lc($self->get_type()) : q{};
38    my $message
39        = $self->get_message()
40        . (exists($EXTRA_MSG_FOR{$type}) ? $EXTRA_MSG_FOR{$type} : q{})
41        . ($self->get_default() ? sprintf($DEF_MSG, $self->get_default()) : q{})
42        . q{: }
43        ;
44    while (1) {
45        print($message);
46        my $answer = readline(STDIN);
47        chomp($answer);
48        if (!$answer && $self->get_default()) {
49            $answer = $self->get_default();
50        }
51        if (!exists($CHECKER_FOR{$type}) || $CHECKER_FOR{$type}->($answer)) {
52            return $answer;
53        }
54    }
55    return;
56}
57
581;
59__END__
60
61=head1 NAME
62
63FCM1::Interactive::InputGetter::CLI
64
65=head1 SYNOPSIS
66
67    use FCM1::Interactive;
68    $answer = FCM1::Interactive::get_input(
69        title   => 'My title',
70        message => 'Would you like to ...?',
71        type    => 'yn',
72        default => 'n',
73    );
74
75=head1 DESCRIPTION
76
77This is a solid implementation of
78L<FCM1::Interactive::InputGetter|FCM1::Interactive::InputGetter>. It gets a user
79reply from STDIN using a prompt on STDOUT.
80
81=head1 METHODS
82
83See L<FCM1::Interactive::InputGetter|FCM1::Interactive::InputGetter> for a list of
84methods.
85
86=head1 TO DO
87
88Use IO::Prompt.
89
90=head1 SEE ALSO
91
92L<FCM1::Interactive|FCM1::Interactive>,
93L<FCM1::Interactive::InputGetter|FCM1::Interactive::InputGetter>,
94L<FCM1::Interactive::InputGetter::GUI|FCM1::Interactive::InputGetter::GUI>
95
96=head1 COPYRIGHT
97
98E<169> Crown copyright Met Office. All rights reserved.
99
100=cut
Note: See TracBrowser for help on using the repository browser.