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
10 # Full author contact details are available in the file CREDITS
11 # or at https://www.lyx.org/Credits
13 # Used as wrapper for Getopt::Mixed
20 # my %options = %{&handleOptions(\%optionsDef)};
32 @EXPORT = qw(handleOptions);
38 sub makeOpts(); # Create option spec for Getopt::Mixed::init()
39 sub makeHelp(); # Create help-string to describe options
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
49 #option|param|type|aliases|comment
50 my $helpFormat = " %-8.8s|%-9.9s|%-7.7s|%-17.17s|%s\n";
54 if (ref($_[0]) eq "ARRAY") {
55 for (my $i = 0; defined($_[0]->[$i]); $i++) {
57 $optionsDef{$rO->[0]} = $rO->[1];
58 $optionsDef{$rO->[0]}->{Sort} = $i+2;
62 %optionsDef = %{$_[0]};
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}->{comment} = "Display recognized params";
70 $optionsDef{v}->{Sort} = 1;
72 my %options = ("help" => 0);
73 my $opts = &makeOpts();
75 Getopt::Mixed::init($opts);
76 while( my( $option, $value, $pretty ) = Getopt::Mixed::nextOption()) {
77 if (defined($optionsDef{$option})) {
78 my $fieldname = $optionsDef{$option}->{fieldname};
79 if (exists($options{$fieldname}) && ($option ne "h")) {
80 print "Option $option already set\n";
81 if (defined($options{$fieldname})) {
82 print "Value \"$value\" would overwrite ";
83 if (ref($options{$fieldname}) eq "ARRAY") {
84 print "\"" . join(',', @{$options{$fieldname}}) . "\"\n";
87 print "\"$options{$fieldname}\"\n";
94 print "Syntax: $0 options xxxx ...\n";
95 print "Available options:\n";
96 printf($helpFormat, "option", "param", "type", "aliases", "comment");
97 print " " . "-" x 90 . "\n";
98 my $optx = &makeHelp();
100 $options{$fieldname} = 1;
103 if (defined($optionsDef{$option}->{listsep})) {
104 my @list = split($optionsDef{$option}->{listsep}, $value);
105 $options{$fieldname} = \@list;
108 $options{$fieldname} = $value;
114 Getopt::Mixed::cleanup();
115 if (exists($options{verbose})) {
116 printf("Found following options:\n %-16soptvalue\n", "option");
117 print " " . "-" x 32 . "\n";
118 for my $k (sort keys %options) {
119 if (defined($options{$k})) {
121 if (ref($options{$k}) eq "ARRAY") {
122 $val = join(',', @{$options{$k}});
127 printf(" %-16s%s\n", $k, $val);
134 if ($options{help}) {
140 #############################################################
142 # Create option spec for Getopt::Mixed::init()
147 for my $ex (sort keys %optionsDef) {
148 my $e = $optionsDef{$ex};
154 if (defined($e->{type})) {
158 if (defined($e->{alias})) {
159 for my $a (@{$e->{alias}}) {
169 if (defined($optionsDef{$a}->{Sort})) {
170 if (defined($optionsDef{$b}->{Sort})) {
171 return $optionsDef{$a}->{Sort} <=> $optionsDef{$b}->{Sort};
175 if (defined($optionsDef{$b}->{Sort})) {
183 # Create help-string to describe options
194 for my $ex (sort sortHelp keys %optionsDef) {
195 my $e = $optionsDef{$ex};
201 if (defined($e->{type})) {
203 if ($tp =~ /^([:=])([sif])$/) {
204 $needed = $modifier{$1};
205 $partype = $modifier{$2};
208 print "wrong option type: $tp\n";
212 if (defined($e->{alias})) {
213 $aliases = join(',', @{$e->{alias}});
215 if (defined($e->{comment})) {
216 $comment = $e->{comment};
218 $opts .= sprintf($helpFormat, $ex, $needed, $partype, $aliases, $comment);
219 if (defined($e->{comment2})) {
221 $opts .= sprintf($helpFormat, $fill, $fill, $fill, $fill, $e->{comment2});
227 #############################################################