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 = " %-8s|%-9s|%-7s|%-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}->{Sort} = 1;
71 my %options = ("help" => 0);
72 my $opts = &makeOpts();
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 (exists($options{$fieldname})) {
79 print "Option $option already set\n";
80 print "Value \"$value\" would overwrite ";
81 if (ref($options{$fieldname}) eq "ARRAY") {
82 print "\"" . join(',', @{$options{$fieldname}}) . "\"\n";
85 print "\"$options{$fieldname}\"\n";
91 print "Syntax: $0 options xxxx ...\n";
92 print "Available options:\n";
93 printf($helpFormat, "option", "param", "type", "aliases", "comment");
94 print " " . "-" x 90 . "\n";
95 my $optx = &makeHelp();
97 $options{$fieldname} = 1;
100 if (defined($optionsDef{$option}->{listsep})) {
101 my @list = split($optionsDef{$option}->{listsep}, $value);
102 $options{$fieldname} = \@list;
105 $options{$fieldname} = $value;
111 Getopt::Mixed::cleanup();
112 if (exists($options{verbose})) {
113 printf("Found following options:\n %-16soptvalue\n", "option");
114 print " " . "-" x 32 . "\n";
115 for my $k (sort keys %options) {
116 if (defined($options{$k})) {
118 if (ref($options{$k}) eq "ARRAY") {
119 $val = join(',', @{$options{$k}});
124 printf(" %-16s%s\n", $k, $val);
131 if ($options{help}) {
137 #############################################################
139 # Create option spec for Getopt::Mixed::init()
144 for my $ex (sort keys %optionsDef) {
145 my $e = $optionsDef{$ex};
151 if (defined($e->{type})) {
155 if (defined($e->{alias})) {
156 for my $a (@{$e->{alias}}) {
166 if (defined($optionsDef{$a}->{Sort})) {
167 if (defined($optionsDef{$b}->{Sort})) {
168 return $optionsDef{$a}->{Sort} <=> $optionsDef{$b}->{Sort};
172 if (defined($optionsDef{$b}->{Sort})) {
180 # Create help-string to describe options
191 for my $ex (sort sortHelp keys %optionsDef) {
192 my $e = $optionsDef{$ex};
198 if (defined($e->{type})) {
200 if ($tp =~ /^([:=])([sif])$/) {
201 $needed = $modifier{$1};
202 $partype = $modifier{$2};
205 print "wrong option type: $tp\n";
209 if (defined($e->{alias})) {
210 $aliases = join(',', @{$e->{alias}});
212 if (defined($e->{comment})) {
213 $comment = $e->{comment};
215 $opts .= sprintf($helpFormat, $ex, $needed, $partype, $aliases, $comment);
220 #############################################################