[1980] | 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 | # ------------------------------------------------------------------------------ |
---|
| 6 | use strict; |
---|
| 7 | use warnings; |
---|
| 8 | |
---|
| 9 | package Fcm::Interactive; |
---|
| 10 | use base qw{Exporter}; |
---|
| 11 | |
---|
| 12 | our @EXPORT_OK = qw{get_input}; |
---|
| 13 | |
---|
| 14 | use Fcm::Util::ClassLoader; |
---|
| 15 | |
---|
| 16 | my $DEFAULT_IMPL_CLASS = 'Fcm::Interactive::InputGetter::CLI'; |
---|
| 17 | my %DEFAULT_IMPL_CLASS_OPTIONS = (); |
---|
| 18 | |
---|
| 19 | my $IMPL_CLASS = $DEFAULT_IMPL_CLASS; |
---|
| 20 | my %IMPL_CLASS_OPTIONS = %DEFAULT_IMPL_CLASS_OPTIONS; |
---|
| 21 | |
---|
| 22 | ################################################################################ |
---|
| 23 | # Returns the name of the current class/settings for getting input |
---|
| 24 | sub 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 |
---|
| 30 | sub 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 |
---|
| 40 | sub 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 |
---|
| 55 | sub 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 | |
---|
| 63 | 1; |
---|
| 64 | __END__ |
---|
| 65 | |
---|
| 66 | =head1 NAME |
---|
| 67 | |
---|
| 68 | Fcm::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 | |
---|
| 83 | Common interface for getting an interactive user reply. The default is to use a |
---|
| 84 | L<Fcm::Interactive::InputGetter::CLI|Fcm::Interactive::InputGetter::CLI> object |
---|
| 85 | with no extra options. |
---|
| 86 | |
---|
| 87 | =head1 FUNCTIONS |
---|
| 88 | |
---|
| 89 | =over 4 |
---|
| 90 | |
---|
| 91 | =item get_impl() |
---|
| 92 | |
---|
| 93 | Returns the class that implements the function for get_input(%options). In |
---|
| 94 | scalar context, returns the class name only. In list context, returns the class |
---|
| 95 | name and the extra hash options that would be passed to its constructor. |
---|
| 96 | |
---|
| 97 | =item get_default_impl() |
---|
| 98 | |
---|
| 99 | Returns the defaut values for get_impl(). |
---|
| 100 | |
---|
| 101 | =item set_impl($impl_class,$impl_class_options_ref) |
---|
| 102 | |
---|
| 103 | Sets the class that implements the function for get_input(%options). The name |
---|
| 104 | of the class is given in $impl_class. Any extra options that should be given to |
---|
| 105 | the constructor should be set in the hash reference $impl_class_options_ref. |
---|
| 106 | |
---|
| 107 | =item get_input(%options) |
---|
| 108 | |
---|
| 109 | Calls the appropriate function to get an input string from the user, and |
---|
| 110 | returns it. |
---|
| 111 | |
---|
| 112 | Input options are: I<title>, for a short title of the prompt, I<message>, for |
---|
| 113 | the message prompt, I<type> for the prompt type, and I<default> for the default |
---|
| 114 | value of the return value. |
---|
| 115 | |
---|
| 116 | Prompt type can be YN (yes or no), YNA (yes, no or all) or input (for an input |
---|
| 117 | string). |
---|
| 118 | |
---|
| 119 | =back |
---|
| 120 | |
---|
| 121 | =head1 SEE ALSO |
---|
| 122 | |
---|
| 123 | L<Fcm::Interactive::InputGetter|Fcm::Interactive::InputGetter>, |
---|
| 124 | L<Fcm::Interactive::InputGetter::CLI|Fcm::Interactive::InputGetter::CLI>, |
---|
| 125 | L<Fcm::Interactive::InputGetter::GUI|Fcm::Interactive::InputGetter::GUI> |
---|
| 126 | |
---|
| 127 | =head1 COPYRIGHT |
---|
| 128 | |
---|
| 129 | E<169> Crown copyright Met Office. All rights reserved. |
---|
| 130 | |
---|
| 131 | =cut |
---|