parse-wormedit: format option overrides image file
authorMischa Poslawsky <wormy@shiar.org>
Mon, 26 Oct 2020 21:14:12 +0000 (22:14 +0100)
committerMischa Poslawsky <wormy@shiar.org>
Mon, 26 Oct 2020 22:35:49 +0000 (23:35 +0100)
Save specific extension allowing stdout syntax distinct from pnm.

parse-wormedit

index 99daaa15e8579dfa8d8a889a593ba19294122c2d..721cec841e15a519862aae501dd8d7484550ec7c 100755 (executable)
@@ -54,12 +54,12 @@ else {
 
 if ($opt{output}) {{
        # derive format from file extension
-       if ($opt{output} =~ /\.yaml$/) {
-               $opt{format} //= 'yaml';
+       if ($opt{output} =~ /\.(yaml|txt)$/) {
+               $opt{format} //= $1
        }
-       elsif ($opt{output} !~ /\.txt$/) {
-               $opt{format} //= 'pnm';
-               last;  # images are written directly to file
+       else {
+               # images are written directly to file
+               last;
        }
 
        # redirect standard output to given file
@@ -70,27 +70,6 @@ if ($opt{output}) {{
 
 # output with user-preferred formatting
 given ($opt{format}) {
-when ('pnm') {
-       require Games::Wormy::Render;
-
-       my @request;
-       if ($opt{levels}) {
-               # find all numeric values in argument
-               @request = $opt{levels} =~ /(\d+)/g;
-       }
-       else {
-               # default to all singleplayer levels
-               @request = 0 .. $data->{levelcount}->{single} - 1;
-       }
-       @request or die "no levels found or specified\n";
-
-       my $img = Games::Wormy::Render->composite(
-               map { $data->{levels}->[$_] } @request
-       ) or die "empty result for levels\n";
-       $img->write(
-               $opt{output} ? (file => $opt{output}) : (fh => \*STDOUT, type => 'pnm')
-       ) or die $img->errstr;
-}
 when ('yaml') {
        # full data in yaml (human-readable) formatting
        require YAML;
@@ -113,7 +92,7 @@ when ('yaml') {
 
        print $yml;
 }
-default {
+when ('txt') {
        print $data->{name};
        print " ($data->{description})" if defined $data->{description};
        print "\n";
@@ -166,6 +145,28 @@ default {
        }
        print "\n";
 }
+default {
+       require Games::Wormy::Render;
+
+       my @request;
+       if ($opt{levels}) {
+               # find all numeric values in argument
+               @request = $opt{levels} =~ /(\d+)/g;
+       }
+       else {
+               # default to all singleplayer levels
+               @request = 0 .. $data->{levelcount}->{single} - 1;
+       }
+       @request or die "no levels found or specified\n";
+
+       my $img = Games::Wormy::Render->composite(
+               map { $data->{levels}->[$_] } @request
+       ) or die "empty result for levels\n";
+       $img->write(
+               $opt{output} ? (file => $opt{output}) : (fh => \*STDOUT),
+               type => $opt{format} // 'pnm',
+       ) or die $img->errstr;
+}
 }
 
 __END__