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

Last change on this file since 6332 was 6125, checked in by mdewsnip, 21 years ago

Angle brackets in option defaults are now entified.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.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 print_xml_header
34{
35 print STDERR "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
36 print STDERR "<!DOCTYPE Info [\n";
37 print STDERR " <!ELEMENT Info (Name, Desc, Arguments)>\n";
38 print STDERR " <!ELEMENT Arguments (Option*)>\n";
39 print STDERR " <!ELEMENT Option (Name, Desc, Type, Required, Range, Default?, List?)>\n";
40 print STDERR " <!ELEMENT Name (#PCDATA)>\n";
41 print STDERR " <!ELEMENT Desc (#PCDATA)>\n";
42 print STDERR " <!ELEMENT Type (#PCDATA)>\n";
43 print STDERR " <!ELEMENT Required (#PCDATA)>\n";
44 print STDERR " <!ELEMENT Range (#PCDATA)>\n";
45 print STDERR " <!ELEMENT Default (#PCDATA)>\n";
46 print STDERR " <!ELEMENT List (Value*)>\n";
47 print STDERR " <!ELEMENT Value (Name, Desc?)>\n";
48 print STDERR "]>\n\n";
49}
50
51
52sub print_options_xml
53{
54 local $language = shift(@_);
55 local $options = shift(@_);
56
57 foreach $option (@$options) {
58 local $optionname = $option->{'name'};
59 local $optiondesc = &gsprintf::lookup_string($language, $option->{'desc'});
60
61 # Escape '<' and '>' characters
62 $optiondesc =~ s/</&lt;/g;
63 $optiondesc =~ s/>/&gt;/g;
64
65 # Display option name, description and type
66 print STDERR " <Option>\n";
67 print STDERR " <Name>$optionname</Name>\n";
68 print STDERR " <Desc>$optiondesc</Desc>\n";
69 print STDERR " <Type>$option->{'type'}</Type>\n";
70
71 # If the option has a required field, display this
72 if (defined($option->{'reqd'})) {
73 print STDERR " <Required>$option->{'reqd'}</Required>\n";
74 }
75
76 # If the option has a range field, display this
77 if (defined($option->{'range'})) {
78 print STDERR " <Range>$option->{'range'}</Range>\n";
79 }
80
81 # If the option has a list of possible values, display these
82 if (defined $option->{'list'}) {
83 print STDERR " <List>\n";
84 local $optionvalueslist = $option->{'list'};
85 foreach $optionvalue (@$optionvalueslist) {
86 print STDERR " <Value>\n";
87 print STDERR " <Name>$optionvalue->{'name'}</Name>\n";
88 if (defined $optionvalue->{'desc'}) {
89 local $optionvaluedesc = &gsprintf::lookup_string($language, $optionvalue->{'desc'});
90
91 # Escape '<' and '>' characters
92 $optionvaluedesc =~ s/</&lt;/g;
93 $optionvaluedesc =~ s/>/&gt;/g;
94
95 print STDERR " <Desc>$optionvaluedesc</Desc>\n";
96 }
97 print STDERR " </Value>\n";
98 }
99
100 # Special case for 'input_encoding'
101 if ($optionname =~ m/^input_encoding$/i) {
102 my $e = $encodings::encodings;
103 foreach $enc (sort {$e->{$a}->{'name'} cmp $e->{$b}->{'name'}} keys (%$e)) {
104 print STDERR " <Value>\n";
105 print STDERR " <Name>$enc</Name>\n";
106 print STDERR " <Desc>$e->{$enc}->{'name'}</Desc>\n";
107 print STDERR " </Value>\n";
108 }
109 }
110
111 print STDERR " </List>\n";
112 }
113
114 # Show the default value for the option, if there is one
115 if (defined $option->{'deft'}) {
116 my $optiondeftvalue = $option->{'deft'};
117
118 # Escape '<' and '>' characters
119 $optiondeftvalue =~ s/</&lt;/g;
120 $optiondeftvalue =~ s/>/&gt;/g;
121
122 print STDERR " <Default>$optiondeftvalue</Default>\n";
123 }
124
125 print STDERR " </Option>\n";
126 }
127}
128
129
130sub find_longest_option_string
131{
132 local $options = shift(@_);
133
134 local $maxlength = 0;
135 foreach $option (@$options) {
136 local $optionname = $option->{'name'};
137 local $optiontype = $option->{'type'};
138
139 local $optionlength = length(" -$optionname");
140 if ($optiontype ne "flag") {
141 $optionlength = $optionlength + length(" <$optiontype>");
142 }
143
144 # Remember the longest
145 if ($optionlength > $maxlength) {
146 $maxlength = $optionlength;
147 }
148 }
149 return $maxlength;
150}
151
152
153sub print_options_txt
154{
155 local $language = shift(@_);
156 local $options = shift(@_);
157 local $optiondescoffset = shift(@_);
158
159 foreach $option (@$options) {
160 # Display option name
161 local $optionname = $option->{'name'};
162 print STDERR " -$optionname";
163 local $optionstringlength = length(" -$optionname");
164
165 # Display option type, if the option is not a flag
166 local $optiontype = $option->{'type'};
167 if ($optiontype ne "flag") {
168 print STDERR " <$optiontype>";
169 $optionstringlength = $optionstringlength + length(" <$optiontype>");
170 }
171
172 # Display the option description
173 local $optiondesc = &gsprintf::lookup_string($language, $option->{'desc'});
174 local $optionreqd = $option->{'reqd'};
175 if (defined($optionreqd) && $optionreqd eq "yes") {
176 $optiondesc = "(" . &gsprintf::lookup_string($language, "{PrintUsage.required}") . ") " . $optiondesc;
177 }
178 &display_text_in_column($optiondesc, $optiondescoffset, $optionstringlength, 80);
179
180 # Show the default value for the option, if there is one
181 local $optiondefault = $option->{'deft'};
182 if (defined($optiondefault)) {
183 print STDERR " " x $optiondescoffset;
184 print STDERR &gsprintf::lookup_string($language, "{PrintUsage.default}");
185 print STDERR ": " . &gsprintf::lookup_string($language, $optiondefault) . "\n";
186 }
187
188 # If the option has a list of possible values, display these
189 local $optionvalueslist = $option->{'list'};
190 if (defined($optionvalueslist)) {
191 print STDERR "\n";
192 foreach $optionvalue (@$optionvalueslist) {
193 local $optionvaluename = $optionvalue->{'name'};
194 print STDERR " " x $optiondescoffset;
195 print STDERR "$optionvaluename:";
196
197 local $optionvaluedesc = &gsprintf::lookup_string($language, $optionvalue->{'desc'});
198 &display_text_in_column($optionvaluedesc, $optiondescoffset + 2,
199 $optiondescoffset + length($optionvaluename), 80);
200 }
201 }
202
203 # Special case for 'input_encoding'
204 if ($optionname =~ m/^input_encoding$/i) {
205 my $e = $encodings::encodings;
206 foreach $enc (sort {$e->{$a}->{'name'} cmp $e->{$b}->{'name'}} keys (%$e)) {
207 print STDERR " " x $optiondescoffset;
208 print STDERR "$enc:";
209
210 local $encodingdesc = $e->{$enc}->{'name'};
211 &display_text_in_column($encodingdesc, $optiondescoffset + 2,
212 $optiondescoffset + length("$enc:"), 80);
213 }
214 }
215
216 # Add a blank line to separate options
217 print STDERR "\n";
218 }
219}
220
221
222sub display_text_in_column
223{
224 local ($text, $columnbeg, $firstlineoffset, $columnend) = @_;
225
226 # Spaces are put *before* words, so treat the column beginning as 1 smaller than it is
227 $columnbeg = $columnbeg - 1;
228
229 # Add some padding (if needed) for the first line
230 local $linelength = $columnbeg;
231 if ($firstlineoffset < $columnbeg) {
232 print STDERR " " x ($columnbeg - $firstlineoffset);
233 }
234 else {
235 $linelength = $firstlineoffset;
236 }
237
238 # Break the text into words, and display one at a time
239 local @words = split(/ /, $text);
240
241 foreach $word (@words) {
242 # If printing this word would exceed the column end, start a new line
243 if (($linelength + length($word)) >= $columnend) {
244 print STDERR "\n";
245 print STDERR " " x $columnbeg;
246 $linelength = $columnbeg;
247 }
248
249 # Write the word
250 print STDERR " $word";
251 $linelength = $linelength + length(" $word");
252 }
253
254 print STDERR "\n";
255}
256
257
2581;
Note: See TracBrowser for help on using the repository browser.