gpt4 book ai didi

javascript - 检查指挥官中的参数是否为空?

转载 作者:行者123 更新时间:2023-12-03 01:56:05 25 4
gpt4 key购买 nike

使用commander.js创建一个 CLI,我有这样的声明:

 cli.
command('serve').
alias('s').
description('Create a new project').
action((target) => {
console.log("TARGET IS: ", target) ....

无论是否向命令提供值,参数target 都会记录为值。我们如何检查 target 参数是否为 null?这是当没有值传递给命令时记录的目标值:

        TARGET IS:  Command {
commands: [],
options: [],
_execs: {},
_allowUnknownOption: false,
_args: [],
_name: 'serve',
_noHelp: false,
parent:
Command {
commands:
[ [Command],
[Command],
[Command],
[Command],
[Command],
[Command],
[Circular],
[Command] ],
options: [ [Option] ],
_execs: {},
_allowUnknownOption: false,
_args: [],
_name: 'sfc',
Command: { [Function: Command] super_: [Function] },
Option: [Function: Option],
_version: '1.0.0',
_versionOptionName: 'version',
_events:
{ 'option:version': [Function],
'command:new': [Function: listener],
'command:n': [Function: listener],
'command:clean': [Function: listener],
'command:c': [Function: listener],
'command:build:main:css': [Function: listener],
'command:bmc': [Function: listener],
'command:build:test:css': [Function: listener],
'command:btc': [Function: listener],
'command:build': [Function: listener],
'command:b': [Function: listener],
'command:test:html': [Function: listener],
'command:t': [Function: listener],
'command:serve': [Function: listener],
'command:s': [Function: listener],
'command:dist': [Function: listener],
'command:d': [Function: listener] },
_eventsCount: 17,
_description: 'SuperflyCSS Command Line Interface',
_argsDescription: undefined,
rawArgs: [ '/usr/bin/node', '/home/ole/.npm-packages/bin/sfc', 'serve' ],
args: [ [Circular] ] },
_alias: 's',
_description: 'Serve project',
_argsDescription: undefined }

最佳答案

以下是 !(逻辑 NOT)运算符的可运行片段,用于检查变量是否为 null(或 false)。

   var nullVar = null;
if(!nullVar){alert("nullVar is null (or false)")}

因此您可以看到它可以轻松应用于您的案例。

关于javascript - 检查指挥官中的参数是否为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50223279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com