diff --git a/staticgen.pl b/staticgen.pl index da1f71a..c0b6ebd 100755 --- a/staticgen.pl +++ b/staticgen.pl @@ -1,137 +1,148 @@ -#!/usr/bin/perl - -use strict; -use warnings; -use Carp; -#use HTML::Template; - -$SIG{__WARN__} = sub { die @_; }; - -sub DisplayUsage -{ - my $options_ref = shift; - my @options = @$options_ref; - - print "Usage:\n"; - for my $opt (@options) - { - my $opt_str = " " . (defined $opt->{sopt} ? "-$opt->{sopt}, " : " "); - $opt_str .= (defined $opt->{lopt} ? "--$opt->{lopt}" : ""); - - if (lc $opt->{arg} eq "required") - { - $opt_str .= "="; - } - elsif (lc $opt->{arg} eq "optional") - { - $opt_str .= "[=]"; - } - - print $opt_str . "\n " . ($opt->{desc} // "") . "\n\n"; - } - - exit 0; -} - -sub HandleArgs -{ - my ($args_ref, $options_ref) = @_; - - my @args = @$args_ref; - my @options = @$options_ref; - - for my $arg (@args) - { - if ($arg =~ m@^(-(?\w{2,})|-(?\w)|-+(?[\w-]+))(=(?.+))?\z@i) - { - 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) - { - die "No option name to handle!" - unless defined $opt->{lopt} or defined $opt->{sopt}; - - if (defined $opt->{sopt}) - { - if (grep { $_ eq $opt->{sopt} // () } @soptlist) - { - die "Option -$opt->{sopt} cannot be in a list as it requires an argument!\n" - if $opt->{arg} eq "required"; - - $opt->{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); - } - } - - die "Unrecognized option: $arg\n" - unless $handled; - } - } -} - -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); -} - -&Main(); -exit 0; +#!/usr/bin/perl + +use strict; +use warnings; +use Carp; +#use HTML::Template; + +$SIG{__WARN__} = sub { die @_; }; + +my $PRG = $0; +$PRG =~ s!.*/!!; + +#----------------------------------------------------------------------- +# Command line argument processing routines +#----------------------------------------------------------------------- + +sub DisplayUsage +{ + my $options_ref = shift; + my @options = @$options_ref; + + print "Usage: $PRG [OPTIONS]\n"; + for my $opt (@options) + { + my $opt_str = " " . (defined $opt->{sopt} ? "-$opt->{sopt}, " : " "); + $opt_str .= (defined $opt->{lopt} ? "--$opt->{lopt}" : ""); + + if (lc $opt->{arg} eq "required") + { + $opt_str .= "="; + } + elsif (lc $opt->{arg} eq "optional") + { + $opt_str .= "[=]"; + } + + print $opt_str . "\n " . ($opt->{desc} // "") . "\n\n"; + } + + exit 0; +} + +sub HandleArgs +{ + my ($args_ref, $options_ref) = @_; + + my @args = @$args_ref; + my @options = @$options_ref; + + for my $arg (@args) + { + if ($arg =~ m@^(-(?\w{2,})|-(?\w)|-+(?[\w-]+))(=(?.+))?\z@i) + { + my @soptlist; + @soptlist = split //, $+{sopts} if defined $+{sopts}; + @soptlist = do { my %sn; grep { !$sn{$_}++ } @soptlist }; + + my $optarg; + + DisplayUsage($options_ref) + if ((defined $+{sopt} and $+{sopt} eq "h") or + (defined $+{lopt} and $+{lopt} eq "help")); + + for my $lsopt (@soptlist) + { + if (my @i = grep { $options[$_]->{sopt} eq $lsopt } 0..$#options) + { + die "Option -$lsopt cannot be in a list as it requires an argument!\n" + if $options[$i[0]]->{arg} eq "required"; + + $options[$i[0]]->{handler}(); + } + else { warn "Unknown option -$lsopt!\n"; } + } + + for my $k (qw(sopt lopt)) + { + 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 +{ + my $filename = shift // "none"; + print "Loading configuration from: $filename\n"; +} + +#----------------------------------------------------------------------- +# Main routine +#----------------------------------------------------------------------- + +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 +{ + DisplayUsage(\@optArray) if (!@ARGV); + HandleArgs(\@ARGV, \@optArray); +} + +&Main(); +exit 0;