Andy
Andy

Reputation: 2830

Is Perl's GetOpt::Long accepting abbreviations of switches a bug?

This is a simple script I have written to test command line argument handling:

use Getopt::Long;

my $help = 0;

GetOptions(
'help|h|?' => \$help,
) or die "Error!";

print "OK\n";

The results I got are as follows:

D:\>perl test.pl --help
OK

D:\>perl test.pl --hell
Unknown option: hell
Error! at test.pl line 10.

D:\>perl test.pl --he
OK

D:\>perl test.pl --hel
OK

Has anybody noticed this before? Is the behaviour (accepting he and hel instead of help) a potential bug?

Upvotes: 4

Views: 1127

Answers (3)

Michael Carman
Michael Carman

Reputation: 30831

This is a feature. Options may be abbreviated as long as the result is not ambiguous. If you don't want this behavior it is possible to disable it via configuration.

If this were a bug, the place to check to find out whether or not it was a known one is the bug queue at rt.cpan.org.

Upvotes: 8

Paul Tomblin
Paul Tomblin

Reputation: 182782

No, it's intentional. It accepts the shortest non-ambiguious version of the option, so if you had another option "--hex", it wouldn't accept "--he" but it would accept "--hel".

Upvotes: 8

dfa
dfa

Reputation: 116362

it is a feature documented here

Upvotes: 13

Related Questions