chiark / gitweb /
sensible command-line parsing
authorian <ian>
Sun, 18 Apr 2004 22:42:38 +0000 (22:42 +0000)
committerian <ian>
Sun, 18 Apr 2004 22:42:38 +0000 (22:42 +0000)
farnell/farnell-find
farnell/farnell-resistor-convert.pl
pcb/Makefile
pic.make
spice/Makefile

index 22782bcdc2f375feb6f22a92ce2ae7fbf89baf87..1ab15dfb86f8107d58a81c11e3a9506a6dc3fee4 100755 (executable)
@@ -1,11 +1,15 @@
 #!/usr/bin/perl -w
 
 # Usages:
-#   ./farnell-find [<options>] NNN-NNNN
+#   ./farnell-find [<options>] [describe] NNN-NNNN ....
 #       Prints info about the specified relevant part.
-#   ./farnell-find [<options>] NNN-NNNN <qty> [...] <sid>|<url>
+#   ./farnell-find [<options>] order [<sid>|<url>] [NNN-NNNN <qty> ...]
 #       Add specified parts and quantities to the cart.
-#   ./farnell-find [<options>] <file> ...
+#       If no parts and quantities specified on command line,
+#        will read from stdin (format is <item> <qty>).
+#       If no <sid> or <url> specified (ie thing with jsessionid)
+#        then will prompt and read a line from tty.
+#   ./farnell-find [<options>] bom <file> ...
 #       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($_= <STDIN>) 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>; 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();
index f98c6199fb9f6e5c88664a9f863ed2715dbcfde5..8c95ed37014617295a54a163fc744130b34c6d24 100755 (executable)
@@ -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 <item> to
+#  2. Feed one or more outputs from farnell-find describe <item> to
 #     this script with args  <mfprefix> <valueprefix>...
 #     where <mfprefix> is the re for the first part of the mfr
 #     code and <valueprefix> is the words of the part name prefix.
index 310e138f04e824631137a236fd0f0139f3861f7c..29ed6fe04bbeeaed71d1d4162d552207cd9f1a45 100644 (file)
@@ -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
index 310e138f04e824631137a236fd0f0139f3861f7c..29ed6fe04bbeeaed71d1d4162d552207cd9f1a45 100644 (file)
--- 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
index 310e138f04e824631137a236fd0f0139f3861f7c..29ed6fe04bbeeaed71d1d4162d552207cd9f1a45 100644 (file)
@@ -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