-sub main () {
- if (@ARGV==4) {
- ($ship, $have{Swill}, $have{Grog}, $have{Fine}) = @ARGV;
- print_inputs();
- compute_restock_requirements();
- } elsif (@ARGV==3) {
- ($price{Swill}, $price{Grog}, $price{Fine}) = @ARGV;
- print_inputs();
- compute_cheapest_rum();
- } elsif (@ARGV==7) {
- ($price{Swill}, $price{Grog}, $price{Fine},
- $ship,
- $have{Swill}, $have{Grog}, $have{Fine}) = @ARGV;
- print_inputs();
- compute_cheapest_rum();
- compute_restock_requirements();
- compute_restock_cheapest_rum();
- } else {
- die $usage;
+our (@kinds) = qw(Swill Grog Fine Shot);
+our (@proofs) = qw(40 60 100);
+
+sub parse_info ($$$\@$) {
+ my ($omitswgok,$default,$multishot, $storeary, $what) = @_;
+ @ARGV or badusage("missing value for information argument \`$_'");
+ badusage("$what: specified more than once")
+ if defined $storeary->[2];
+ my (@v) = ();
+ while (@ARGV and $ARGV[0] =~ m/^\d|^x$/) {
+ $_ = shift @ARGV;
+ push @v, $_;
+ }
+ if (@v==1 or @v==2) {
+ badusage("$what: swill and grog amounts must be specified")
+ unless $omitswgok;
+ @v=($default,$default,@v);
+ }
+ if ($multishot and @v==4 and length $v[3]) {
+ $v[3] =~ m/^0*(\d+)\+0*(\d+)$/ or
+ badusage("$what: shot must be specified as <hold>+<loaded>");
+ $v[3] = $1 + $2;
+ }
+ if (@v==3) {
+ push @v, $default;
+ }
+ if (@v != 4) {
+ badusage("$what: invalid syntax (wrong number of parameters)");
+ }
+ my $i=0;
+ foreach $_ (@v) {
+ if ($default>0 and m/^x$/) {
+ $_ = $default;
+ } elsif (m/^0*(\d+)$/) {
+ $_= $1;
+ } else {
+ badusage("$what: $kinds[$i] \`$_': bad syntax");
+ }
+ $i++;
+ }
+ @$storeary = @v;
+}
+
+our (@have,@want,@price);
+
+sub parse_args () {
+ @ARGV or badusage("need some information to go on");
+ while (@ARGV) {
+ $_ = shift @ARGV;
+ if (m/^have$/) {
+ parse_info(1,0,1, @{ $have[@have] }, 'have');
+ } elsif (m/^want$/) {
+ parse_info(1,0,0, @want, 'want');
+ } elsif (m/^price$/) {
+ parse_info(0,1e7,0, @price, 'price');
+ } else {
+ badusage("unknown information argument \`$_'");
+ }