From 843ddd83192bdefa944665a19013bbd710e8f558 Mon Sep 17 00:00:00 2001 From: Michael Mikonos <127171689+mknos@users.noreply.github.com> Date: Tue, 10 Oct 2023 09:58:47 +0800 Subject: [PATCH] banner: switch to getopt * banner has only one option: -w NUMBER * Numbers above zero are accepted * Slightly less code to maintain if this uses common Getopt library --- bin/banner | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/bin/banner b/bin/banner index d0b8a79b..9d99eba3 100755 --- a/bin/banner +++ b/bin/banner @@ -15,6 +15,7 @@ use strict; require 5.004; use File::Basename qw(basename); +use Getopt::Std qw(getopts); use constant DWIDTH => 132; use constant EX_SUCCESS => 0; @@ -33,23 +34,15 @@ my @data_table; # Pointers into @data_table for each ASCII char my %ascii_to_table; -# options my $width = DWIDTH; -while (@ARGV && $ARGV[0] =~ s/^-//) { - local $_ = shift; - if (s/^w//) { - if (length) { $width = $_ } - elsif (@ARGV) { $width = shift } - else { warn "$Program: illegal argument for -w option\n"; - exit EX_FAILURE; } - if ($width =~ /[^0-9]/ || $width == 0) { - warn "$Program: illegal argument for -w option\n"; - exit EX_FAILURE; - } - next; +my %opt; +getopts('w:', \%opt) or usage(); +if (defined $opt{'w'}) { + if ($opt{'w'} =~ m/[^0-9]/ || $opt{'w'} == 0) { + warn "$Program: illegal argument for -w option\n"; + exit EX_FAILURE; } - warn "$Program: illegal option -- $_\n"; - usage(); + $width = $opt{'w'}; } # scale characters to width