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 %optionsDef = %{$_[0]};
55 $optionsDef{h}->{fieldname} = "help";
56 $optionsDef{h}->{alias} = ["help"];
57 $optionsDef{v}->{fieldname} = "verbose";
58 $optionsDef{v}->{alias} = ["verbose"];
60 my %options = ("help" => 0);
61 my $opts = &makeOpts();
63 Getopt::Mixed::init($opts);
64 while( my( $option, $value, $pretty ) = Getopt::Mixed::nextOption()) {
65 if (defined($optionsDef{$option})) {
66 my $fieldname = $optionsDef{$option}->{fieldname};
68 print "Syntax: $0 options xxxx ...\n";
69 print "Available options:\n";
70 printf($helpFormat, "option", "param", "type", "aliases", "comment");
71 print " " . "-" x 90 . "\n";
72 my $optx = &makeHelp();
74 $options{$fieldname} = 1;
77 if (defined($optionsDef{$option}->{listsep})) {
78 my @list = split($optionsDef{$option}->{listsep}, $value);
79 $options{$fieldname} = \@list;
82 $options{$fieldname} = $value;
88 Getopt::Mixed::cleanup();
89 if (exists($options{verbose})) {
90 printf("Found following options:\n %-16soptvalue\n", "option");
91 print " " . "-" x 32 . "\n";
92 for my $k (sort keys %options) {
93 if (defined($options{$k})) {
94 printf(" %-16s%s\n", $k, $options{$k});
101 if ($options{help}) {
107 #############################################################
109 # Create option spec for Getopt::Mixed::init()
114 for my $ex (sort keys %optionsDef) {
115 my $e = $optionsDef{$ex};
121 if (defined($e->{type})) {
125 if (defined($e->{alias})) {
126 for my $a (@{$e->{alias}}) {
134 # Create help-string to describe options
145 for my $ex (sort keys %optionsDef) {
146 my $e = $optionsDef{$ex};
152 if (defined($e->{type})) {
154 if ($tp =~ /^([:=])([sif])$/) {
155 $needed = $modifier{$1};
156 $partype = $modifier{$2};
159 print "wrong option type: $tp\n";
163 if (defined($e->{alias})) {
164 $aliases = join(',', @{$e->{alias}});
166 if (defined($e->{comment})) {
167 $comment = $e->{comment};
169 $opts .= sprintf($helpFormat, $ex, $needed, $partype, $aliases, $comment);
174 #############################################################