Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Improve command line parsing.

  • Loading branch information...
commit a064c7f86b779f20aadbef06dbcc0476c6b0cd8b 1 parent a06db7d
@davorg authored
Showing with 18 additions and 12 deletions.
  1. +18 −12 lotto
View
30 lotto
@@ -5,21 +5,27 @@ use warnings;
use 5.010;
my $config = parse_config();
-my $type = shift // 'lotto';
-unless (exists $config->{$type}) {
- unshift @ARGV, $type;
- $type = 'lotto';
+
+my ($type, $count) = qw[lotto 1];
+
+my @errs;
+if (@ARGV == 2) {
+ $type = shift;
+ if (! exists $config->{$type}) {
+ push @errs, qq["$type" is not a recognised type of lottery];
+ }
}
-my $count;
-if (@ARGV) {
- if ($ARGV[0] =~ /^\d+$/) {
- $count = $ARGV[0];
- } else {
- die "$ARGV[0] doesn't look like a positive integer\n";
+if (@ARGV == 1) {
+ $count = shift;
+ if ($count !~ /^\d+$/) {
+ push @errs, qq["$count" doesn't look like a positive integer];
}
-} else {
- $count = 1;
+}
+
+if (@ARGV || @errs) {
+ push @errs, 'Usage: lotto [' . join('|', keys %$config) . "] [count]\n";
+ die join "\n", @errs;
}
for (1 .. $count) {
Please sign in to comment.
Something went wrong with that request. Please try again.