From effab7820acc8ce86c99cab89a516dbaabc1b86c Mon Sep 17 00:00:00 2001 From: Mischa Poslawsky Date: Mon, 26 Oct 2020 21:08:57 +0100 Subject: [PATCH] parse-wormedit: replace raw/render options by --format Clean syntax identifier instead of adding new options for every (sub)format. Retains support for previous options. --- parse-wormedit | 52 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/parse-wormedit b/parse-wormedit index ffd68ef..99daaa1 100755 --- a/parse-wormedit +++ b/parse-wormedit @@ -1,7 +1,8 @@ #!/usr/bin/env perl +use 5.010; use strict; use warnings; -use 5.010; +use experimental 'switch'; use lib 'lib'; # make runnable for simple cases use Data::Dumper; @@ -9,14 +10,17 @@ use Getopt::Long 2.33 qw(HelpMessage :config bundling); use Games::Wormy::TICalcLevels; use Games::Wormy::WormEdit; -our $VERSION = '1.06'; +our $VERSION = '1.07'; GetOptions(\my %opt, - 'raw|r', # full output + 'format|f=s', # output type + 'raw|r!', # compatibility for yaml format 'version=i', # force version - 'render:i', # image of level(s) + 'levels|render:i', # image of level(s) 'output|o=s', # output file ) or HelpMessage(-exitval => 2); +$opt{format} //= 'yaml' if $opt{raw}; +$opt{format} //= 'pnm' if defined $opt{levels}; my @OBJTYPE = ('none', 'line', 'fat line', 'bar', 'circle'); my @ENDTYPE = ('none', 'message', 'small message'); @@ -51,10 +55,10 @@ else { if ($opt{output}) {{ # derive format from file extension if ($opt{output} =~ /\.yaml$/) { - $opt{raw} = 1; + $opt{format} //= 'yaml'; } elsif ($opt{output} !~ /\.txt$/) { - $opt{render} ||= 0; + $opt{format} //= 'pnm'; last; # images are written directly to file } @@ -65,13 +69,14 @@ if ($opt{output}) {{ }} # output with user-preferred formatting -if (defined $opt{render}) { +given ($opt{format}) { +when ('pnm') { require Games::Wormy::Render; my @request; - if ($opt{render}) { + if ($opt{levels}) { # find all numeric values in argument - @request = $opt{render} =~ /(\d+)/g; + @request = $opt{levels} =~ /(\d+)/g; } else { # default to all singleplayer levels @@ -86,7 +91,7 @@ if (defined $opt{render}) { $opt{output} ? (file => $opt{output}) : (fh => \*STDOUT, type => 'pnm') ) or die $img->errstr; } -elsif ($opt{raw}) { +when ('yaml') { # full data in yaml (human-readable) formatting require YAML; local $YAML::CompressSeries; @@ -108,7 +113,7 @@ elsif ($opt{raw}) { print $yml; } -else { +default { print $data->{name}; print " ($data->{description})" if defined $data->{description}; print "\n"; @@ -161,6 +166,7 @@ else { } print "\n"; } +} __END__ @@ -170,14 +176,32 @@ parse-wormedit - Wormy level data parser =head1 SYNOPSIS - parse-wormedit [--raw|--render] [--output ] + parse-wormedit [--format=] [--levels=] [--output=] =head1 DESCRIPTION Reads Wormy levels (either original WormEdit source or compiled TI-86 string) -from STDIN or given file, and prints summarised contents to STDOUT. +from STDIN or given file, and prints parsed data to STDOUT. + +If an I file name is given, its extension determines the format, +otherwise explicitly given by the I option: + +=over 6 + +=item txt + +Plain text summary of levelpack contents. + +=item yaml + +All parsed data in YAML syntax. + +=item pnm, png, bmp, ... + +Image drawing of rendered levels. +Unrecognised values are interpreted as file type and converted by Imager. -If an I file name is given, its extension determines the format. +=back =head1 AUTHOR -- 2.30.0