]> git.lyx.org Git - features.git/blob - development/tools/GetOptions.pm
Try to set executable bit on listFontWithLang.pl
[features.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   %optionsDef = %{$_[0]};
55   $optionsDef{h}->{fieldname} = "help";
56   $optionsDef{h}->{alias} = ["help"];
57   $optionsDef{v}->{fieldname} = "verbose";
58   $optionsDef{v}->{alias} = ["verbose"];
59
60   my %options = ("help" => 0);
61   my $opts = &makeOpts();
62
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};
67       if ($option eq "h") {
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();
73         print "$optx";
74         $options{$fieldname} = 1;
75       }
76       else {
77         if (defined($optionsDef{$option}->{listsep})) {
78           my @list = split($optionsDef{$option}->{listsep}, $value);
79           $options{$fieldname} = \@list;
80         }
81         else {
82           $options{$fieldname} = $value;
83         }
84       }
85     }
86   }
87
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});
95       }
96       else {
97         print "    $k\n";
98       }
99     }
100   }
101   if ($options{help}) {
102     exit 0;
103   }
104   return \%options;
105 }
106
107 #############################################################
108
109 # Create option spec for Getopt::Mixed::init()
110 sub makeOpts()
111 {
112   my $first = 1;
113   my $opts = "";
114   for my $ex (sort keys %optionsDef) {
115     my $e = $optionsDef{$ex};
116     if (! $first) {
117       $opts .= " ";
118     }
119     $first = 0;
120     $opts .= $ex;
121     if (defined($e->{type})) {
122       my $tp = $e->{type};
123       $opts .= $tp;
124     }
125     if (defined($e->{alias})) {
126       for my $a (@{$e->{alias}}) {
127         $opts .= " $a>$ex";
128       }
129     }
130   }
131   return($opts);
132 }
133
134 # Create help-string to describe options
135 sub makeHelp()
136 {
137   my $opts = "";
138   my %modifier = (
139     ":" => "optional",
140     "=" => "required",
141     "s" => "string",
142     "i" => "integer",
143     "f" => "float",
144       );
145   for my $ex (sort keys %optionsDef) {
146     my $e = $optionsDef{$ex};
147     my $type = "";
148     my $needed = "";
149     my $partype = "";
150     my $aliases = "";
151     my $comment = "";
152     if (defined($e->{type})) {
153       my $tp = $e->{type};
154       if ($tp =~ /^([:=])([sif])$/) {
155         $needed = $modifier{$1};
156         $partype = $modifier{$2};
157       }
158       else {
159         print "wrong option type: $tp\n";
160         exit(1);
161       }
162     }
163     if (defined($e->{alias})) {
164       $aliases = join(',', @{$e->{alias}});
165     }
166     if (defined($e->{comment})) {
167       $comment = $e->{comment};
168     }
169     $opts .= sprintf($helpFormat, $ex, $needed, $partype, $aliases, $comment);
170   }
171   return($opts);
172 }
173
174 #############################################################
175 1;
176