Solved

process options which require two accompanying arguments

Posted on 1998-08-18
1
189 Views
Last Modified: 2010-03-05
Hello,
I know Geopt::Std can process single-character option which require one accompanying argument. But my problem is i would  like to invok program with some options which need two accompanying arguments. Is there some way around this?

part of program:
# this program only works when invoke like
#              command [-o sth] [-f filename1 filename2]
# but not
#        command [-f filename1 filename2][-o sth]    
# because processing of @ARGV stops when an argument without # a leading - is encountered.

use Getopt::Std;
getopts("f:o:");

if(!defined($opt_o)){
   $opt_o="other";
}
$other=$opt_o;

if (!defined($opt_f){
   $file1="name1";
   $file2="name2";
}else{
   $file1=$opt_f;
  $file2=$ARGV[0];
}

.............
0
Comment
Question by:rongz
1 Comment
 
LVL 5

Accepted Solution

by:
b2pi earned 50 total points
Comment Utility
$errs = undef;
while (@ARGV) {
   getopts("f:o:");
   if (defined($opt_f) && !defined($opt_f2)) {
       $opt_f2 = shift(@ARGV);
   } else {
       $errs = shift(@ARGV);
       print "Bad option: $errs\n";
}
exit if defined($errs);
$other = defined($opt_o) ? $opt_o : 'other';
$file1 = defined($opt_f) ? $opt_f : 'name1';
$file2 = definend($opt_f2) ? $opt_f2 : 'name2';

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now