[10672] | 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 | # ------------------------------------------------------------------------------ |
---|
| 19 | use strict; |
---|
| 20 | use warnings; |
---|
| 21 | |
---|
| 22 | package FCM1::Interactive; |
---|
| 23 | use base qw{Exporter}; |
---|
| 24 | |
---|
| 25 | our @EXPORT_OK = qw{get_input}; |
---|
| 26 | |
---|
| 27 | use FCM1::Util::ClassLoader; |
---|
| 28 | |
---|
| 29 | my $DEFAULT_IMPL_CLASS = 'FCM1::Interactive::InputGetter::CLI'; |
---|
| 30 | my %DEFAULT_IMPL_CLASS_OPTIONS = (); |
---|
| 31 | |
---|
| 32 | my $IMPL_CLASS = $DEFAULT_IMPL_CLASS; |
---|
| 33 | my %IMPL_CLASS_OPTIONS = %DEFAULT_IMPL_CLASS_OPTIONS; |
---|
| 34 | |
---|
| 35 | ################################################################################ |
---|
| 36 | # Returns the name of the current class/settings for getting input |
---|
| 37 | sub 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 |
---|
| 43 | sub 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 |
---|
| 53 | sub 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 |
---|
| 68 | sub 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 | |
---|
| 76 | 1; |
---|
| 77 | __END__ |
---|
| 78 | |
---|
| 79 | =head1 NAME |
---|
| 80 | |
---|
| 81 | FCM1::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 | |
---|
| 96 | Common interface for getting an interactive user reply. The default is to use a |
---|
| 97 | L<FCM1::Interactive::InputGetter::CLI|FCM1::Interactive::InputGetter::CLI> object |
---|
| 98 | with no extra options. |
---|
| 99 | |
---|
| 100 | =head1 FUNCTIONS |
---|
| 101 | |
---|
| 102 | =over 4 |
---|
| 103 | |
---|
| 104 | =item get_impl() |
---|
| 105 | |
---|
| 106 | Returns the class that implements the function for get_input(%options). In |
---|
| 107 | scalar context, returns the class name only. In list context, returns the class |
---|
| 108 | name and the extra hash options that would be passed to its constructor. |
---|
| 109 | |
---|
| 110 | =item get_default_impl() |
---|
| 111 | |
---|
| 112 | Returns the defaut values for get_impl(). |
---|
| 113 | |
---|
| 114 | =item set_impl($impl_class,$impl_class_options_ref) |
---|
| 115 | |
---|
| 116 | Sets the class that implements the function for get_input(%options). The name |
---|
| 117 | of the class is given in $impl_class. Any extra options that should be given to |
---|
| 118 | the constructor should be set in the hash reference $impl_class_options_ref. |
---|
| 119 | |
---|
| 120 | =item get_input(%options) |
---|
| 121 | |
---|
| 122 | Calls the appropriate function to get an input string from the user, and |
---|
| 123 | returns it. |
---|
| 124 | |
---|
| 125 | Input options are: I<title>, for a short title of the prompt, I<message>, for |
---|
| 126 | the message prompt, I<type> for the prompt type, and I<default> for the default |
---|
| 127 | value of the return value. |
---|
| 128 | |
---|
| 129 | Prompt type can be YN (yes or no), YNA (yes, no or all) or input (for an input |
---|
| 130 | string). |
---|
| 131 | |
---|
| 132 | =back |
---|
| 133 | |
---|
| 134 | =head1 SEE ALSO |
---|
| 135 | |
---|
| 136 | L<FCM1::Interactive::InputGetter|FCM1::Interactive::InputGetter>, |
---|
| 137 | L<FCM1::Interactive::InputGetter::CLI|FCM1::Interactive::InputGetter::CLI>, |
---|
| 138 | L<FCM1::Interactive::InputGetter::GUI|FCM1::Interactive::InputGetter::GUI> |
---|
| 139 | |
---|
| 140 | =head1 COPYRIGHT |
---|
| 141 | |
---|
| 142 | E<169> Crown copyright Met Office. All rights reserved. |
---|
| 143 | |
---|
| 144 | =cut |
---|