sg/staticgen.pl

138 lines
3.6 KiB
Perl
Raw Normal View History

2025-01-19 12:16:01 -05:00
#!/usr/bin/perl
2024-11-29 03:47:11 +00:00
use strict;
use warnings;
2025-01-19 12:16:01 -05:00
use Carp;
2024-11-29 03:47:11 +00:00
#use HTML::Template;
$SIG{__WARN__} = sub { die @_; };
2025-01-19 12:16:01 -05:00
sub DisplayUsage
{
my $options_ref = shift;
my @options = @$options_ref;
2024-11-29 03:47:11 +00:00
2025-01-19 12:16:01 -05:00
print "Usage:\n";
for my $opt (@options)
{
my $opt_str = " " . (defined $opt->{sopt} ? "-$opt->{sopt}, " : " ");
$opt_str .= (defined $opt->{lopt} ? "--$opt->{lopt}" : "");
2024-11-29 03:47:11 +00:00
2025-01-19 12:16:01 -05:00
if (lc $opt->{arg} eq "required")
{
$opt_str .= "=<value>";
}
elsif (lc $opt->{arg} eq "optional")
{
$opt_str .= "[=<value>]";
}
2024-11-29 03:47:11 +00:00
2025-01-19 12:16:01 -05:00
print $opt_str . "\n " . ($opt->{desc} // "") . "\n\n";
2024-11-29 03:47:11 +00:00
}
2025-01-19 12:16:01 -05:00
exit 0;
2024-11-29 03:47:11 +00:00
}
2025-01-19 12:16:01 -05:00
sub HandleArgs
2024-11-29 03:47:11 +00:00
{
2025-01-19 12:16:01 -05:00
my ($args_ref, $options_ref) = @_;
my @args = @$args_ref;
my @options = @$options_ref;
for my $arg (@args)
2024-11-29 03:47:11 +00:00
{
2025-01-19 12:16:01 -05:00
if ($arg =~ m@^(-(?<sopts>\w{2,})|-(?<sopt>\w)|-+(?<lopt>[\w-]+))(=(?<optarg>.+))?\z@i)
2024-11-29 03:47:11 +00:00
{
2025-01-19 12:16:01 -05:00
my @soptlist;
@soptlist = split //, $+{sopts} if defined $+{sopts};
my $optarg;
my $handled = 1;
DisplayUsage($options_ref)
if ((defined $+{sopt} and $+{sopt} eq "h") or
(defined $+{lopt} and $+{lopt} eq "help"));
for my $opt (@options)
2024-11-29 03:47:11 +00:00
{
2025-01-19 12:16:01 -05:00
die "No option name to handle!"
unless defined $opt->{lopt} or defined $opt->{sopt};
if (defined $opt->{sopt})
{
2025-01-19 12:35:41 -05:00
if (grep { $_ eq $opt->{sopt} // () } @soptlist)
2025-01-19 12:16:01 -05:00
{
die "Option -$opt->{sopt} cannot be in a list as it requires an argument!\n"
if $opt->{arg} eq "required";
$opt->{handler}();
}
2025-01-19 12:35:41 -05:00
# will make these look better later
elsif (defined $+{sopt} and defined $+{sopt}
and $+{sopt} eq $opt->{sopt})
2025-01-19 12:16:01 -05:00
{
$optarg = $+{optarg} // ();
$opt->{handler}($optarg);
}
}
2025-01-19 12:35:41 -05:00
if (defined $opt->{lopt} and defined $+{lopt}
and $+{lopt} eq $opt->{lopt})
2025-01-19 12:16:01 -05:00
{
$optarg = $+{optarg} // ();
$opt->{handler}($optarg);
}
2024-11-29 03:47:11 +00:00
}
2025-01-19 12:16:01 -05:00
die "Unrecognized option: $arg\n"
unless $handled;
2024-11-29 03:47:11 +00:00
}
}
}
2025-01-19 12:16:01 -05:00
sub HandleConf
{
my $filename = shift;
print "Loading configuration from: $filename\n";
}
my @optArray = (
{
lopt => "config",
sopt => "c",
desc => "Loads a custom config file at a specified path.",
arg => "required",
handler => \&HandleConf
},
{
lopt => "new-post",
sopt => "n",
desc => "Create a new post.",
arg => "none",
handler => sub { print "do something\n"; }
},
{
lopt => "build",
sopt => "b",
desc => "Build all posts and templates into HTML.",
arg => "optional",
handler => sub { print "do something\n"; }
},
{
lopt => "publish",
sopt => "p",
desc => "Publish built files.",
arg => "none",
handler => sub { print "do something\n"; }
},
);
sub Main
{
HandleArgs(\@ARGV, \@optArray);
}
2024-11-29 03:47:11 +00:00
&Main();
2025-01-19 12:16:01 -05:00
exit 0;