]> git.lyx.org Git - lyx.git/blob - development/tools/GetOptions.pm
Tools(listFontWithLang.pl): Polishing
[lyx.git] / development / tools / GetOptions.pm
1 #! /usr/bin/env perl
2 # -*- mode: perl; -*-
3
4 # file GetOptions.pm
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING
7 # or at http://www.lyx.org/about/licence.php
8 #
9 # author Kornel Benko
10 # Full author contact details are available in the file CREDITS
11 # or at https://www.lyx.org/Credits
12 #
13 # Used as wrapper for Getopt::Mixed
14 # as
15 #    use GetOptions;
16 #    ...
17 #    my %optionsDef = (
18 #    ...
19 #    );
20 #    my %options = %{&handleOptions(\%optionsDef)};
21
22 package GetOptions;
23
24 use strict;
25 our(@EXPORT, @ISA);
26
27 sub handleOptions($);
28
29 BEGIN {
30   use Exporter   ();
31   @ISA        = qw(Exporter);
32   @EXPORT     = qw(handleOptions);
33 }
34
35 use warnings;
36 use Getopt::Mixed;
37
38 sub makeOpts();            # Create option spec for Getopt::Mixed::init()
39 sub makeHelp();            # Create help-string to describe options
40
41 # Following fields for a parameter can be defined:
42 # fieldname:         Name of entry in %options
43 # type:              [:=][sif], ':' = optional, '=' = required, 's' = string, 'i' = integer, 'f' = float
44 # alias:             reference to a list of aliases e.g. ["alias1", "alias2", ... ]
45 # listsep:           Separator for multiple data
46 # comment:           Parameter description
47
48 my %optionsDef = ();
49 #option|param|type|aliases|comment
50 my $helpFormat = "  %-8s|%-9s|%-7s|%-17s|%s\n";
51
52 sub handleOptions($)
53 {
54   if (ref($_[0]) eq "ARRAY") {
55     for (my $i = 0; defined($_[0]->[$i]); $i++) {
56       my $rO = $_[0]->[$i];
57       $optionsDef{$rO->[0]} = $rO->[1];
58       $optionsDef{$rO->[0]}->{Sort} = $i+2;
59     }
60   }
61   else {
62     %optionsDef = %{$_[0]};
63   }
64   $optionsDef{h}->{fieldname} = "help";
65   $optionsDef{h}->{alias} = ["help"];
66   $optionsDef{h}->{Sort} = 0;
67   $optionsDef{v}->{fieldname} = "verbose";
68   $optionsDef{v}->{alias} = ["verbose"];
69   $optionsDef{v}->{Sort} = 1;
70
71   my %options = ("help" => 0);
72   my $opts = &makeOpts();
73
74   Getopt::Mixed::init($opts);
75   while( my( $option, $value, $pretty ) = Getopt::Mixed::nextOption()) {
76     if (defined($optionsDef{$option})) {
77       my $fieldname = $optionsDef{$option}->{fieldname};
78       if ($option eq "h") {
79         print "Syntax: $0 options xxxx ...\n";
80         print "Available options:\n";
81         printf($helpFormat, "option", "param", "type", "aliases", "comment");
82         print "  " . "-" x 90 . "\n";
83         my $optx = &makeHelp();
84         print "$optx";
85         $options{$fieldname} = 1;
86       }
87       else {
88         if (defined($optionsDef{$option}->{listsep})) {
89           my @list = split($optionsDef{$option}->{listsep}, $value);
90           $options{$fieldname} = \@list;
91         }
92         else {
93           $options{$fieldname} = $value;
94         }
95       }
96     }
97   }
98
99   Getopt::Mixed::cleanup();
100   if (exists($options{verbose})) {
101     printf("Found following options:\n    %-16soptvalue\n", "option");
102     print "    " . "-" x 32 . "\n";
103     for my $k (sort keys %options) {
104       if (defined($options{$k})) {
105         my $val;
106         if (ref($options{$k}) eq "ARRAY") {
107           $val = join(',', @{$options{$k}});
108         }
109         else {
110           $val = $options{$k};
111         }
112         printf("    %-16s%s\n", $k, $val);
113       }
114       else {
115         print "    $k\n";
116       }
117     }
118   }
119   if ($options{help}) {
120     exit 0;
121   }
122   return \%options;
123 }
124
125 #############################################################
126
127 # Create option spec for Getopt::Mixed::init()
128 sub makeOpts()
129 {
130   my $first = 1;
131   my $opts = "";
132   for my $ex (sort keys %optionsDef) {
133     my $e = $optionsDef{$ex};
134     if (! $first) {
135       $opts .= " ";
136     }
137     $first = 0;
138     $opts .= $ex;
139     if (defined($e->{type})) {
140       my $tp = $e->{type};
141       $opts .= $tp;
142     }
143     if (defined($e->{alias})) {
144       for my $a (@{$e->{alias}}) {
145         $opts .= " $a>$ex";
146       }
147     }
148   }
149   return($opts);
150 }
151
152 sub sortHelp
153 {
154   if (defined($optionsDef{$a}->{Sort})) {
155     if (defined($optionsDef{$b}->{Sort})) {
156       return $optionsDef{$a}->{Sort} <=> $optionsDef{$b}->{Sort};
157     }
158     return -1;
159   }
160   if (defined($optionsDef{$b}->{Sort})) {
161     return 1;
162   }
163   else {
164     return $a cmp $b;
165   }
166 }
167
168 # Create help-string to describe options
169 sub makeHelp()
170 {
171   my $opts = "";
172   my %modifier = (
173     ":" => "optional",
174     "=" => "required",
175     "s" => "string",
176     "i" => "integer",
177     "f" => "float",
178       );
179   for my $ex (sort sortHelp keys %optionsDef) {
180     my $e = $optionsDef{$ex};
181     my $type = "";
182     my $needed = "";
183     my $partype = "";
184     my $aliases = "";
185     my $comment = "";
186     if (defined($e->{type})) {
187       my $tp = $e->{type};
188       if ($tp =~ /^([:=])([sif])$/) {
189         $needed = $modifier{$1};
190         $partype = $modifier{$2};
191       }
192       else {
193         print "wrong option type: $tp\n";
194         exit(1);
195       }
196     }
197     if (defined($e->{alias})) {
198       $aliases = join(',', @{$e->{alias}});
199     }
200     if (defined($e->{comment})) {
201       $comment = $e->{comment};
202     }
203     $opts .= sprintf($helpFormat, $ex, $needed, $partype, $aliases, $comment);
204   }
205   return($opts);
206 }
207
208 #############################################################
209 1;
210