parse-wormedit: silence switch feature warnings
[wormy.git] / parse-wormedit
index 4c4358f875a7a37bcf3e678badac5973582a06a3..0c94994a5ae79a122d603d924af658354a898a56 100755 (executable)
@@ -9,11 +9,13 @@ use Getopt::Long 2.33 qw(HelpMessage :config bundling);
 use Games::Wormy::TICalcLevels;
 use Games::Wormy::WormEdit;
 
-our $VERSION = '1.05';
+our $VERSION = '1.06';
 
 GetOptions(\my %opt,
        'raw|r',  # full output
        'version=i',  # force version
+       'render:i',  # image of level(s)
+       'output|o=s',  # output file
 ) or HelpMessage(-exitval => 2);
 
 my @OBJTYPE = ('none', 'line', 'fat line', 'bar', 'circle');
@@ -46,8 +48,44 @@ else {
        die "Unrecognised file type\n";
 }
 
+if ($opt{output}) {{
+       # derive format from file extension
+       if ($opt{output} =~ /\.yaml$/) {
+               $opt{raw} = 1;
+       }
+       elsif ($opt{output} !~ /\.txt$/) {
+               $opt{render} ||= 0;
+               last;  # images are written directly to file
+       }
+
+       # redirect standard output to given file
+       open my $output, '>', $opt{output}
+               or die "Cannot output to '$opt{output}': $!";
+       select $output;
+}}
+
 # output with user-preferred formatting
-if ($opt{raw}) {
+if (defined $opt{render}) {
+       require Games::Wormy::Render;
+
+       my @request;
+       if ($opt{render}) {
+               # find all numeric values in argument
+               @request = $opt{render} =~ /(\d+)/g;
+       }
+       else {
+               # default to all singleplayer levels
+               @request = 0 .. $data->{levelcount}->{single} - 1;
+       }
+
+       my $img = Games::Wormy::Render->composite(
+               map { $data->{levels}->[$_] } @request
+       );
+       $img->write(
+               $opt{output} ? (file => $opt{output}) : (fh => \*STDOUT, type => 'pnm')
+       ) or die $img->errstr;
+}
+elsif ($opt{raw}) {
        # full data in yaml (human-readable) formatting
        require YAML;
        local $YAML::CompressSeries;
@@ -131,12 +169,14 @@ parse-wormedit - Wormy level data parser
 
 =head1 SYNOPSIS
 
- parse-wormedit [--raw] <input.lvl>
+ parse-wormedit [--raw|--render] [--output <file.ext>] <input.lvl>
 
 =head1 DESCRIPTION
 
 Reads Wormy levels (either original WormEdit source or compiled TI-86 string)
-from STDIN or given file, and outputs contents, summarised or in full.
+from STDIN or given file, and prints summarised contents to STDOUT.
+
+If an I<output> file name is given, its extension determines the format.
 
 =head1 AUTHOR