From 7a72ba29faa5f4209ce6f519395b466a0341830d Mon Sep 17 00:00:00 2001 From: ian Date: Sun, 18 Apr 2004 22:42:38 +0000 Subject: [PATCH] sensible command-line parsing --- farnell/farnell-find | 104 +++++++++++++++++++--------- farnell/farnell-resistor-convert.pl | 2 +- pcb/Makefile | 6 +- pic.make | 6 +- spice/Makefile | 6 +- 5 files changed, 86 insertions(+), 38 deletions(-) diff --git a/farnell/farnell-find b/farnell/farnell-find index 22782bc..1ab15df 100755 --- a/farnell/farnell-find +++ b/farnell/farnell-find @@ -1,11 +1,15 @@ #!/usr/bin/perl -w # Usages: -# ./farnell-find [] NNN-NNNN +# ./farnell-find [] [describe] NNN-NNNN .... # Prints info about the specified relevant part. -# ./farnell-find [] NNN-NNNN [...] | +# ./farnell-find [] order [|] [NNN-NNNN ...] # Add specified parts and quantities to the cart. -# ./farnell-find [] ... +# If no parts and quantities specified on command line, +# will read from stdin (format is ). +# If no or specified (ie thing with jsessionid) +# then will prompt and read a line from tty. +# ./farnell-find [] bom ... # Processes input files and produces BOM parts list. # # Options: @@ -219,6 +223,7 @@ sub cart_add (@) { my (@orderlist) = @_; my ($i, $url, $item, $qty, @submit, $request, $response); my ($page_output); + local ($_); $url= "$urlbase/jsp/op/shoppingbasket.jsp;jsessionid=$jsessionid"; get_useragent(); $response= $useragent->get($url); @@ -618,40 +623,77 @@ while (@ARGV && $ARGV[0] =~ m/^\-/) { } } -if (@ARGV && $ARGV[0] =~ m/^\d\d\d\-/) { - if (@ARGV==1) { +sub is_jsessionid ($) { + my ($a) = @_; + return undef unless + $a =~ m/\;jsessionid=([0-9A-Z]+)\b/ || + $a =~ m/^([0-9A-Z]+)$/; + return $1; +} + +sub main () { + my ($op); + my ($item, $a); + + die unless @ARGV; + $op= $ARGV[0] =~ m/^\d/ ? 'describe' : shift @ARGV; + + if ($op eq 'describe') { my ($chr, $k); - $chr= by_item($ARGV[0]); - dump_warnerrs(); - foreach $k (sort keys %$chr) { - printf "%-20s %s\n", $k, $chr->{$k} or die $!; - } - } elsif (@ARGV > 1 && (@ARGV % 2)) { - my (@orderlist,$item,$qty); - $jsessionid= pop @ARGV; - if ($jsessionid =~ m/\;jsessionid=([0-9A-Z]+)\b/) { - $jsessionid= $1; - } elsif ($jsessionid =~ m/^([0-9A-Z]+)$/) { - } else { - die "bad jsessionid\n"; + foreach $item (@ARGV) { + $chr= by_item($item); + dump_warnerrs(); + foreach $k (sort keys %$chr) { + printf "%-20s %s\n", $k, $chr->{$k} or die $!; + } } + } elsif ($op eq 'order') { + my ($qty, @orderlist, $newjsid); while (@ARGV) { - ($item,$qty,@ARGV) = @ARGV; - $item =~ m/^$item_re$/ or die "$item ?"; - $qty =~ m/^[1-9]\d{0,5}$/ or die "$qty ?"; - push @orderlist, $item, $qty; + $a= shift @ARGV; + if (defined($newjsid= is_jsessionid($a))) { + die if defined $jsessionid; + $jsessionid= $newjsid; + } elsif ($a =~ m/^$item_re$/) { + die unless @ARGV; + $qty= shift @ARGV; + push @orderlist, $a, $qty; + } else { + die "$a ?"; + } + } + if (!@orderlist) { + for (;;) { + defined($_= ) or last; + chomp; + s/^\s+//; s/\s+$//; + next if m/^\#/; + m/^($item_re)\s+(\d+)$/ or die "$_ ?"; + push @orderlist, $1, $2; + } + die $! if STDIN->error; + } + if (!defined $jsessionid) { + open TTY, "+< /dev/tty" or die $!; + select((select(TTY), $|=1)[0]); + print TTY "URL or jsessionid: " or die $!; + $_= ; TTY->error and die $!; + length or die; + $newjsid= is_jsessionid($_); + defined $newjsid or die; + $jsessionid= $newjsid; } cart_add(@orderlist); + } elsif ($op eq 'bom') { + my ($filename); + foreach $filename (@ARGV) { + read_spec($filename); + } + analyse_spec(); + exit !!@fault; } else { die; } -} elsif (@ARGV) { - my ($filename); - foreach $filename (@ARGV) { - read_spec($filename); - } - analyse_spec(); - exit !!@fault; -} else { - die; } + +main(); diff --git a/farnell/farnell-resistor-convert.pl b/farnell/farnell-resistor-convert.pl index f98c619..8c95ed3 100755 --- a/farnell/farnell-resistor-convert.pl +++ b/farnell/farnell-resistor-convert.pl @@ -2,7 +2,7 @@ # ways to invoke: # 1. Get a search list from farnell and feed HTML source to # this script with no args. Will produce part number list. -# 2. Feed one or more outputs from farnell-find to +# 2. Feed one or more outputs from farnell-find describe to # this script with args ... # where is the re for the first part of the mfr # code and is the words of the part name prefix. diff --git a/pcb/Makefile b/pcb/Makefile index 310e138..29ed6fe 100644 --- a/pcb/Makefile +++ b/pcb/Makefile @@ -17,8 +17,10 @@ all: default layers circuit.bom o=>$@.new && mv -f $@.new $@ -circuit.bom: ./farnell-find control.partlist circuit.partlist - ./$^ $o +CIRCUITPARTS= control.partlist circuit.partlist + +circuit.bom: farnell-find $(CIRCUITPARTS) + ./farnell-find bom $(CIRCUITPARTS) %.d4: %.m4 $(M4INCS) Makefile m4 -s <$< $o diff --git a/pic.make b/pic.make index 310e138..29ed6fe 100644 --- a/pic.make +++ b/pic.make @@ -17,8 +17,10 @@ all: default layers circuit.bom o=>$@.new && mv -f $@.new $@ -circuit.bom: ./farnell-find control.partlist circuit.partlist - ./$^ $o +CIRCUITPARTS= control.partlist circuit.partlist + +circuit.bom: farnell-find $(CIRCUITPARTS) + ./farnell-find bom $(CIRCUITPARTS) %.d4: %.m4 $(M4INCS) Makefile m4 -s <$< $o diff --git a/spice/Makefile b/spice/Makefile index 310e138..29ed6fe 100644 --- a/spice/Makefile +++ b/spice/Makefile @@ -17,8 +17,10 @@ all: default layers circuit.bom o=>$@.new && mv -f $@.new $@ -circuit.bom: ./farnell-find control.partlist circuit.partlist - ./$^ $o +CIRCUITPARTS= control.partlist circuit.partlist + +circuit.bom: farnell-find $(CIRCUITPARTS) + ./farnell-find bom $(CIRCUITPARTS) %.d4: %.m4 $(M4INCS) Makefile m4 -s <$< $o -- 2.30.2