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+1;
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} = 0;
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};
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();
85 $options{$fieldname} = 1;
88 if (defined($optionsDef{$option}->{listsep})) {
89 my @list = split($optionsDef{$option}->{listsep}, $value);
90 $options{$fieldname} = \@list;
93 $options{$fieldname} = $value;
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 printf(" %-16s%s\n", $k, $options{$k});
112 if ($options{help}) {
118 #############################################################
120 # Create option spec for Getopt::Mixed::init()
125 for my $ex (sort keys %optionsDef) {
126 my $e = $optionsDef{$ex};
132 if (defined($e->{type})) {
136 if (defined($e->{alias})) {
137 for my $a (@{$e->{alias}}) {
145 # Create help-string to describe options
156 for my $ex (sort {$optionsDef{$a}->{Sort} <=> $optionsDef{$b}->{Sort};} keys %optionsDef) {
157 my $e = $optionsDef{$ex};
163 if (defined($e->{type})) {
165 if ($tp =~ /^([:=])([sif])$/) {
166 $needed = $modifier{$1};
167 $partype = $modifier{$2};
170 print "wrong option type: $tp\n";
174 if (defined($e->{alias})) {
175 $aliases = join(',', @{$e->{alias}});
177 if (defined($e->{comment})) {
178 $comment = $e->{comment};
180 $opts .= sprintf($helpFormat, $ex, $needed, $partype, $aliases, $comment);
185 #############################################################