Refactored the options parsing to be less bad. Now time to implement actual functionalitygit add .
This commit is contained in:
parent
99fc573b3c
commit
f75fff9c98
1 changed files with 148 additions and 137 deletions
75
staticgen.pl
75
staticgen.pl
|
@ -7,12 +7,19 @@ use Carp;
|
||||||
|
|
||||||
$SIG{__WARN__} = sub { die @_; };
|
$SIG{__WARN__} = sub { die @_; };
|
||||||
|
|
||||||
|
my $PRG = $0;
|
||||||
|
$PRG =~ s!.*/!!;
|
||||||
|
|
||||||
|
#-----------------------------------------------------------------------
|
||||||
|
# Command line argument processing routines
|
||||||
|
#-----------------------------------------------------------------------
|
||||||
|
|
||||||
sub DisplayUsage
|
sub DisplayUsage
|
||||||
{
|
{
|
||||||
my $options_ref = shift;
|
my $options_ref = shift;
|
||||||
my @options = @$options_ref;
|
my @options = @$options_ref;
|
||||||
|
|
||||||
print "Usage:\n";
|
print "Usage: $PRG [OPTIONS]\n";
|
||||||
for my $opt (@options)
|
for my $opt (@options)
|
||||||
{
|
{
|
||||||
my $opt_str = " " . (defined $opt->{sopt} ? "-$opt->{sopt}, " : " ");
|
my $opt_str = " " . (defined $opt->{sopt} ? "-$opt->{sopt}, " : " ");
|
||||||
|
@ -37,7 +44,7 @@ sub HandleArgs
|
||||||
{
|
{
|
||||||
my ($args_ref, $options_ref) = @_;
|
my ($args_ref, $options_ref) = @_;
|
||||||
|
|
||||||
my @args = @$args_ref;
|
my @args = @$args_ref;
|
||||||
my @options = @$options_ref;
|
my @options = @$options_ref;
|
||||||
|
|
||||||
for my $arg (@args)
|
for my $arg (@args)
|
||||||
|
@ -46,57 +53,60 @@ sub HandleArgs
|
||||||
{
|
{
|
||||||
my @soptlist;
|
my @soptlist;
|
||||||
@soptlist = split //, $+{sopts} if defined $+{sopts};
|
@soptlist = split //, $+{sopts} if defined $+{sopts};
|
||||||
|
@soptlist = do { my %sn; grep { !$sn{$_}++ } @soptlist };
|
||||||
|
|
||||||
my $optarg;
|
my $optarg;
|
||||||
my $handled = 1;
|
|
||||||
|
|
||||||
DisplayUsage($options_ref)
|
DisplayUsage($options_ref)
|
||||||
if ((defined $+{sopt} and $+{sopt} eq "h") or
|
if ((defined $+{sopt} and $+{sopt} eq "h") or
|
||||||
(defined $+{lopt} and $+{lopt} eq "help"));
|
(defined $+{lopt} and $+{lopt} eq "help"));
|
||||||
|
|
||||||
for my $opt (@options)
|
for my $lsopt (@soptlist)
|
||||||
{
|
{
|
||||||
die "No option name to handle!"
|
if (my @i = grep { $options[$_]->{sopt} eq $lsopt } 0..$#options)
|
||||||
unless defined $opt->{lopt} or defined $opt->{sopt};
|
|
||||||
|
|
||||||
if (defined $opt->{sopt})
|
|
||||||
{
|
{
|
||||||
if (grep { $_ eq $opt->{sopt} // () } @soptlist)
|
die "Option -$lsopt cannot be in a list as it requires an argument!\n"
|
||||||
{
|
if $options[$i[0]]->{arg} eq "required";
|
||||||
die "Option -$opt->{sopt} cannot be in a list as it requires an argument!\n"
|
|
||||||
if $opt->{arg} eq "required";
|
|
||||||
|
|
||||||
$opt->{handler}();
|
$options[$i[0]]->{handler}();
|
||||||
}
|
|
||||||
# will make these look better later
|
|
||||||
elsif (defined $+{sopt} and defined $+{sopt}
|
|
||||||
and $+{sopt} eq $opt->{sopt})
|
|
||||||
{
|
|
||||||
$optarg = $+{optarg} // ();
|
|
||||||
$opt->{handler}($optarg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (defined $opt->{lopt} and defined $+{lopt}
|
|
||||||
and $+{lopt} eq $opt->{lopt})
|
|
||||||
{
|
|
||||||
$optarg = $+{optarg} // ();
|
|
||||||
$opt->{handler}($optarg);
|
|
||||||
}
|
}
|
||||||
|
else { warn "Unknown option -$lsopt!\n"; }
|
||||||
}
|
}
|
||||||
|
|
||||||
die "Unrecognized option: $arg\n"
|
for my $k (qw(sopt lopt))
|
||||||
unless $handled;
|
{
|
||||||
|
next if !defined $+{$k};
|
||||||
|
my $h = $k eq "lopt" ? '--':'-';
|
||||||
|
|
||||||
|
if (my @j = grep { $options[$_]->{$k} eq $+{$k} } 0..$#options)
|
||||||
|
{
|
||||||
|
$optarg = $+{optarg} // ();
|
||||||
|
|
||||||
|
die "Option $h$+{$k} requires an argument!\n"
|
||||||
|
if !defined $optarg and $options[$j[0]]->{arg} eq "required";
|
||||||
|
|
||||||
|
$options[$j[0]]->{handler}($optarg);
|
||||||
|
}
|
||||||
|
else { warn "Unknown option $h$+{$k}!\n"; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# TODO: Perhaps add precedence for different options.
|
||||||
|
# For example, if you pass -pb to the program, you probably want to
|
||||||
|
# build before publishing.
|
||||||
|
|
||||||
sub HandleConf
|
sub HandleConf
|
||||||
{
|
{
|
||||||
my $filename = shift;
|
my $filename = shift // "none";
|
||||||
print "Loading configuration from: $filename\n";
|
print "Loading configuration from: $filename\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#-----------------------------------------------------------------------
|
||||||
|
# Main routine
|
||||||
|
#-----------------------------------------------------------------------
|
||||||
|
|
||||||
my @optArray = (
|
my @optArray = (
|
||||||
{
|
{
|
||||||
lopt => "config",
|
lopt => "config",
|
||||||
|
@ -130,6 +140,7 @@ my @optArray = (
|
||||||
|
|
||||||
sub Main
|
sub Main
|
||||||
{
|
{
|
||||||
|
DisplayUsage(\@optArray) if (!@ARGV);
|
||||||
HandleArgs(\@ARGV, \@optArray);
|
HandleArgs(\@ARGV, \@optArray);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue