Skip to content

Commit

Permalink
moo: usage() > warn("Usage") (#569)
Browse files Browse the repository at this point in the history
* moo: usage() > warn("Usage")

* Introduce a regular usage() function
* Size of 0 is still not allowed (the puzzle must have 1 or more numbers)

* tweak description text
  • Loading branch information
mknos committed Apr 19, 2024
1 parent c7aa8b1 commit c63c9d0
Showing 1 changed file with 8 additions and 31 deletions.
39 changes: 8 additions & 31 deletions bin/moo
Expand Up @@ -16,38 +16,15 @@ use strict;

my ($VERSION) = '1.2';

# Print a usuage message on a unknown option.
$SIG {__WARN__} = sub {
require File::Basename;
$0 = File::Basename::basename ($0);
if (substr ($_ [0], 0, 14) eq "Unknown option" ||
substr ($_ [0], 0, 5) eq "Usage") {
warn <<EOF;
$0 (Perl bin utils) $VERSION
$0 [size]
EOF
exit 1;
}
else {
warn "$0: @_";
}
};

$SIG {__DIE__} = sub {
require File::Basename;
$0 = File::Basename::basename ($0);
die "$0: @_";
};

my $size = 4;

warn "Usage" if @ARGV > 1; # Will exit.

if (@ARGV) {
$size = shift;
warn "Usage" if !$size || $size =~ /\D/; # Will exit;
sub usage {
die "usage: moo [size]\n";
}

my $size = shift;
$size = 4 unless defined $size;
usage() if $size !~ m/\A[0-9]+\Z/ or !$size;
usage() if @ARGV;

print "MOO\n";
{
my @secret_by_value = (0) x 10;
Expand Down Expand Up @@ -122,7 +99,7 @@ moo [size]
=head1 DESCRIPTION
I<moo> is a game where the user has to guess a number choosen by
I<moo> is a game where the user guesses a random number chosen by
the computer. By default, the computer takes a number of four digits
(including 0's), but that can be changed by giving I<moo> the number of
digits to take. After each guess, the number of B<bull>s and B<cow>s
Expand Down

0 comments on commit c63c9d0

Please sign in to comment.