in reply to Getopt::Long Validation

Actually, I think I'd prefer not using Getopt::Long (or even Getopt::Std) at all: just expect one argument after the name of the script. If it turns out to be the name of an existing file, read that file; if it's not a file, but it looks like an IP range, treat it as such; otherwise die with a usage message:
use strict; my $Usage = "$0 lower.ip.addr-upper.ip.addr\n or\n$0 text.file\n"; die $Usage unless @ARGV == 1; my ($iplower, $ipupper); if ( -f $ARGV[0] ) { # treat the arg as a file } elsif ( $ARGV[0] =~ /^((?:\d{1,3}\.){3}\d{1,3})-(\d{1,3}(?:\.\d{1,3}){ +3})$/ ) { ($iplower,$ipupper) = ($1,$2); } else { die "Invalid command-line arg: $ARGV[0]\n\n$Usage"; } ...
(update: I do not mean to suggest that I'd never use of the Getopt modules -- I do use them often. It's just that for a simple case like this, option flags seem unnecessary: you have to provide one type of arg or the other, and you can't provide both -- these are not really "options", and you don't need a command-line flag to tell them apart.)

Replies are listed 'Best First'.
Re: Re: Getopt::Long Validation
by Ninthwave (Chaplain) on Oct 21, 2003 at 08:09 UTC
    Very true but I might add other options later so wanted the basic logic for doing this with Getopt::Long so I would already be using it if I needed to add oh say verbose which I am thinking of doing. But again good logic to take in. Thank you