On Thu, 03 May 2001, Donald J Bindner wrote:

> I think it might have some interest because it also demo's how to
> use "getopt" to parse command-line options in a script.

<BEGIN CODE>
> eval set -- "$OPT"
> 
> while test "X$1" != "X--"; do
>   case "$1" in
>     -h)
>       echo "Usage: lp2f [infile ...] [-o outfile]"
>       exit
>       ;;
>     -o)
>       shift
>       OUTFILE=$1
>       ;;
>   esac
>   shift
> done
> shift
> 
> if [ "$*" == "" ]; then
>   set -- -
> fi
> 
<END CODE>

getopt is fine for shell scripts.  There's even some packages in perl
that do a lot of the same things, but I found a better way to do it
without any packages:


my @nextargv;
for (@ARGV) {
    /^--help$/ and do { &usage; next };
#    /^--tor/ and do {$type = "tornado"; next };
#    /^--ham/ and do {$type = "hamming"; next };
            /^-(.*)$/ and do {foreach (split //, $1) {
                /[\?h]/ and do { &usage; next };
                        /t/ and do {push @nextargv, \$TRIALS; next };
                        /l/ and do {push @nextargv, \$LEFT; next };
                        /r/ and do {push @nextargv, \$RIGHT; next };
                        /w/ and do {push @nextargv, \$WEIGHT; next };
                        /v/ and do {next };
                        die "$0: unknown option\"-$1\", check usage with --help\n"; }
                        next };
           @nextargv and do {my $r = shift @nextargv; $r and $$r = $_; next };
   }

This is a cute way to parse arguments that's very functional.
And the syntactical sugar makes things so nice.

Eric
-- 
E-mail: [EMAIL PROTECTED]        If you love something, set it free.
GPG 1536g/B9C5D1F7 fpr:075A A3F7 F70B 1397 345D  A67E 70AA 820B A806 F95D

-- Attached file included as plaintext by Listar --

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.5 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE68vBmIfueuDWa3/wRAnhmAJ43EjH6rpWjqzYB9Rx2UrbegTDh8QCfdL7k
jbgde+ECl3dZqLrI/6RSvyA=
=f4tS
-----END PGP SIGNATURE-----



Reply via email to