如果在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);

...