Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

random: raise error for bad option #720

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 12 additions & 37 deletions bin/random
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,15 @@ License: perl
use strict;
use Getopt::Std;

my ($VERSION) = '1.2';
my ($VERSION) = '1.3';

my $warnings = 0;

END {
close STDOUT || die "$0: can't close stdout: $!\n";
$? = 1 if $? == 255; # from die
}

# Print a usuage message on a unknown option.
# Requires my patch to Getopt::Std of 25 Feb 1999.
$SIG {__WARN__} = sub {
if (substr ($_ [0], 0, 14) eq "Unknown option") {die "Usage"};
require File::Basename;
$0 = File::Basename::basename ($0);
$warnings = 1;
warn "$0: @_";
};

$SIG {__DIE__} = sub {
require File::Basename;
$0 = File::Basename::basename ($0);
if (substr ($_ [0], 0, 5) eq "Usage") {
die <<EOF;
$0 (Perl game utils) $VERSION
$0 [-er] [denominator]
EOF
}
die "$0: @_";
};

# Get the options.
getopts ('er', \my %options);

die "Usage" if @ARGV > 1;

my $denominator = @ARGV ? shift : 2;

die "Usage" if $denominator =~ /\D/ || $denominator == 0;
my %options;
getopts('er', \%options) or usage();
my $denominator = shift;
$denominator = 2 unless defined $denominator;
usage() if @ARGV;
usage() if $denominator =~ /\D/ || $denominator == 0;

exit int rand $denominator if exists $options {e};

Expand All @@ -65,6 +35,11 @@ while (<>) {print if $frac >= rand;}

exit $warnings;

sub usage {
warn "usage: $0 [-er] [denominator]\n";
exit 1;
}

__END__

=pod
Expand Down
Loading