]> git.lyx.org Git - lyx.git/blob - development/tools/GetOptions.pm
Tools(listFontWithLang.pl): Group related parameters in the command syntax
[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+1;
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} = 0;
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 # Create help-string to describe options
146 sub makeHelp()
147 {
148   my $opts = "";
149   my %modifier = (
150     ":" => "optional",
151     "=" => "required",
152     "s" => "string",
153     "i" => "integer",
154     "f" => "float",
155       );
156   for my $ex (sort {$optionsDef{$a}->{Sort} <=> $optionsDef{$b}->{Sort};} keys %optionsDef) {
157     my $e = $optionsDef{$ex};
158     my $type = "";
159     my $needed = "";
160     my $partype = "";
161     my $aliases = "";
162     my $comment = "";
163     if (defined($e->{type})) {
164       my $tp = $e->{type};
165       if ($tp =~ /^([:=])([sif])$/) {
166         $needed = $modifier{$1};
167         $partype = $modifier{$2};
168       }
169       else {
170         print "wrong option type: $tp\n";
171         exit(1);
172       }
173     }
174     if (defined($e->{alias})) {
175       $aliases = join(',', @{$e->{alias}});
176     }
177     if (defined($e->{comment})) {
178       $comment = $e->{comment};
179     }
180     $opts .= sprintf($helpFormat, $ex, $needed, $partype, $aliases, $comment);
181   }
182   return($opts);
183 }
184
185 #############################################################
186 1;
187