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

Last change on this file since 11089 was 10984, checked in by jrm21, 18 years ago

move the terminal detection/pager redirection stuff from pluginfo.pl to
the usage module so that all command-line scripts using print_usage_txt()
benefit.

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