perl - perl 中的可否定选项
问题描述
我有一个否定选项定义为顶部!.
当命令使用此选项时,例如:command_name -top,向用户打印警告
它的代码片段如下:
if ($args{top}) { print "Warning"; }
但是,当命令使用否定选项时,例如: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};
Warning
如果您使用其中一个-top
或-notop
在命令行上,这将打印出来。Warning
如果您不使用-top
或,它将不会打印-notop
。
推荐阅读
- r - ggplot:在线条图中标记 x 轴
- reactjs - 当我将项目上传到 github 时,我的 git 有什么问题?
- javascript - JavaScript charAt() 运行时
- odoo - 在 ODOO 中打开确认框之前检查一些字段
- c# - 通过 Web API 从 Excel 表(共享点)中获取价值
- amazon-ec2 - 地形。如何使用 for_each 识别元素(键)
- oracle-sqldeveloper - 退格键不适用于空格 Oracle SQL Developer 20.4.1
- scala - 如何确保一个方法在每个工作节点上只执行一次?
- java - 使用 log4j2 压缩超过 5 天的日志文件
- python - Http流,它是如何工作的?