Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

process options which require two accompanying arguments

Posted on 1998-08-18
1
Medium Priority
?
217 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 200 total points
ID: 1204379
$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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

886 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