Negatableoptionsinperl

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"; }

但是,当命令使用否定选项时,例如:command_name -notop,它不会向用户打印相同的警告。

有没有办法在这两种情况下获得相同的警告?

回答

是的,您可以确定您的选项(以任何一种形式)是否已在命令行上使用。由于您为选项使用哈希,您可以检查该选项是否存在:

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

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

print "Warningn" if exists $args{top};

Warning如果您使用-top-notop在命令行上,这将打印。Warning如果您不使用-top或,它将不会打印-notop


以上是Negatableoptionsinperl的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>