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

Last change on this file since 10664 was 10324, checked in by kjdon, 19 years ago

added no strict, also changed Charactor Length to CharactorLength (its an element name\!)

  • Property svn:keywords set to Author Date Id Revision
File size: 10.3 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 my $programname = $options->{'name'};
180 my $programargs = $options->{'args'};
181
182 # Find the length of the longest option string
183 my $descoffset = 0;
184 if (defined($programargs)) {
185 $descoffset = &find_longest_option_string($programargs);
186 }
187
188 # Produce the usage information using the data structure above
189 if ($programdesc) {
190 &gsprintf(STDERR, $programname . ": $options->{'desc'}\n\n");
191 }
192 &gsprintf(STDERR, " {common.usage}: $programname $params\n\n");
193
194 # Display the program options, if there are some
195 if (defined($programargs)) {
196 # Calculate the column offset of the option descriptions
197 my $optiondescoffset = $descoffset + 2; # 2 spaces between options & descriptions
198
199 &gsprintf(STDERR, " {common.options}:\n");
200
201 # Display the program options
202 &print_options_txt($programargs, $optiondescoffset);
203 }
204}
205
206
207sub print_options_txt
208{
209 my $options = shift(@_);
210 my $optiondescoffset = shift(@_);
211
212 foreach my $option (@$options) {
213 # Display option name
214 my $optionname = $option->{'name'};
215 &gsprintf(STDERR, " -$optionname");
216 my $optionstringlength = length(" -$optionname");
217
218 # Display option type, if the option is not a flag
219 my $optiontype = $option->{'type'};
220 if ($optiontype ne "flag") {
221 &gsprintf(STDERR, " <$optiontype>");
222 $optionstringlength = $optionstringlength + length(" <$optiontype>");
223 }
224
225 # Display the option description
226 my $optiondesc = &gsprintf::lookup_string($option->{'desc'});
227 my $optionreqd = $option->{'reqd'};
228 if (defined($optionreqd) && $optionreqd eq "yes") {
229 $optiondesc = "(" . &gsprintf::lookup_string("{PrintUsage.required}") . ") " . $optiondesc;
230 }
231 &display_text_in_column($optiondesc, $optiondescoffset, $optionstringlength, 80);
232
233 # Show the default value for the option, if there is one
234 my $optiondefault = $option->{'deft'};
235 if (defined($optiondefault)) {
236 &gsprintf(STDERR, " " x $optiondescoffset);
237 &gsprintf(STDERR, "{PrintUsage.default}: $optiondefault\n");
238 }
239
240 # If the option has a list of possible values, display these
241 my $optionvalueslist = $option->{'list'};
242 if (defined($optionvalueslist)) {
243 &gsprintf(STDERR, "\n");
244 foreach my $optionvalue (@$optionvalueslist) {
245 my $optionvaluename = $optionvalue->{'name'};
246 &gsprintf(STDERR, " " x $optiondescoffset);
247 &gsprintf(STDERR, "$optionvaluename:");
248
249 my $optionvaluedesc = &gsprintf::lookup_string($optionvalue->{'desc'});
250 &display_text_in_column($optionvaluedesc, $optiondescoffset + 2,
251 $optiondescoffset + length($optionvaluename), 80);
252 }
253 }
254
255# # Special case for 'input_encoding'
256# if ($optionname =~ m/^input_encoding$/i) {
257# my $e = $encodings::encodings;
258# foreach my $enc (sort {$e->{$a}->{'name'} cmp $e->{$b}->{'name'}} keys (%$e)) {
259# &gsprintf(STDERR, " " x $optiondescoffset);
260# &gsprintf(STDERR, "$enc:");
261#
262# my $encodingdesc = $e->{$enc}->{'name'};
263# &display_text_in_column($encodingdesc, $optiondescoffset + 2,
264# $optiondescoffset + length("$enc:"), 80);
265# }
266# }
267
268 # Add a blank line to separate options
269 &gsprintf(STDERR, "\n");
270 }
271}
272
273
274sub display_text_in_column
275{
276 my ($text, $columnbeg, $firstlineoffset, $columnend) = @_;
277
278 # Spaces are put *before* words, so treat the column beginning as 1 smaller than it is
279 $columnbeg = $columnbeg - 1;
280
281 # Add some padding (if needed) for the first line
282 my $linelength = $columnbeg;
283 if ($firstlineoffset < $columnbeg) {
284 &gsprintf(STDERR, " " x ($columnbeg - $firstlineoffset));
285 }
286 else {
287 $linelength = $firstlineoffset;
288 }
289
290 # Break the text into words, and display one at a time
291 my @words = split(/ /, $text);
292
293 foreach my $word (@words) {
294 # If printing this word would exceed the column end, start a new line
295 if (($linelength + length($word)) >= $columnend) {
296 &gsprintf(STDERR, "\n");
297 &gsprintf(STDERR, " " x $columnbeg);
298 $linelength = $columnbeg;
299 }
300
301 # Write the word
302 &gsprintf(STDERR, " $word");
303 $linelength = $linelength + length(" $word");
304 }
305
306 &gsprintf(STDERR, "\n");
307}
308
309
310sub find_longest_option_string
311{
312 my $options = shift(@_);
313
314 my $maxlength = 0;
315 foreach my $option (@$options) {
316 my $optionname = $option->{'name'};
317 my $optiontype = $option->{'type'};
318
319 my $optionlength = length(" -$optionname");
320 if ($optiontype ne "flag") {
321 $optionlength = $optionlength + length(" <$optiontype>");
322 }
323
324 # Remember the longest
325 if ($optionlength > $maxlength) {
326 $maxlength = $optionlength;
327 }
328 }
329 return $maxlength;
330}
331
332
3331;
Note: See TracBrowser for help on using the repository browser.