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

Last change on this file since 6983 was 6945, checked in by mdewsnip, 20 years ago

Updated the resource bundle handling code some more. Strings are first looked for in a language specific resource bundle (if specified). If not found there, the default resource bundle is checked. If still not found, the English resource bundle is checked. These resource bundles are loaded on an as-needed basis.

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