Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GetOptions and wildcard Argument

Status
Not open for further replies.

cptk

Technical User
Mar 18, 2003
305
0
0
US
Using Getopt:Long ...
Is there a way to correctly capture all the files return if the cmd line arg contains a wildcard?
e.g., -f *.dbg ?

Since the shell expands the wildcard prior to perl, double-quoting "*.dbg" will resolves this, but sometimes the users don't always use double-quotes.

I guess I could try looking at the leftover "not matching" values in the @ARGV array after the GetOptions cmd and append those matching files to my original -f array, but that would be an exercise in futility because I wouldn't know what the original user's input file (with wildcard) would of looked like to match on - any thoughts?

my @oCLA;
my @fCLA;
GetOptions("o=i"=>\@oCLA,
"f=s"=>\@fCLA);

print "o: $oCLA\n" if $oCLA[0];
print "o: $oCLA\n" if $oCLA[1];
print "f: $fCLA\n" if $fCLA[0];

foreach (@ARGV) {
print "NOT VALID: $_\n";
}


prompt> zgetargs.pl -o 4 -o 5 -f *.dbg
oCLA: 4
oCLA: 5
fCLA: 2009061119645N01.dbg
NOT VALID: 2009061119645N02.dbg
 
cptk said:
I guess I could try looking at the leftover "not matching" values in the @ARGV array after the GetOptions cmd and append those matching files to my original -f array, but that would be an exercise in futility because I wouldn't know what the original user's input file (with wildcard) would of looked like to match on - any thoughts?

That would be the "traditional" way to handle it, i.e. all options that are not preceded by switches are considered to be input files. I don't understand why you say that would be an exercise in futility; what do you need to match on the user's input when the shell has already done it for you?

Annihilannic.
 
... in case there are other "non-valid" non-file arguments; but if this is the "traditional" way and there's no other way, I guess I could test if each argument is a file ...
if ( -e $_)

It just seems (to me at least) this scenario should of been automatically built-in to the -f(ile) switch of the Getopts.
 
As you correctly stated, the shell has expanded those arguments prior to passing the argument list to perl, so unfortunately there is nothing Getopts can do about it.

Testing whether they are files is a good approach.

Annihilannic.
 
Thanks for your responses Annihilannic ... I will put this file test in a global utilities module.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top