LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-23-2009, 09:46 AM   #1
kdelover
Member
 
Registered: Aug 2009
Posts: 311

Rep: Reputation: 36
perl getopt::long help


hello all,

I am trying out the getopt::long perl module and i have a small problem with it

Code:
#!/usr/bin/perl
use strict   
use Getopt::Long;
my $data =;
my $length=;
my $verbose;
$result = GetOptions ("length=s" => \$length, # numeric
"file=s" => \$data, # string
"verbose" => \$verbose); # flag
print "$length,$file";

The problem is when i give the arguments as
Code:
./test --length --file "hello.txt"
Where the argument for --length is omitted,the value of variable length becomes --file ,is there anyway i can stop this from happening?

I need to implement optlong in one of my code,and if the user forgets to give a value for an option,i dont want the next option to become its value . thanks
 
Old 11-23-2009, 10:18 AM   #2
centosboy
Senior Member
 
Registered: May 2009
Location: london
Distribution: centos5
Posts: 1,137

Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by kdelover View Post
hello all,

I am trying out the getopt::long perl module and i have a small problem with it

Code:
#!/usr/bin/perl
use strict   
use Getopt::Long;
my $data =;
my $length=;
my $verbose;
$result = GetOptions ("length=s" => \$length, # numeric
"file=s" => \$data, # string
"verbose" => \$verbose); # flag
print "$length,$file";

The problem is when i give the arguments as
Code:
./test --length --file "hello.txt"
Where the argument for --length is omitted,the value of variable length becomes --file ,is there anyway i can stop this from happening?

I need to implement optlong in one of my code,and if the user forgets to give a value for an option,i dont want the next option to become its value . thanks



you could add a condition to say if @ARGV is not equal to <number> return to help subrouting section. or if a number of arguments are not defined, then print correct syntax and exit.

Code:
if @ARGV != 3 
{
&help;
}
 
Old 11-23-2009, 01:12 PM   #3
kdelover
Member
 
Registered: Aug 2009
Posts: 311

Original Poster
Rep: Reputation: 36
Thanks,but the problem is if there are many options and the number of argumetns to take is not fixed,then it creates a problem.
 
Old 11-23-2009, 01:56 PM   #4
MBybee
Member
 
Registered: Jan 2009
Location: wherever I can make a living
Distribution: OpenBSD / Debian / Ubuntu / Win7 / OpenVMS
Posts: 440

Rep: Reputation: 57
What I do is check for a "-" character at the start of the returned value. That way it'll choke if a switch gets passed as a value.
 
Old 11-23-2009, 02:27 PM   #5
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
Check over the documentation for Getopt::Long a bit. You're defining --length in such a way that (1) an argument is required for it and (2) the argument is expected to be a string. Read over the section "Options with values".

If you haven't already, though, you might want to read over the entire "Getting started with Getopt::Long" section of the docs.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Don't know how to solve the Perl dependency (Getopt::Std) error that yum update gives info2share Linux - Software 2 07-07-2009 03:18 AM
C getopt segfault h/w Programming 3 01-14-2008 12:39 PM
Help for perl--- Getopt::Declare arobinson74 Programming 1 08-20-2004 10:31 AM
getopt(3) Berhanie Programming 8 06-15-2004 09:50 PM
Getopt() newbie... AMMullan Programming 6 02-11-2004 01:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration