Hi,
i've problem with reading arguments on command line. I don't know, how do I distinguish, if the user entered some argument with one or two dashes.

I try:

while($ARGV[$i])
{
     if($ARGV[$i] == '--anything')
   {
    print "The argument with '--'.\n";
   }
   else
   {
    print "The argumentwith '-'.\n";
   }
  
   $i++;
}

But if the input is --anything or -anything, the output is always The argument with '--'. But why??? :/

I tried also the GetOpt::Long module, but no result :/

Thanks for any advice.

Recommended Answers

All 5 Replies

Dude,

The problem with your code is, you are comparing two strings but using integer comparision operator. You must have used 'eq' instead of '=='. Next, lets make life easy! use Getopt module for effective use of switches.

When you use Getopt, you dont have to manually manipulate -- or - in your switch. It will throw error automatically if you use the switch wrongly. Getopt should work. The following is a sample for creating switch

use Getopt::Long;

GetOptions(
"anything!" => \$anything,
"integerswitch:i" => \$integer,
"stringswitch:s" => \$string, #
) or Usage("Missing option");

if ($anything) {
print "Life rocks!";
}

Cheers,
~RDX~

Dude,

The problem with your code is, you are comparing two strings but using integer comparision operator. You must have used 'eq' instead of '=='. Next, lets make life easy! use Getopt module for effective use of switches.

When you use Getopt, you dont have to manually manipulate -- or - in your switch. It will throw error automatically if you use the switch wrongly. Getopt should work. The following is a sample for creating switch

use Getopt::Long;

GetOptions(
"anything!" => \$anything,
"integerswitch:i" => \$integer,
"stringswitch:s" => \$string, #
) or Usage("Missing option");

if ($anything) {
print "Life rocks!";
}

Cheers,
~RDX~

Hey Cheers,
thank you for your anwer. I know this GetOpt example, but i NEED TO KNOW, when the user enters argument with '-' and when with '--'. Because for example i've following arguments:

--anything
-a
-b

And i need print error log, when the user enters '-anything', '--a', '--b'... short i need to know, if the argument is with one or two dashes
I'm beginner in Perl language, but this option I not found :/

#!/usr/bin/perl

use 5.006;
use strict;
use warnings;


foreach (@ARGV) {
	if (m/^--/) {
		print "$_ starts with two hyphens.\n";
	}
	elsif (m/^-/) {
		print "$_ starts with one hyphen.\n";
	}
}

Sorry dude, I thought you were facing problem with Getopt. If you want to throw an error message when the user enters with only one hyphen, then you can use the pattern matching method given by user d5e5. To be more specific you can check something like log the error if the switch doesnt contain two hyphens(i.e. one or more than two)

foreach my $arg (@ARGS) {
    if  ($arg =~  /^-/  && $arg !~ /^--[^\-]/) {
        print "Switch must contain only two hyphens!";
    }
}

~RDX~

commented: Good tip on checking for too many hyphens +1

d5e5, rdxblast - thank you guys for help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.