MySimple.pm 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. package Getopt::MySimple;
  2. # Name:
  3. # Getopt::MySimple.
  4. #
  5. # Documentation:
  6. # POD-style (incomplete) documentation is in file MySimple.pod
  7. #
  8. # Tabs:
  9. # 4 spaces || die.
  10. #
  11. # Author:
  12. # Ron Savage rpsavage@ozemail.com.au.
  13. # 1.00 19-Aug-97 Initial version.
  14. # 1.10 13-Oct-97 Add arrays of switches (eg '=s@').
  15. # 1.20 3-Dec-97 Add 'Help' on a per-switch basis.
  16. # 1.30 11-Dec-97 Change 'Help' to 'verbose'. Make all hash keys lowercase.
  17. # 1.40 10-Nov-98 Change width of help report. Restructure tests.
  18. # 1-Jul-00 Modifications for Texi2html
  19. # --------------------------------------------------------------------------
  20. # Locally modified by obachman (Display type instead of env, order by cmp)
  21. # $Id$
  22. # use strict;
  23. # no strict 'refs';
  24. use vars qw(@EXPORT @EXPORT_OK @ISA);
  25. use vars qw($fieldWidth $opt $VERSION);
  26. use Exporter();
  27. use Getopt::Long;
  28. @ISA = qw(Exporter);
  29. @EXPORT = qw();
  30. @EXPORT_OK = qw($opt); # An alias for $self -> {'opt'}.
  31. # --------------------------------------------------------------------------
  32. $fieldWidth = 20;
  33. $VERSION = '1.41';
  34. # --------------------------------------------------------------------------
  35. sub byOrder
  36. {
  37. my($self) = @_;
  38. return uc($a) cmp (uc($b));
  39. }
  40. # --------------------------------------------------------------------------
  41. sub dumpOptions
  42. {
  43. my($self) = @_;
  44. print 'Option', ' ' x ($fieldWidth - length('Option') ), "Value\n";
  45. for (sort byOrder keys(%{$self -> {'opt'} }) )
  46. {
  47. print "-$_", ' ' x ($fieldWidth - (1 + length) ), "${$self->{'opt'} }{$_}\n";
  48. }
  49. print "\n";
  50. } # End of dumpOptions.
  51. # --------------------------------------------------------------------------
  52. # Return:
  53. # 0 -> Error.
  54. # 1 -> Ok.
  55. sub getOptions
  56. {
  57. push(@_, 0) if ($#_ == 2); # Default for $ignoreCase is 0.
  58. push(@_, 1) if ($#_ == 3); # Default for $helpThenExit is 1.
  59. my($self, $default, $helpText, $versionText,
  60. $helpThenExit, $versionThenExit, $ignoreCase) = @_;
  61. $helpThenExit = 1 unless (defined($helpThenExit));
  62. $versionThenExit = 1 unless (defined($versionThenExit));
  63. $ignoreCase = 0 unless (defined($ignoreCase));
  64. $self -> {'default'} = $default;
  65. $self -> {'helpText'} = $helpText;
  66. $self -> {'versionText'} = $versionText;
  67. $Getopt::Long::ignorecase = $ignoreCase;
  68. unless (defined($self -> {'default'}{'help'}))
  69. {
  70. $self -> {'default'}{'help'} =
  71. {
  72. type => ':i',
  73. default => '',
  74. linkage => sub {$self->helpOptions($_[1]); sleep 5;exit (0) if $helpThenExit;},
  75. verbose => "print help and exit"
  76. };
  77. }
  78. unless (defined($self -> {'default'}{'version'}))
  79. {
  80. $self -> {'default'}{'version'} =
  81. {
  82. type => '',
  83. default => '',
  84. linkage => sub {print $self->{'versionText'}; exit (0) if $versionThenExit;},
  85. verbose => "print version and exit"
  86. };
  87. }
  88. for (keys(%{$self -> {'default'} }) )
  89. {
  90. next unless (ref(${$self -> {'default'} }{$_}) eq 'HASH');
  91. my $type = ${$self -> {'default'} }{$_}{'type'};
  92. push(@{$self -> {'type'} }, "$_$type");
  93. my $key = $_;
  94. # get rid of aliases, if any
  95. $key =~ s/\|.*//;
  96. $self->{'opt'}->{$key} = ${$self -> {'default'} }{$_}{'linkage'}
  97. if ${$self -> {'default'} }{$_}{'linkage'};
  98. }
  99. my($result) = &GetOptions($self -> {'opt'}, @{$self -> {'type'} });
  100. return $result unless $result;
  101. for (keys(%{$self -> {'default'} }) )
  102. {
  103. if (! defined(${$self -> {'opt'} }{$_})) #{
  104. {
  105. ${$self -> {'opt'} }{$_} = ${$self -> {'default'} }{$_}{'default'};
  106. }
  107. }
  108. $result;
  109. } # End of getOptions.
  110. # --------------------------------------------------------------------------
  111. sub helpOptions
  112. {
  113. my($self) = shift;
  114. my($noHelp) = shift;
  115. $noHelp = 0 unless $noHelp;
  116. my($optwidth, $typewidth, $defaultwidth, $maxlinewidth, $valind, $valwidth)
  117. = (10, 5, 9, 78, 4, 11);
  118. print "$self->{'helpText'}" if ($self -> {'helpText'});
  119. print ' Option', ' ' x ($optwidth - length('Option') -1 ),
  120. 'Type', ' ' x ($typewidth - length('Type') + 1),
  121. 'Default', ' ' x ($defaultwidth - length('Default') ),
  122. "Description\n";
  123. for (sort byOrder keys(%{$self -> {'default'} }) )
  124. {
  125. my($line, $help, $option, $val);
  126. $option = $_;
  127. next if (ref(${$self -> {'default'} }{$_}) ne 'HASH' or (${$self->{'default'} }{$_}{'noHelp'} && ${$self->{'default'} }{$_}{'noHelp'} > $noHelp));
  128. #$line = " -$_" . ' ' x ($optwidth - (2 + length) ) .
  129. # "${$self->{'default'} }{$_}{'type'} ".
  130. # ' ' x ($typewidth - (1+length(${$self -> {'default'} }{$_}{'type'}) ));
  131. $line = " --$_" . "${$self->{'default'} }{$_}{'type'}".
  132. ' ' x ($typewidth - (1+length(${$self -> {'default'} }{$_}{'type'}) ));
  133. $val = ${$self->{'default'} }{$_}{'linkage'};
  134. if ($val)
  135. {
  136. if ((ref($val) eq 'SCALAR') and (defined($$val)))
  137. {
  138. $val = $$val;
  139. }
  140. else
  141. {
  142. $val = '';
  143. }
  144. }
  145. elsif (defined(${$self->{'default'} }{$_}{'default'}))
  146. {
  147. $val = ${$self->{'default'} }{$_}{'default'};
  148. }
  149. else
  150. {
  151. $val = '';
  152. }
  153. $line .= "$val ";
  154. $line .= ' ' x ($optwidth + $typewidth + $defaultwidth + 1 - length($line));
  155. if (defined(${$self -> {'default'} }{$_}{'verbose'}) &&
  156. ${$self -> {'default'} }{$_}{'verbose'} ne '')
  157. {
  158. $help = "${$self->{'default'} }{$_}{'verbose'}";
  159. }
  160. else
  161. {
  162. $help = ' ';
  163. }
  164. if ((length("$line") + length($help)) < $maxlinewidth)
  165. {
  166. print $line , $help, "\n";
  167. }
  168. else
  169. {
  170. print $line, "\n", ' ' x $valind, $help, "\n";
  171. }
  172. for $val (sort byOrder keys(%{${$self->{'default'}}{$option}{'values'}}))
  173. {
  174. print ' ' x ($valind + 2);
  175. print $val, ' ', ' ' x ($valwidth - length($val) - 2);
  176. print ${$self->{'default'}}{$option}{'values'}{$val}, "\n";
  177. }
  178. }
  179. print <<EOT;
  180. Note: 'Options' may be abbreviated. -- prefix may be replaced by a single -.
  181. 'Type' specifications mean:
  182. <none>| ! no argument: variable is set to 1 on -foo (or, to 0 on -nofoo)
  183. =s | :s mandatory (or, optional) string argument
  184. =i | :i mandatory (or, optional) integer argument
  185. EOT
  186. } # End of helpOptions.
  187. #-------------------------------------------------------------------
  188. sub new
  189. {
  190. my($class) = @_;
  191. my($self) = {};
  192. $self -> {'default'} = {};
  193. $self -> {'helpText'} = '';
  194. $self -> {'opt'} = {};
  195. $opt = $self -> {'opt'}; # An alias for $self -> {'opt'}.
  196. $self -> {'type'} = ();
  197. return bless $self, $class;
  198. } # End of new.
  199. # --------------------------------------------------------------------------
  200. 1;
  201. # End MySimple.pm