]> git.lyx.org Git - lyx.git/blob - development/tools/GetOptions.pm
Tools(listFontWithLang.pl): Select fonts containig specified glyphs
[lyx.git] / development / tools / GetOptions.pm
1 #! /usr/bin/env perl
2 # -*- mode: perl; -*-
3
4 # file GetOptions.pm
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
8 #
9 # author Kornel Benko
10 # Full author contact details are available in the file CREDITS
11 # or at https://www.lyx.org/Credits
12 #
13 # Used as wrapper for Getopt::Mixed
14 # as
15 #    use GetOptions;
16 #    ...
17 #    my %optionsDef = (
18 #    ...
19 #    );
20 #    my %options = %{&handleOptions(\%optionsDef)};
21
22 package GetOptions;
23
24 use strict;
25 our(@EXPORT, @ISA);
26
27 sub handleOptions($);
28
29 BEGIN {
30   use Exporter   ();
31   @ISA        = qw(Exporter);
32   @EXPORT     = qw(handleOptions);
33 }
34
35 use warnings;
36 use Getopt::Mixed;
37
38 sub makeOpts();            # Create option spec for Getopt::Mixed::init()
39 sub makeHelp();            # Create help-string to describe options
40
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
47
48 my %optionsDef = ();
49 #option|param|type|aliases|comment
50 my $helpFormat = "  %-8s|%-9s|%-7s|%-17s|%s\n";
51
52 sub handleOptions($)
53 {
54   if (ref($_[0]) eq "ARRAY") {
55     for (my $i = 0; defined($_[0]->[$i]); $i++) {
56       my $rO = $_[0]->[$i];
57       $optionsDef{$rO->[0]} = $rO->[1];
58       $optionsDef{$rO->[0]}->{Sort} = $i+2;
59     }
60   }
61   else {
62     %optionsDef = %{$_[0]};
63   }
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;
70
71   my %options = ("help" => 0);
72   my $opts = &makeOpts();
73
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 ($option eq "h") {
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();
84         print "$optx";
85         $options{$fieldname} = 1;
86       }
87       else {
88         if (defined($optionsDef{$option}->{listsep})) {
89           my @list = split($optionsDef{$option}->{listsep}, $value);
90           $options{$fieldname} = \@list;
91         }
92         else {
93           $options{$fieldname} = $value;
94         }
95       }
96     }
97   }
98
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});
106       }
107       else {
108         print "    $k\n";
109       }
110     }
111   }
112   if ($options{help}) {
113     exit 0;
114   }
115   return \%options;
116 }
117
118 #############################################################
119
120 # Create option spec for Getopt::Mixed::init()
121 sub makeOpts()
122 {
123   my $first = 1;
124   my $opts = "";
125   for my $ex (sort keys %optionsDef) {
126     my $e = $optionsDef{$ex};
127     if (! $first) {
128       $opts .= " ";
129     }
130     $first = 0;
131     $opts .= $ex;
132     if (defined($e->{type})) {
133       my $tp = $e->{type};
134       $opts .= $tp;
135     }
136     if (defined($e->{alias})) {
137       for my $a (@{$e->{alias}}) {
138         $opts .= " $a>$ex";
139       }
140     }
141   }
142   return($opts);
143 }
144
145 sub sortHelp
146 {
147   if (defined($optionsDef{$a}->{Sort})) {
148     if (defined($optionsDef{$b}->{Sort})) {
149       return $optionsDef{$a}->{Sort} <=> $optionsDef{$b}->{Sort};
150     }
151     return -1;
152   }
153   if (defined($optionsDef{$b}->{Sort})) {
154     return 1;
155   }
156   else {
157     return $a cmp $b;
158   }
159 }
160
161 # Create help-string to describe options
162 sub makeHelp()
163 {
164   my $opts = "";
165   my %modifier = (
166     ":" => "optional",
167     "=" => "required",
168     "s" => "string",
169     "i" => "integer",
170     "f" => "float",
171       );
172   for my $ex (sort sortHelp keys %optionsDef) {
173     my $e = $optionsDef{$ex};
174     my $type = "";
175     my $needed = "";
176     my $partype = "";
177     my $aliases = "";
178     my $comment = "";
179     if (defined($e->{type})) {
180       my $tp = $e->{type};
181       if ($tp =~ /^([:=])([sif])$/) {
182         $needed = $modifier{$1};
183         $partype = $modifier{$2};
184       }
185       else {
186         print "wrong option type: $tp\n";
187         exit(1);
188       }
189     }
190     if (defined($e->{alias})) {
191       $aliases = join(',', @{$e->{alias}});
192     }
193     if (defined($e->{comment})) {
194       $comment = $e->{comment};
195     }
196     $opts .= sprintf($helpFormat, $ex, $needed, $partype, $aliases, $comment);
197   }
198   return($opts);
199 }
200
201 #############################################################
202 1;
203