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

Last change on this file since 6933 was 6925, checked in by mdewsnip, 20 years ago

Changed the way display in different languages is done. Instead of passing a language variable throughout the process, the desired resource bundle is explicitly loaded during the initialization of each program (buildcol.pl, classinfo.pl, exportcol.pl, import.pl mkcol.pl, pluginfo.pl).

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