source: gsdl/trunk/perllib/printusage.pm@ 14965

Last change on this file since 14965 was 14965, checked in by davidb, 16 years ago

Minor change to printing usage information to help with new g2f-import.pl and g2f-buildcol.pl scripts

  • Property svn:keywords set to Author Date Id Revision
File size: 10.6 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) {
[6920]73 my $optionname = $option->{'name'};
[11686]74 my $displayname = $option->{'disp'};
75
[6925]76 my $optiondesc = &gsprintf::lookup_string($option->{'desc'});
[4777]77
[4873]78 # Escape '<' and '>' characters
[7023]79 $optiondesc =~ s/</&amp;lt;/g; # doubly escaped
80 $optiondesc =~ s/>/&amp;gt;/g;
[4873]81
[4777]82 # Display option name, description and type
[6920]83 &gsprintf(STDERR, " <Option>\n");
84 &gsprintf(STDERR, " <Name>$optionname</Name>\n");
[11686]85 if (defined($option->{'disp'})) {
86 my $displayname = &gsprintf::lookup_string($option->{'disp'});
87 # Escape '<' and '>' characters
88 $displayname =~ s/</&amp;lt;/g; # doubly escaped
89 $displayname =~ s/>/&amp;gt;/g;
[11787]90 &gsprintf(STDERR, " <DisplayName>$displayname</DisplayName>\n");
[11686]91 }
[6920]92 &gsprintf(STDERR, " <Desc>$optiondesc</Desc>\n");
93 &gsprintf(STDERR, " <Type>$option->{'type'}</Type>\n");
[4777]94
95 # If the option has a required field, display this
96 if (defined($option->{'reqd'})) {
[6920]97 &gsprintf(STDERR, " <Required>$option->{'reqd'}</Required>\n");
[4777]98 }
99
[10218]100 # If the option has a charactor length field, display this
101 if (defined($option->{'char_length'})) {
[10324]102 &gsprintf(STDERR, " <CharactorLength>$option->{'char_length'}</CharactorLength>\n");
[10218]103 }
104
[6110]105 # If the option has a range field, display this
106 if (defined($option->{'range'})) {
[6920]107 &gsprintf(STDERR, " <Range>$option->{'range'}</Range>\n");
[6110]108 }
109
[4777]110 # If the option has a list of possible values, display these
111 if (defined $option->{'list'}) {
[6920]112 &gsprintf(STDERR, " <List>\n");
113 my $optionvalueslist = $option->{'list'};
[8716]114 foreach my $optionvalue (@$optionvalueslist) {
[6920]115 &gsprintf(STDERR, " <Value>\n");
116 &gsprintf(STDERR, " <Name>$optionvalue->{'name'}</Name>\n");
[4777]117 if (defined $optionvalue->{'desc'}) {
[6925]118 my $optionvaluedesc = &gsprintf::lookup_string($optionvalue->{'desc'});
[4873]119
120 # Escape '<' and '>' characters
[7023]121 $optionvaluedesc =~ s/</&amp;lt;/g; #doubly escaped
122 $optionvaluedesc =~ s/>/&amp;gt;/g;
[4873]123
[6920]124 &gsprintf(STDERR, " <Desc>$optionvaluedesc</Desc>\n");
[4777]125 }
[6920]126 &gsprintf(STDERR, " </Value>\n");
[4777]127 }
128
[10218]129# # Special case for 'input_encoding'
130# if ($optionname =~ m/^input_encoding$/i) {
131# my $e = $encodings::encodings;
132# foreach my $enc (sort {$e->{$a}->{'name'} cmp $e->{$b}->{'name'}} keys (%$e)) {
133# &gsprintf(STDERR, " <Value>\n");
134# &gsprintf(STDERR, " <Name>$enc</Name>\n");
135# &gsprintf(STDERR, " <Desc>$e->{$enc}->{'name'}</Desc>\n");
136# &gsprintf(STDERR, " </Value>\n");
137# }
138# }
[4777]139
[6920]140 &gsprintf(STDERR, " </List>\n");
[4777]141 }
142
143 # Show the default value for the option, if there is one
144 if (defined $option->{'deft'}) {
[6125]145 my $optiondeftvalue = $option->{'deft'};
146
147 # Escape '<' and '>' characters
[11351]148 $optiondeftvalue =~ s/</&lt;/g;
149 $optiondeftvalue =~ s/>/&gt;/g;
[6125]150
[6920]151 &gsprintf(STDERR, " <Default>$optiondeftvalue</Default>\n");
[4777]152 }
153
[6408]154 # If the option is noted as being hidden in GLI, add that to the printout
155 if (defined($option->{'hiddengli'})) {
[6920]156 &gsprintf(STDERR, " <HiddenGLI>$option->{'hiddengli'}</HiddenGLI>\n");
[6408]157 }
158 # If the argument is not hidden then print out the lowest detail mode it is visible in
159 if (defined($option->{'modegli'})) {
[6920]160 &gsprintf(STDERR, " <ModeGLI>$option->{'modegli'}</ModeGLI>\n");
[6408]161 }
162
[6920]163 &gsprintf(STDERR, " </Option>\n");
[4777]164 }
165}
166
167
[6920]168sub print_txt_usage
[4777]169{
[6920]170 my $options = shift(@_);
171 my $params = shift(@_);
172 my $programdesc = shift(@_);
[4777]173
[10984]174 # this causes us to automatically send output to a pager, if one is
175 # set, AND our output is going to a terminal
176 # active state perl on windows doesn't do open(handle, "-|");
177 if ($ENV{'GSDLOS'} !~ /windows/ && -t STDOUT) {
178 my $pager = $ENV{"PAGER"};
179 if (! $pager) {$pager="(less || more)"}
180 my $pid = open(STDIN, "-|"); # this does a fork... see man perlipc(1)
181 if (!defined $pid) {
182 gsprintf(STDERR, "pluginfo.pl - can't fork: $!");
183 } else {
184 if ($pid != 0) { # parent (ie forking) process. child gets 0
185 exec ($pager);
186 }
187 }
188 open(STDERR,">&STDOUT"); # so it's easier to pipe output
189 }
190
191
192
[6920]193 my $programname = $options->{'name'};
194 my $programargs = $options->{'args'};
[4777]195
[6920]196 # Find the length of the longest option string
197 my $descoffset = 0;
198 if (defined($programargs)) {
199 $descoffset = &find_longest_option_string($programargs);
200 }
[4777]201
[6920]202 # Produce the usage information using the data structure above
203 if ($programdesc) {
204 &gsprintf(STDERR, $programname . ": $options->{'desc'}\n\n");
[4777]205 }
[14965]206
[6920]207 &gsprintf(STDERR, " {common.usage}: $programname $params\n\n");
208
209 # Display the program options, if there are some
210 if (defined($programargs)) {
211 # Calculate the column offset of the option descriptions
212 my $optiondescoffset = $descoffset + 2; # 2 spaces between options & descriptions
213
214 &gsprintf(STDERR, " {common.options}:\n");
215
216 # Display the program options
[6925]217 &print_options_txt($programargs, $optiondescoffset);
[6920]218 }
[4777]219}
220
221
222sub print_options_txt
223{
[6920]224 my $options = shift(@_);
225 my $optiondescoffset = shift(@_);
[4777]226
[8716]227 foreach my $option (@$options) {
[4777]228 # Display option name
[6920]229 my $optionname = $option->{'name'};
230 &gsprintf(STDERR, " -$optionname");
231 my $optionstringlength = length(" -$optionname");
[4777]232
233 # Display option type, if the option is not a flag
[6920]234 my $optiontype = $option->{'type'};
[4777]235 if ($optiontype ne "flag") {
[6920]236 &gsprintf(STDERR, " <$optiontype>");
[4777]237 $optionstringlength = $optionstringlength + length(" <$optiontype>");
238 }
239
[11686]240 # Display the option description
241 if (defined($option->{'disp'})) {
242 my $optiondisp = &gsprintf::lookup_string($option->{'disp'});
243 &display_text_in_column($optiondisp, $optiondescoffset, $optionstringlength, 80);
244 &gsprintf(STDERR, " " x $optionstringlength);
245 }
[6925]246 my $optiondesc = &gsprintf::lookup_string($option->{'desc'});
[6920]247 my $optionreqd = $option->{'reqd'};
[4777]248 if (defined($optionreqd) && $optionreqd eq "yes") {
[6925]249 $optiondesc = "(" . &gsprintf::lookup_string("{PrintUsage.required}") . ") " . $optiondesc;
[4777]250 }
251 &display_text_in_column($optiondesc, $optiondescoffset, $optionstringlength, 80);
252
253 # Show the default value for the option, if there is one
[6920]254 my $optiondefault = $option->{'deft'};
[4777]255 if (defined($optiondefault)) {
[6920]256 &gsprintf(STDERR, " " x $optiondescoffset);
257 &gsprintf(STDERR, "{PrintUsage.default}: $optiondefault\n");
[4777]258 }
259
260 # If the option has a list of possible values, display these
[6920]261 my $optionvalueslist = $option->{'list'};
[4777]262 if (defined($optionvalueslist)) {
[6920]263 &gsprintf(STDERR, "\n");
[8716]264 foreach my $optionvalue (@$optionvalueslist) {
[6920]265 my $optionvaluename = $optionvalue->{'name'};
266 &gsprintf(STDERR, " " x $optiondescoffset);
267 &gsprintf(STDERR, "$optionvaluename:");
[4777]268
[6925]269 my $optionvaluedesc = &gsprintf::lookup_string($optionvalue->{'desc'});
[4777]270 &display_text_in_column($optionvaluedesc, $optiondescoffset + 2,
271 $optiondescoffset + length($optionvaluename), 80);
272 }
273 }
274
[10218]275# # Special case for 'input_encoding'
276# if ($optionname =~ m/^input_encoding$/i) {
277# my $e = $encodings::encodings;
278# foreach my $enc (sort {$e->{$a}->{'name'} cmp $e->{$b}->{'name'}} keys (%$e)) {
279# &gsprintf(STDERR, " " x $optiondescoffset);
280# &gsprintf(STDERR, "$enc:");
281#
282# my $encodingdesc = $e->{$enc}->{'name'};
283# &display_text_in_column($encodingdesc, $optiondescoffset + 2,
284# $optiondescoffset + length("$enc:"), 80);
285# }
286# }
[4777]287
288 # Add a blank line to separate options
[6920]289 &gsprintf(STDERR, "\n");
[4777]290 }
291}
292
293
294sub display_text_in_column
295{
[6920]296 my ($text, $columnbeg, $firstlineoffset, $columnend) = @_;
[4777]297
298 # Spaces are put *before* words, so treat the column beginning as 1 smaller than it is
299 $columnbeg = $columnbeg - 1;
300
301 # Add some padding (if needed) for the first line
[6920]302 my $linelength = $columnbeg;
[4777]303 if ($firstlineoffset < $columnbeg) {
[6920]304 &gsprintf(STDERR, " " x ($columnbeg - $firstlineoffset));
[4777]305 }
306 else {
307 $linelength = $firstlineoffset;
308 }
309
310 # Break the text into words, and display one at a time
[6920]311 my @words = split(/ /, $text);
[4777]312
[8716]313 foreach my $word (@words) {
[4777]314 # If printing this word would exceed the column end, start a new line
315 if (($linelength + length($word)) >= $columnend) {
[6920]316 &gsprintf(STDERR, "\n");
317 &gsprintf(STDERR, " " x $columnbeg);
[4777]318 $linelength = $columnbeg;
319 }
320
321 # Write the word
[6920]322 &gsprintf(STDERR, " $word");
[4777]323 $linelength = $linelength + length(" $word");
324 }
325
[6920]326 &gsprintf(STDERR, "\n");
[4777]327}
328
329
[6920]330sub find_longest_option_string
331{
332 my $options = shift(@_);
333
334 my $maxlength = 0;
[8716]335 foreach my $option (@$options) {
[6920]336 my $optionname = $option->{'name'};
337 my $optiontype = $option->{'type'};
338
339 my $optionlength = length(" -$optionname");
340 if ($optiontype ne "flag") {
341 $optionlength = $optionlength + length(" <$optiontype>");
342 }
343
344 # Remember the longest
345 if ($optionlength > $maxlength) {
346 $maxlength = $optionlength;
347 }
348 }
349 return $maxlength;
350}
351
352
[4777]3531;
Note: See TracBrowser for help on using the repository browser.