2020-05-02 11:20:53 +00:00
|
|
|
#! /usr/bin/env perl
|
|
|
|
# -*- mode: perl; -*-
|
|
|
|
|
|
|
|
# file GetOptions.pm
|
|
|
|
# This file is part of LyX, the document processor.
|
|
|
|
# Licence details can be found in the file COPYING
|
|
|
|
# or at http://www.lyx.org/about/licence.php
|
|
|
|
#
|
|
|
|
# author Kornel Benko
|
|
|
|
# Full author contact details are available in the file CREDITS
|
|
|
|
# or at https://www.lyx.org/Credits
|
|
|
|
#
|
2022-02-05 19:04:06 +00:00
|
|
|
# Used as wrapper for Getopt::Long
|
2020-05-02 11:20:53 +00:00
|
|
|
# as
|
|
|
|
# use GetOptions;
|
|
|
|
# ...
|
|
|
|
# my %optionsDef = (
|
|
|
|
# ...
|
|
|
|
# );
|
|
|
|
# my %options = %{&handleOptions(\%optionsDef)};
|
|
|
|
|
|
|
|
package GetOptions;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
our(@EXPORT, @ISA);
|
|
|
|
|
|
|
|
sub handleOptions($);
|
|
|
|
|
|
|
|
BEGIN {
|
|
|
|
use Exporter ();
|
|
|
|
@ISA = qw(Exporter);
|
|
|
|
@EXPORT = qw(handleOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
use warnings;
|
2022-02-05 19:04:06 +00:00
|
|
|
use Getopt::Long;
|
2020-05-02 11:20:53 +00:00
|
|
|
|
2022-02-05 19:04:06 +00:00
|
|
|
sub makeLongOpts(); # Create option spec for Getopt::Long::GetOptions();
|
2020-05-02 11:20:53 +00:00
|
|
|
sub makeHelp(); # Create help-string to describe options
|
|
|
|
|
|
|
|
# Following fields for a parameter can be defined:
|
|
|
|
# fieldname: Name of entry in %options
|
|
|
|
# type: [:=][sif], ':' = optional, '=' = required, 's' = string, 'i' = integer, 'f' = float
|
|
|
|
# alias: reference to a list of aliases e.g. ["alias1", "alias2", ... ]
|
|
|
|
# listsep: Separator for multiple data
|
|
|
|
# comment: Parameter description
|
|
|
|
|
|
|
|
my %optionsDef = ();
|
|
|
|
#option|param|type|aliases|comment
|
2020-05-30 12:29:39 +00:00
|
|
|
my $helpFormat = " %-8.8s|%-9.9s|%-7.7s|%-17.17s|%s\n";
|
2020-05-02 11:20:53 +00:00
|
|
|
|
|
|
|
sub handleOptions($)
|
|
|
|
{
|
2020-05-16 19:40:26 +00:00
|
|
|
if (ref($_[0]) eq "ARRAY") {
|
|
|
|
for (my $i = 0; defined($_[0]->[$i]); $i++) {
|
|
|
|
my $rO = $_[0]->[$i];
|
|
|
|
$optionsDef{$rO->[0]} = $rO->[1];
|
2020-05-18 16:21:25 +00:00
|
|
|
$optionsDef{$rO->[0]}->{Sort} = $i+2;
|
2020-05-16 19:40:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
%optionsDef = %{$_[0]};
|
|
|
|
}
|
2020-05-02 11:20:53 +00:00
|
|
|
$optionsDef{h}->{fieldname} = "help";
|
|
|
|
$optionsDef{h}->{alias} = ["help"];
|
2020-05-16 19:40:26 +00:00
|
|
|
$optionsDef{h}->{Sort} = 0;
|
2020-05-02 11:20:53 +00:00
|
|
|
$optionsDef{v}->{fieldname} = "verbose";
|
|
|
|
$optionsDef{v}->{alias} = ["verbose"];
|
2020-05-30 12:29:39 +00:00
|
|
|
$optionsDef{v}->{comment} = "Display recognized params";
|
2020-05-18 16:21:25 +00:00
|
|
|
$optionsDef{v}->{Sort} = 1;
|
2020-05-02 11:20:53 +00:00
|
|
|
|
2022-02-05 19:04:06 +00:00
|
|
|
use vars qw(%options);
|
|
|
|
%options = ("help" => 0);
|
2020-05-02 11:20:53 +00:00
|
|
|
|
2022-02-05 19:04:06 +00:00
|
|
|
{
|
|
|
|
my $roptr = &makeLongOpts();
|
|
|
|
my $p = Getopt::Long::Parser->new;
|
|
|
|
$p->configure("bundling");
|
|
|
|
$p->getoptions(%{$roptr});
|
|
|
|
}
|
|
|
|
|
|
|
|
# Callback routine called by $p->getoptions().
|
|
|
|
sub handleopts($$$)
|
|
|
|
{
|
|
|
|
my ($option, $value, $unknown) = @_;
|
2020-05-02 11:20:53 +00:00
|
|
|
if (defined($optionsDef{$option})) {
|
|
|
|
my $fieldname = $optionsDef{$option}->{fieldname};
|
2020-05-30 12:29:39 +00:00
|
|
|
if (exists($options{$fieldname}) && ($option ne "h")) {
|
2022-02-05 19:04:06 +00:00
|
|
|
print "Option $option already set\n";
|
2020-05-30 12:29:39 +00:00
|
|
|
if (defined($options{$fieldname})) {
|
|
|
|
print "Value \"$value\" would overwrite ";
|
|
|
|
if (ref($options{$fieldname}) eq "ARRAY") {
|
|
|
|
print "\"" . join(',', @{$options{$fieldname}}) . "\"\n";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
print "\"$options{$fieldname}\"\n";
|
|
|
|
}
|
|
|
|
}
|
2022-02-05 19:04:06 +00:00
|
|
|
$option = "h";
|
|
|
|
$fieldname = "help";
|
2020-05-21 14:09:21 +00:00
|
|
|
}
|
2020-05-02 11:20:53 +00:00
|
|
|
if ($option eq "h") {
|
|
|
|
print "Syntax: $0 options xxxx ...\n";
|
|
|
|
print "Available options:\n";
|
|
|
|
printf($helpFormat, "option", "param", "type", "aliases", "comment");
|
|
|
|
print " " . "-" x 90 . "\n";
|
|
|
|
my $optx = &makeHelp();
|
|
|
|
print "$optx";
|
|
|
|
$options{$fieldname} = 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (defined($optionsDef{$option}->{listsep})) {
|
2020-07-11 10:46:26 +00:00
|
|
|
my @list = split(/(?<!\\)$optionsDef{$option}->{listsep}/, $value);
|
2020-05-02 11:20:53 +00:00
|
|
|
$options{$fieldname} = \@list;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$options{$fieldname} = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (exists($options{verbose})) {
|
|
|
|
printf("Found following options:\n %-16soptvalue\n", "option");
|
|
|
|
print " " . "-" x 32 . "\n";
|
|
|
|
for my $k (sort keys %options) {
|
|
|
|
if (defined($options{$k})) {
|
2020-05-21 09:50:42 +00:00
|
|
|
my $val;
|
|
|
|
if (ref($options{$k}) eq "ARRAY") {
|
|
|
|
$val = join(',', @{$options{$k}});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$val = $options{$k};
|
|
|
|
}
|
|
|
|
printf(" %-16s%s\n", $k, $val);
|
2020-05-02 11:20:53 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
print " $k\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($options{help}) {
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
return \%options;
|
|
|
|
}
|
|
|
|
|
|
|
|
#############################################################
|
|
|
|
|
2022-02-05 19:04:06 +00:00
|
|
|
# Create option spec for Getopt::Long::GetOptions()
|
|
|
|
sub makeLongOpts()
|
2020-05-02 11:20:53 +00:00
|
|
|
{
|
2022-02-05 19:04:06 +00:00
|
|
|
my %opts = ();
|
2020-05-02 11:20:53 +00:00
|
|
|
for my $ex (sort keys %optionsDef) {
|
|
|
|
my $e = $optionsDef{$ex};
|
2022-02-05 19:04:06 +00:00
|
|
|
my $type = "";
|
2020-05-02 11:20:53 +00:00
|
|
|
if (defined($e->{type})) {
|
2022-02-05 19:04:06 +00:00
|
|
|
$type = $e->{type};
|
2020-05-02 11:20:53 +00:00
|
|
|
}
|
2022-02-05 19:04:06 +00:00
|
|
|
my $optx = $ex;
|
2020-05-02 11:20:53 +00:00
|
|
|
if (defined($e->{alias})) {
|
|
|
|
for my $a (@{$e->{alias}}) {
|
2022-02-05 19:04:06 +00:00
|
|
|
$optx .= "|$a";
|
2020-05-02 11:20:53 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-05 19:04:06 +00:00
|
|
|
$opts{"$optx$type"} = \&handleopts;
|
2020-05-02 11:20:53 +00:00
|
|
|
}
|
2022-02-05 19:04:06 +00:00
|
|
|
return \%opts; # to be used by Getopt::Long();
|
2020-05-02 11:20:53 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 16:21:25 +00:00
|
|
|
sub sortHelp
|
|
|
|
{
|
|
|
|
if (defined($optionsDef{$a}->{Sort})) {
|
|
|
|
if (defined($optionsDef{$b}->{Sort})) {
|
|
|
|
return $optionsDef{$a}->{Sort} <=> $optionsDef{$b}->{Sort};
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (defined($optionsDef{$b}->{Sort})) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $a cmp $b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-02 11:20:53 +00:00
|
|
|
# Create help-string to describe options
|
|
|
|
sub makeHelp()
|
|
|
|
{
|
|
|
|
my $opts = "";
|
|
|
|
my %modifier = (
|
|
|
|
":" => "optional",
|
|
|
|
"=" => "required",
|
|
|
|
"s" => "string",
|
|
|
|
"i" => "integer",
|
|
|
|
"f" => "float",
|
|
|
|
);
|
2020-05-18 16:21:25 +00:00
|
|
|
for my $ex (sort sortHelp keys %optionsDef) {
|
2020-05-02 11:20:53 +00:00
|
|
|
my $e = $optionsDef{$ex};
|
|
|
|
my $type = "";
|
|
|
|
my $needed = "";
|
|
|
|
my $partype = "";
|
|
|
|
my $aliases = "";
|
|
|
|
my $comment = "";
|
|
|
|
if (defined($e->{type})) {
|
|
|
|
my $tp = $e->{type};
|
|
|
|
if ($tp =~ /^([:=])([sif])$/) {
|
|
|
|
$needed = $modifier{$1};
|
|
|
|
$partype = $modifier{$2};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
print "wrong option type: $tp\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (defined($e->{alias})) {
|
|
|
|
$aliases = join(',', @{$e->{alias}});
|
|
|
|
}
|
|
|
|
if (defined($e->{comment})) {
|
|
|
|
$comment = $e->{comment};
|
|
|
|
}
|
|
|
|
$opts .= sprintf($helpFormat, $ex, $needed, $partype, $aliases, $comment);
|
2020-05-30 12:29:39 +00:00
|
|
|
if (defined($e->{comment2})) {
|
|
|
|
my $fill = "_" x 20;
|
|
|
|
$opts .= sprintf($helpFormat, $fill, $fill, $fill, $fill, $e->{comment2});
|
|
|
|
}
|
2020-05-02 11:20:53 +00:00
|
|
|
}
|
|
|
|
return($opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
#############################################################
|
|
|
|
1;
|
|
|
|
|