#!/usr/local/apps/perl/bin/perl ############################################################################# # # # WHO: John L. Moreland # # # # WHAT: cmdline # # # # WHY: Demonstrates one command line parsing technique. # # # # WHERE: Opus Software # # # # WHEN: Tue Sep 29 21:21:13 PDT 1998 # # # # HOW: PERL # # # ############################################################################# while ( @ARGV ) { $arg = shift( @ARGV ); if ( $arg =~ /^-[acx]$/ ) { # Handle single-character options $opt{$arg} = 1; # Could do compound option handling here... } elsif ( $arg eq "-help" ) { # Handle multi-character options $opt{$arg} = 1; } elsif ( $arg eq "-f" ) { # Handle options with arguments $opt{$arg} = shift( @ARGV ); if ( ($opt{$arg} eq "") || ($opt{$arg} =~ /^-/) ) { print STDERR "The $arg option requires an argument\n"; exit; } } elsif ( -f $arg ) { # Handle input or output file arguments @files = ( @files, $arg ); } else { # Handle unknown arguments print "Invalid argument: $arg\n"; print "Usage: $0 [-a] [-c] [-x] [-help] [-f optarg] [files]\n"; exit; } } print "Options:\n" if ( %opt ); foreach $key ( keys %opt ) { print " $key = $opt{$key}\n"; } print "Files:\n" if ( @files ); foreach $file ( @files ) { print " $file\n"; }