Dhanashri P
Dhanashri P

Reputation: 111

Negatable options in perl

I have a negatable option defined as top!.

When a command uses this option, say : command_name -top, prints a warning to the user

Code snippet for it is as follows :

if ($args{top}) { print "Warning"; }

But, when a command uses a negated option, say : command_name -notop, it does not print the same warning to the user.

Is there a way to get same warning for both the cases?

Upvotes: 1

Views: 211

Answers (1)

toolic
toolic

Reputation: 62037

Yes, you can determine if your option (in either form) has been used on the command line or not. Since you are using a hash for your options, you can check if the option exists:

use warnings;
use strict;
use Getopt::Long qw(GetOptions);

my %args;
GetOptions(\%args, qw(top!));

print "Warning\n" if exists $args{top};

This will print Warning if you use either -top or -notop on the command line. It will not print Warning if you do not use -top or -notop.

Upvotes: 5

Related Questions