source: main/tags/2.40/gsdl/perllib/printusage.pm@ 25668

Last change on this file since 25668 was 4777, checked in by mdewsnip, 21 years ago

A module containing methods for automatically generating usage text from the $options and $arguments structures defined in plugins, classifiers, import.pl, buildcol.pl, mkcol.pl etc.

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