如果在Perl中找不到必需的参数,则从脚本退出

问题描述

我有应该从命令行接受两个参数的脚本。为此,我使用了@Entity(primaryKeys = ["firstName","lastName"]) // <-- Composite primary key data class UserAccessToCompany( val user_idUser: Int,val company_idcompany: Int ) @Entity(foreignKeys = [ForeignKey( entity = UserAccessToCompany::class,parentColumns = arrayOf("user_idUser","company_idcompany"),childColumns = arrayOf("user_access_to_company_idUser","user_access_to_company_idcompany")) ]) // <-- Composite primary key data class Notification( @PrimaryKey val idNotification: Int,// ............... val user_access_to_company_idUser: Int,val user_access_to_company_idcompany: Int ) Perl模块。

这是脚本:

Getopt::Long

该脚本在肯定的情况下也可以正常工作,即,如果我向脚本传递2个参数,它将在屏幕上打印这些参数。

但是,如果我仅将一个参数传递给脚本,则应使用#!/usr/bin/perl use strict; use warnings; use Getopt::Long 'HelpMessage'; GetOptions( 'node|n=s' => \my $node,'cmd|c=s' => \my $command,'help' => sub { HelpMessage(0) } ) or HelpMessage(1); print "Node:$node\nCmd:$command\n"; doSomeOpearation($node,$command); print "END\n"; sub doSomeOpearation { my ($n,$c) = @_; #... return; } HelpMessage(1) unless ($node && $command); =head1 NAME run_command - Run Commands on SVC Server =head1 SYNOPSIS --node,-n Node name (required) --command,-c Command (required) --help,-h Print this help =head1 VERSION 1.00 =cut 函数。脚本不是向我发出HelpMessage警告,而是向我显示Use of uninitialized value $command in concatenation (.) or string at script2.pl line 14.消息。

除非有2个参数,如何打印END并退出脚本?

解决方法

您的支票来不及了。

doSomeOpearation($node,$command);

...

HelpMessage(1) unless ($node && $command);

应该是

HelpMessage(1) unless ($node && $command);

doSomeOpearation($node,$command);

...

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...