source: XMLIO_V2/tools/FCM/lib/Fcm/Interactive/InputGetter/CLI.pm @ 81

Last change on this file since 81 was 81, checked in by ymipsl, 15 years ago

ajout FCM 1.5

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