[Templates] tpage extension

Slaven Rezic slaven@rezic.de
Tue, 21 Oct 2003 13:15:18 +0000


Here's an extension for tpage to accept a number of Template config
options. With this change one is able to say

    tpage --pre_chomp=1 --post_chomp==1 -I/another/include/dir --debug=all ...

Note that the command line arguments are lowercase and will be converted
to uppercase options in the script.

Regards,
    Slaven

--- /usr/local/dist/cpan/build/Template-Toolkit-2.10/bin/tpage	2003-07-24 18:02:05.000000000 +0200
+++ tpage	2003-10-21 15:11:47.000000000 +0200
@@ -25,31 +25,54 @@
 
 use strict;
 use Template;
+use Getopt::Long;
 
-# look for -h or --help option, print usage and exit
-if (grep /^--?h(elp)?/, @ARGV) {
-    print "usage: tpage file [ file [...] ]\n";
-    exit 0;
+my %opt = (absolute => 1,
+	   relative => 1,
+	  );
+if (!GetOptions(\%opt, "h|help", "d|define=s@",
+		'start_tag=s', 'end_tag=s', 'tag_style=s',
+		'pre_chomp=s', 'post_chomp=s', 'trim=i',
+		'interpolate=i', 'anycase=i',
+		'include_path=s@', 'I=s@',
+		'delimiter=s',
+		'absolute=s', 'relative=s',
+		'default=s', 'recursion=s',
+		'eval_perl=s', 'pre_process=s@', 'post_process=s@',
+		'process=s', 'error=s', 'output=s', 'output_path=s',
+		'debug=s', 'debug_format=s', 'cache_size=s',
+		'compile_ext=s', 'compile_dir=s', 'plugin_base=s',
+		'load_perl=s', 'v1dollar=s', 'tolerant=s',
+		# BLOCKS, AUTO_RESET, VARIABLES, PRE_DEFINE, CONSTANTS,
+		# CONSTANT_NAMESPACE, NAMESPACE, ERRORS, PLUGINS,
+		# FILTERS, LOAD_TEMPLATES, LOAD_PLUGINS, LOAD_FILTERS,
+		# SERVICE, CONTEXT, STASH, PARSER, GRAMMAR missing
+	       )) {
+    usage();
 }
-my $vars = { };
-my ($var, $val);
+usage() if $opt{h};
 
-while ($ARGV[0] && $ARGV[0] =~ /^--?d(efine)?/) {
-    shift(@ARGV);
-    die "--define expect a 'variable=value' argument\n" 
-	unless defined ($var = shift(@ARGV));
-    ($var, $val) = split(/\s*=\s*/, $var, 2);
+my $vars = { };
+for (@{ $opt{d} }) {
+    my($var, $val) = split(/\s*=\s*/, $_, 2);
     $vars->{ $var } = $val;
 }
+delete $opt{d};
+
+# -I is an alias for -include_path
+push @{$opt{include_path}}, @{$opt{I}} if $opt{I} && @{$opt{I}};
+delete $opt{I};
+
+my %tmpl_config;
+while(my($k,$v) = each %opt) {
+    $tmpl_config{uc($k)} = $v;
+}
 
 # read from STDIN if no files specified
 push(@ARGV, '-') unless @ARGV;
 
-# create a template processor 
-my $template = Template->new({
-    ABSOLUTE => 1,
-    RELATIVE => 1,
-});
+# create a template processor
+my $template = Template->new(\%tmpl_config);
 
 # process each input file 
 foreach my $file (@ARGV) {
@@ -58,6 +81,11 @@ foreach my $file (@ARGV) {
 	|| die $template->error();
 }
 
+sub usage {
+    print "usage: tpage [--define var=val ...] [Template options] file [ file [...] ]\n";
+    exit 0;
+}
+
 __END__
 
 
@@ -125,4 +153,4 @@ modify it under the same terms as Perl i
 
 =head1 SEE ALSO
 
-L<ttree|Template::Tools::ttree>
\ No newline at end of file
+L<ttree|Template::Tools::ttree>


-- 
Slaven Rezic - slaven <at> rezic <dot> de