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::Long
20 # my %options = %{&handleOptions(\%optionsDef)};
32 @EXPORT = qw(handleOptions);
38 sub makeLongOpts(); # Create option spec for Getopt::Long::GetOptions();
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 use vars qw(%options);
73 %options = ("help" => 0);
76 my $roptr = &makeLongOpts();
77 my $p = Getopt::Long::Parser->new;
78 $p->configure("bundling");
79 $p->getoptions(%{$roptr});
82 # Callback routine called by $p->getoptions().
85 my ($option, $value, $unknown) = @_;
86 if (defined($optionsDef{$option})) {
87 my $fieldname = $optionsDef{$option}->{fieldname};
88 if (exists($options{$fieldname}) && ($option ne "h")) {
89 print "Option $option already set\n";
90 if (defined($options{$fieldname})) {
91 print "Value \"$value\" would overwrite ";
92 if (ref($options{$fieldname}) eq "ARRAY") {
93 print "\"" . join(',', @{$options{$fieldname}}) . "\"\n";
96 print "\"$options{$fieldname}\"\n";
102 if ($option eq "h") {
103 print "Syntax: $0 options xxxx ...\n";
104 print "Available options:\n";
105 printf($helpFormat, "option", "param", "type", "aliases", "comment");
106 print " " . "-" x 90 . "\n";
107 my $optx = &makeHelp();
109 $options{$fieldname} = 1;
112 if (defined($optionsDef{$option}->{listsep})) {
113 my @list = split(/(?<!\\)$optionsDef{$option}->{listsep}/, $value);
114 $options{$fieldname} = \@list;
117 $options{$fieldname} = $value;
123 if (exists($options{verbose})) {
124 printf("Found following options:\n %-16soptvalue\n", "option");
125 print " " . "-" x 32 . "\n";
126 for my $k (sort keys %options) {
127 if (defined($options{$k})) {
129 if (ref($options{$k}) eq "ARRAY") {
130 $val = join(',', @{$options{$k}});
135 printf(" %-16s%s\n", $k, $val);
142 if ($options{help}) {
148 #############################################################
150 # Create option spec for Getopt::Long::GetOptions()
154 for my $ex (sort keys %optionsDef) {
155 my $e = $optionsDef{$ex};
157 if (defined($e->{type})) {
161 if (defined($e->{alias})) {
162 for my $a (@{$e->{alias}}) {
166 $opts{"$optx$type"} = \&handleopts;
168 return \%opts; # to be used by Getopt::Long();
173 if (defined($optionsDef{$a}->{Sort})) {
174 if (defined($optionsDef{$b}->{Sort})) {
175 return $optionsDef{$a}->{Sort} <=> $optionsDef{$b}->{Sort};
179 if (defined($optionsDef{$b}->{Sort})) {
187 # Create help-string to describe options
198 for my $ex (sort sortHelp keys %optionsDef) {
199 my $e = $optionsDef{$ex};
205 if (defined($e->{type})) {
207 if ($tp =~ /^([:=])([sif])$/) {
208 $needed = $modifier{$1};
209 $partype = $modifier{$2};
212 print "wrong option type: $tp\n";
216 if (defined($e->{alias})) {
217 $aliases = join(',', @{$e->{alias}});
219 if (defined($e->{comment})) {
220 $comment = $e->{comment};
222 $opts .= sprintf($helpFormat, $ex, $needed, $partype, $aliases, $comment);
223 if (defined($e->{comment2})) {
225 $opts .= sprintf($helpFormat, $fill, $fill, $fill, $fill, $e->{comment2});
231 #############################################################