Option.pm 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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::CLI::Option;
  9. use constant NO_ARG => 0;
  10. use constant SCALAR_ARG => 1;
  11. use constant ARRAY_ARG => 2;
  12. use constant HASH_ARG => 3;
  13. use constant ARG_STRING_SUFFIX_FOR => (q{}, q{=s}, q{=s@}, q{=s%});
  14. ################################################################################
  15. # Constructor
  16. sub new {
  17. my ($class, $args_ref) = @_;
  18. return bless({%{$args_ref}}, $class);
  19. }
  20. ################################################################################
  21. # Methods: get_*
  22. for my $key (
  23. # Returns the delimiter of this option, if it is an array
  24. 'delimiter',
  25. # Returns the description of this option
  26. 'description',
  27. # Returns the (long) name of this option
  28. 'name',
  29. ) {
  30. no strict qw{refs};
  31. my $getter = "get_$key";
  32. *$getter = sub {
  33. my ($self) = @_;
  34. return $self->{$key};
  35. }
  36. }
  37. ################################################################################
  38. # Returns the letter of this option
  39. sub get_letter {
  40. my ($self) = @_;
  41. if (defined($self->{letter})) {
  42. return substr($self->{letter}, 0, 1);
  43. }
  44. else {
  45. return;
  46. }
  47. }
  48. ################################################################################
  49. # Returns whether the current option has no, scalar, array or hash arguments
  50. sub has_arg {
  51. my ($self) = @_;
  52. return (defined($self->{has_arg}) ? $self->{has_arg} : $self->NO_ARG);
  53. }
  54. ################################################################################
  55. # Returns true if this option is associated with help
  56. sub is_help {
  57. my ($self) = @_;
  58. return $self->{is_help};
  59. }
  60. ################################################################################
  61. # Returns an option string/reference pair for Getopt::Long::GetOptions
  62. sub get_arg_for_getopt_long {
  63. my ($self) = @_;
  64. my $option_string
  65. = $self->get_name()
  66. . ($self->get_letter() ? q{|} . $self->get_letter() : q{})
  67. . (ARG_STRING_SUFFIX_FOR)[$self->has_arg()]
  68. ;
  69. return $option_string;
  70. }
  71. 1;
  72. __END__
  73. =head1 NAME
  74. Fcm::CLI::Option
  75. =head1 SYNOPSIS
  76. use Fcm::CLI::Option;
  77. $option = Fcm::CLI::Option->new({
  78. name => 'name',
  79. letter => 'n',
  80. has_arg => Fcm::CLI::Option->SCALAR_ARG,
  81. is_help => 1,
  82. description => 'an example option',
  83. });
  84. # time passes ...
  85. use Getopt::Long qw{GetOptions};
  86. $success = GetOptions(
  87. \%hash,
  88. $option->get_arg_for_getopt_long(), # ('name|n=s')
  89. # and other options ...
  90. );
  91. $option_value = $option->get_value();
  92. =head1 DESCRIPTION
  93. An object of this class represents a CLI option.
  94. =head1 METHODS
  95. =over 4
  96. =item new($args_ref)
  97. Constructor.
  98. =item get_arg_for_getopt_long()
  99. Returns an option string for this option that is suitable for use as arguments
  100. to L<Getopt::Long|Getopt::Long>.
  101. =item get_description()
  102. Returns a description of this option.
  103. =item get_delimiter()
  104. Returns the delimiter of this option. This is only relevant if has_arg() is
  105. equal to C<ARRAY_ARG>. If set, the argument for this option should be re-grouped
  106. using this delimiter.
  107. =item get_name()
  108. Returns the (long) name of this option.
  109. =item get_letter()
  110. Returns the option letter of this option.
  111. =item has_arg()
  112. Returns whether this option has no, scalar, array or hash arguments. See
  113. L</CONSTANTS> for detail.
  114. =item is_help()
  115. Returns true if this option is associated with help.
  116. =back
  117. =head1 CONSTANTS
  118. =over 4
  119. =item NO_ARG
  120. An option has no argument. (Default)
  121. =item SCALAR_ARG
  122. An option has a single scalar argument.
  123. =item ARRAY_ARG
  124. An option has multiple arguments, which can be placed in an array.
  125. =item HASH_ARG
  126. An option has multiple arguments, which can be placed in an hash.
  127. =back
  128. =head1 SEE ALSO
  129. L<Getopt::Long|Getopt::Long>
  130. =head1 COPYRIGHT
  131. E<169> Crown copyright Met Office. All rights reserved.
  132. =cut