source: main/trunk/greenstone2/perllib/printusage.pm@ 21341

Last change on this file since 21341 was 21341, checked in by kjdon, 14 years ago

can add internal => 'true' to plugin/classifier option, and it won't get printed out with pluginfo. use for internal options

  • Property svn:keywords set to Author Date Id Revision
File size: 10.7 KB
RevLine 
[4777]1###########################################################################
2#
3# printusage.pm --
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26
27package PrintUsage;
28
29
[5682]30use gsprintf;
[10324]31use strict;
32no strict 'subs'; # allow barewords (eg STDERR) as function arguments
[5682]33
34
[6920]35sub gsprintf
36{
37 return &gsprintf::gsprintf(@_);
38}
39
[11668]40# this is not called by plugins or classifiers, just by scripts
[6920]41sub print_xml_usage
42{
43 my $options = shift(@_);
44
[6934]45 # XML output is always in UTF-8
[6945]46 &gsprintf::output_strings_in_UTF8;
[6934]47
[11668]48 &print_xml_header("script");
[6920]49
50 &gsprintf(STDERR, "<Info>\n");
51 &gsprintf(STDERR, " <Name>$options->{'name'}</Name>\n");
52 &gsprintf(STDERR, " <Desc>$options->{'desc'}</Desc>\n");
53 &gsprintf(STDERR, " <Arguments>\n");
54 if (defined($options->{'args'})) {
[6925]55 &print_options_xml($options->{'args'});
[6920]56 }
57 &gsprintf(STDERR, " </Arguments>\n");
58 &gsprintf(STDERR, "</Info>\n");
59}
60
61
[4777]62sub print_xml_header
63{
[6920]64 &gsprintf(STDERR, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
[4777]65}
66
67
68sub print_options_xml
69{
[6920]70 my $options = shift(@_);
[4777]71
[8716]72 foreach my $option (@$options) {
[21341]73 next if defined($option->{'internal'});
74
[6920]75 my $optionname = $option->{'name'};
[11686]76 my $displayname = $option->{'disp'};
77
[6925]78 my $optiondesc = &gsprintf::lookup_string($option->{'desc'});
[4777]79
[4873]80 # Escape '<' and '>' characters
[7023]81 $optiondesc =~ s/</&amp;lt;/g; # doubly escaped
82 $optiondesc =~ s/>/&amp;gt;/g;
[4873]83
[4777]84 # Display option name, description and type
[6920]85 &gsprintf(STDERR, " <Option>\n");
86 &gsprintf(STDERR, " <Name>$optionname</Name>\n");
[11686]87 if (defined($option->{'disp'})) {
88 my $displayname = &gsprintf::lookup_string($option->{'disp'});
89 # Escape '<' and '>' characters
90 $displayname =~ s/</&amp;lt;/g; # doubly escaped
91 $displayname =~ s/>/&amp;gt;/g;
[11787]92 &gsprintf(STDERR, " <DisplayName>$displayname</DisplayName>\n");
[11686]93 }
[6920]94 &gsprintf(STDERR, " <Desc>$optiondesc</Desc>\n");
95 &gsprintf(STDERR, " <Type>$option->{'type'}</Type>\n");
[4777]96
97 # If the option has a required field, display this
98 if (defined($option->{'reqd'})) {
[6920]99 &gsprintf(STDERR, " <Required>$option->{'reqd'}</Required>\n");
[4777]100 }
101
[10218]102 # If the option has a charactor length field, display this
103 if (defined($option->{'char_length'})) {
[10324]104 &gsprintf(STDERR, " <CharactorLength>$option->{'char_length'}</CharactorLength>\n");
[10218]105 }
106
[6110]107 # If the option has a range field, display this
108 if (defined($option->{'range'})) {
[6920]109 &gsprintf(STDERR, " <Range>$option->{'range'}</Range>\n");
[6110]110 }
111
[4777]112 # If the option has a list of possible values, display these
113 if (defined $option->{'list'}) {
[6920]114 &gsprintf(STDERR, " <List>\n");
115 my $optionvalueslist = $option->{'list'};
[8716]116 foreach my $optionvalue (@$optionvalueslist) {
[6920]117 &gsprintf(STDERR, " <Value>\n");
118 &gsprintf(STDERR, " <Name>$optionvalue->{'name'}</Name>\n");
[4777]119 if (defined $optionvalue->{'desc'}) {
[6925]120 my $optionvaluedesc = &gsprintf::lookup_string($optionvalue->{'desc'});
[4873]121
122 # Escape '<' and '>' characters
[7023]123 $optionvaluedesc =~ s/</&amp;lt;/g; #doubly escaped
124 $optionvaluedesc =~ s/>/&amp;gt;/g;
[4873]125
[6920]126 &gsprintf(STDERR, " <Desc>$optionvaluedesc</Desc>\n");
[4777]127 }
[6920]128 &gsprintf(STDERR, " </Value>\n");
[4777]129 }
130
[10218]131# # Special case for 'input_encoding'
132# if ($optionname =~ m/^input_encoding$/i) {
133# my $e = $encodings::encodings;
134# foreach my $enc (sort {$e->{$a}->{'name'} cmp $e->{$b}->{'name'}} keys (%$e)) {
135# &gsprintf(STDERR, " <Value>\n");
136# &gsprintf(STDERR, " <Name>$enc</Name>\n");
137# &gsprintf(STDERR, " <Desc>$e->{$enc}->{'name'}</Desc>\n");
138# &gsprintf(STDERR, " </Value>\n");
139# }
140# }
[4777]141
[6920]142 &gsprintf(STDERR, " </List>\n");
[4777]143 }
144
145 # Show the default value for the option, if there is one
146 if (defined $option->{'deft'}) {
[6125]147 my $optiondeftvalue = $option->{'deft'};
148
149 # Escape '<' and '>' characters
[11351]150 $optiondeftvalue =~ s/</&lt;/g;
151 $optiondeftvalue =~ s/>/&gt;/g;
[6125]152
[6920]153 &gsprintf(STDERR, " <Default>$optiondeftvalue</Default>\n");
[4777]154 }
155
[6408]156 # If the option is noted as being hidden in GLI, add that to the printout
157 if (defined($option->{'hiddengli'})) {
[6920]158 &gsprintf(STDERR, " <HiddenGLI>$option->{'hiddengli'}</HiddenGLI>\n");
[6408]159 }
160 # If the argument is not hidden then print out the lowest detail mode it is visible in
161 if (defined($option->{'modegli'})) {
[6920]162 &gsprintf(STDERR, " <ModeGLI>$option->{'modegli'}</ModeGLI>\n");
[6408]163 }
164
[6920]165 &gsprintf(STDERR, " </Option>\n");
[4777]166 }
167}
168
169
[6920]170sub print_txt_usage
[4777]171{
[6920]172 my $options = shift(@_);
173 my $params = shift(@_);
[4777]174
[10984]175 # this causes us to automatically send output to a pager, if one is
176 # set, AND our output is going to a terminal
177 # active state perl on windows doesn't do open(handle, "-|");
178 if ($ENV{'GSDLOS'} !~ /windows/ && -t STDOUT) {
179 my $pager = $ENV{"PAGER"};
180 if (! $pager) {$pager="(less || more)"}
181 my $pid = open(STDIN, "-|"); # this does a fork... see man perlipc(1)
182 if (!defined $pid) {
183 gsprintf(STDERR, "pluginfo.pl - can't fork: $!");
184 } else {
185 if ($pid != 0) { # parent (ie forking) process. child gets 0
186 exec ($pager);
187 }
188 }
189 open(STDERR,">&STDOUT"); # so it's easier to pipe output
190 }
191
192
193
[6920]194 my $programname = $options->{'name'};
195 my $programargs = $options->{'args'};
[20723]196 my $programdesc = $options->{'desc'};
[4777]197
[6920]198 # Find the length of the longest option string
199 my $descoffset = 0;
200 if (defined($programargs)) {
201 $descoffset = &find_longest_option_string($programargs);
202 }
[4777]203
[6920]204 # Produce the usage information using the data structure above
[20723]205 if (defined($programdesc)) {
[6920]206 &gsprintf(STDERR, $programname . ": $options->{'desc'}\n\n");
[4777]207 }
[14965]208
[6920]209 &gsprintf(STDERR, " {common.usage}: $programname $params\n\n");
210
211 # Display the program options, if there are some
212 if (defined($programargs)) {
213 # Calculate the column offset of the option descriptions
214 my $optiondescoffset = $descoffset + 2; # 2 spaces between options & descriptions
215
216 &gsprintf(STDERR, " {common.options}:\n");
217
218 # Display the program options
[6925]219 &print_options_txt($programargs, $optiondescoffset);
[6920]220 }
[4777]221}
222
223
224sub print_options_txt
225{
[6920]226 my $options = shift(@_);
227 my $optiondescoffset = shift(@_);
[4777]228
[8716]229 foreach my $option (@$options) {
[21341]230 next if defined($option->{'internal'});
231
[4777]232 # Display option name
[6920]233 my $optionname = $option->{'name'};
234 &gsprintf(STDERR, " -$optionname");
235 my $optionstringlength = length(" -$optionname");
[4777]236
237 # Display option type, if the option is not a flag
[6920]238 my $optiontype = $option->{'type'};
[4777]239 if ($optiontype ne "flag") {
[6920]240 &gsprintf(STDERR, " <$optiontype>");
[4777]241 $optionstringlength = $optionstringlength + length(" <$optiontype>");
242 }
243
[11686]244 # Display the option description
245 if (defined($option->{'disp'})) {
246 my $optiondisp = &gsprintf::lookup_string($option->{'disp'});
247 &display_text_in_column($optiondisp, $optiondescoffset, $optionstringlength, 80);
248 &gsprintf(STDERR, " " x $optionstringlength);
249 }
[6925]250 my $optiondesc = &gsprintf::lookup_string($option->{'desc'});
[6920]251 my $optionreqd = $option->{'reqd'};
[4777]252 if (defined($optionreqd) && $optionreqd eq "yes") {
[6925]253 $optiondesc = "(" . &gsprintf::lookup_string("{PrintUsage.required}") . ") " . $optiondesc;
[4777]254 }
255 &display_text_in_column($optiondesc, $optiondescoffset, $optionstringlength, 80);
256
257 # Show the default value for the option, if there is one
[6920]258 my $optiondefault = $option->{'deft'};
[4777]259 if (defined($optiondefault)) {
[6920]260 &gsprintf(STDERR, " " x $optiondescoffset);
261 &gsprintf(STDERR, "{PrintUsage.default}: $optiondefault\n");
[4777]262 }
263
264 # If the option has a list of possible values, display these
[6920]265 my $optionvalueslist = $option->{'list'};
[4777]266 if (defined($optionvalueslist)) {
[6920]267 &gsprintf(STDERR, "\n");
[8716]268 foreach my $optionvalue (@$optionvalueslist) {
[6920]269 my $optionvaluename = $optionvalue->{'name'};
270 &gsprintf(STDERR, " " x $optiondescoffset);
271 &gsprintf(STDERR, "$optionvaluename:");
[4777]272
[6925]273 my $optionvaluedesc = &gsprintf::lookup_string($optionvalue->{'desc'});
[4777]274 &display_text_in_column($optionvaluedesc, $optiondescoffset + 2,
275 $optiondescoffset + length($optionvaluename), 80);
276 }
277 }
278
[10218]279# # Special case for 'input_encoding'
280# if ($optionname =~ m/^input_encoding$/i) {
281# my $e = $encodings::encodings;
282# foreach my $enc (sort {$e->{$a}->{'name'} cmp $e->{$b}->{'name'}} keys (%$e)) {
283# &gsprintf(STDERR, " " x $optiondescoffset);
284# &gsprintf(STDERR, "$enc:");
285#
286# my $encodingdesc = $e->{$enc}->{'name'};
287# &display_text_in_column($encodingdesc, $optiondescoffset + 2,
288# $optiondescoffset + length("$enc:"), 80);
289# }
290# }
[4777]291
292 # Add a blank line to separate options
[6920]293 &gsprintf(STDERR, "\n");
[4777]294 }
295}
296
297
298sub display_text_in_column
299{
[6920]300 my ($text, $columnbeg, $firstlineoffset, $columnend) = @_;
[4777]301
302 # Spaces are put *before* words, so treat the column beginning as 1 smaller than it is
303 $columnbeg = $columnbeg - 1;
304
305 # Add some padding (if needed) for the first line
[6920]306 my $linelength = $columnbeg;
[4777]307 if ($firstlineoffset < $columnbeg) {
[6920]308 &gsprintf(STDERR, " " x ($columnbeg - $firstlineoffset));
[4777]309 }
310 else {
311 $linelength = $firstlineoffset;
312 }
313
314 # Break the text into words, and display one at a time
[6920]315 my @words = split(/ /, $text);
[4777]316
[8716]317 foreach my $word (@words) {
[4777]318 # If printing this word would exceed the column end, start a new line
319 if (($linelength + length($word)) >= $columnend) {
[6920]320 &gsprintf(STDERR, "\n");
321 &gsprintf(STDERR, " " x $columnbeg);
[4777]322 $linelength = $columnbeg;
323 }
324
325 # Write the word
[6920]326 &gsprintf(STDERR, " $word");
[4777]327 $linelength = $linelength + length(" $word");
328 }
329
[6920]330 &gsprintf(STDERR, "\n");
[4777]331}
332
333
[6920]334sub find_longest_option_string
335{
336 my $options = shift(@_);
337
338 my $maxlength = 0;
[8716]339 foreach my $option (@$options) {
[6920]340 my $optionname = $option->{'name'};
341 my $optiontype = $option->{'type'};
342
343 my $optionlength = length(" -$optionname");
344 if ($optiontype ne "flag") {
345 $optionlength = $optionlength + length(" <$optiontype>");
346 }
347
348 # Remember the longest
349 if ($optionlength > $maxlength) {
350 $maxlength = $optionlength;
351 }
352 }
353 return $maxlength;
354}
355
356
[4777]3571;
Note: See TracBrowser for help on using the repository browser.