Dha*_*i P 1 perl options getopt getopt-long command-line-arguments
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"; }
Run Code Online (Sandbox Code Playgroud)
但是,当命令使用否定选项时,例如:command_name -notop,它不会向用户打印相同的警告。
有没有办法在这两种情况下获得相同的警告?
是的,您可以确定您的选项(以任何一种形式)是否已在命令行上使用。由于您为选项使用哈希,您可以检查该选项是否存在:
use warnings;
use strict;
use Getopt::Long qw(GetOptions);
my %args;
GetOptions(\%args, qw(top!));
print "Warning\n" if exists $args{top};
Run Code Online (Sandbox Code Playgroud)
Warning如果您使用-top或-notop在命令行上,这将打印。Warning如果您不使用-top或,它将不会打印-notop。