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

Last change on this file since 6408 was 6408, checked in by jmt12, 20 years ago

Added two new attributes for script arguments. HiddenGLI controls whether the argument will be visible at all in GLI, while ModeGLI defines the lowest detail mode under which the argument will be visible (only really for import and buildcol). Also ensured that the scripts were reporting their correct default process expressions, and further refined argument types by adding the catagory regexp for any regular expression (which can then be hidden under lower detail modes in GLI)

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