Interactive.pm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. package Fcm::Interactive;
  9. use base qw{Exporter};
  10. our @EXPORT_OK = qw{get_input};
  11. use Fcm::Util::ClassLoader;
  12. my $DEFAULT_IMPL_CLASS = 'Fcm::Interactive::InputGetter::CLI';
  13. my %DEFAULT_IMPL_CLASS_OPTIONS = ();
  14. my $IMPL_CLASS = $DEFAULT_IMPL_CLASS;
  15. my %IMPL_CLASS_OPTIONS = %DEFAULT_IMPL_CLASS_OPTIONS;
  16. ################################################################################
  17. # Returns the name of the current class/settings for getting input
  18. sub get_impl {
  19. return (wantarray() ? ($IMPL_CLASS, \%IMPL_CLASS_OPTIONS) : $IMPL_CLASS);
  20. }
  21. ################################################################################
  22. # Returns the name of the current class/settings for getting input
  23. sub get_default_impl {
  24. return (
  25. wantarray()
  26. ? ($DEFAULT_IMPL_CLASS, \%DEFAULT_IMPL_CLASS_OPTIONS)
  27. : $DEFAULT_IMPL_CLASS
  28. );
  29. }
  30. ################################################################################
  31. # Sets the name of the class/settings for getting input
  32. sub set_impl {
  33. my ($impl_class, $impl_class_options_ref) = @_;
  34. if ($impl_class) {
  35. $IMPL_CLASS = $impl_class;
  36. if ($impl_class_options_ref) {
  37. %IMPL_CLASS_OPTIONS = (%{$impl_class_options_ref});
  38. }
  39. else {
  40. %IMPL_CLASS_OPTIONS = ();
  41. }
  42. }
  43. }
  44. ################################################################################
  45. # Gets an input from the user and returns it
  46. sub get_input {
  47. my (%options) = @_;
  48. my ($class_name, $class_options_ref) = get_impl();
  49. Fcm::Util::ClassLoader::load($class_name);
  50. %options = map {lc($_), $options{$_}} keys(%options);
  51. return $class_name->new({%{$class_options_ref}, %options})->invoke();
  52. }
  53. 1;
  54. __END__
  55. =head1 NAME
  56. Fcm::Interactive
  57. =head1 SYNOPSIS
  58. use Fcm::Interactive;
  59. Fcm::Interactive::set_impl('My::InputGetter', {option1 => 'value1', ...});
  60. $answer = Fcm::Interactive::get_input(
  61. title => 'My title',
  62. message => 'Would you like to ...?',
  63. type => 'yn',
  64. default => 'n',
  65. );
  66. =head1 DESCRIPTION
  67. Common interface for getting an interactive user reply. The default is to use a
  68. L<Fcm::Interactive::InputGetter::CLI|Fcm::Interactive::InputGetter::CLI> object
  69. with no extra options.
  70. =head1 FUNCTIONS
  71. =over 4
  72. =item get_impl()
  73. Returns the class that implements the function for get_input(%options). In
  74. scalar context, returns the class name only. In list context, returns the class
  75. name and the extra hash options that would be passed to its constructor.
  76. =item get_default_impl()
  77. Returns the defaut values for get_impl().
  78. =item set_impl($impl_class,$impl_class_options_ref)
  79. Sets the class that implements the function for get_input(%options). The name
  80. of the class is given in $impl_class. Any extra options that should be given to
  81. the constructor should be set in the hash reference $impl_class_options_ref.
  82. =item get_input(%options)
  83. Calls the appropriate function to get an input string from the user, and
  84. returns it.
  85. Input options are: I<title>, for a short title of the prompt, I<message>, for
  86. the message prompt, I<type> for the prompt type, and I<default> for the default
  87. value of the return value.
  88. Prompt type can be YN (yes or no), YNA (yes, no or all) or input (for an input
  89. string).
  90. =back
  91. =head1 SEE ALSO
  92. L<Fcm::Interactive::InputGetter|Fcm::Interactive::InputGetter>,
  93. L<Fcm::Interactive::InputGetter::CLI|Fcm::Interactive::InputGetter::CLI>,
  94. L<Fcm::Interactive::InputGetter::GUI|Fcm::Interactive::InputGetter::GUI>
  95. =head1 COPYRIGHT
  96. E<169> Crown copyright Met Office. All rights reserved.
  97. =cut