使用command.js创建一个 CLI,我有这样的声明:
cli.
command('serve').
alias('s').
description('Create a new project').
action((target) => {
console.log("TARGET IS: ", target) ....
target
无论是否向命令提供值,参数都会记录为值。我们如何检查target
参数是否为空?这是在没有值传递给命令时为目标值记录的内容:
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 }