tod*_*ddg 4 javascript command-line node.js node-commander
我正在使用commander.js编写一个与API交互的简单node.js程序.所有调用都需要使用子命令.例如:
apicommand get
Run Code Online (Sandbox Code Playgroud)
被称如下:
program
.version('1.0.0')
.command('get [accountId]')
.description('retrieves account info for the specified account')
.option('-v, --verbose', 'display extended logging information')
.action(getAccount);
Run Code Online (Sandbox Code Playgroud)
我现在要做的是在apicommand没有任何子命令的情况下调用时显示默认消息.就像git没有子命令时调用一样:
MacBook-Air:Desktop username$ git
usage: git [--version] [--help] [-C <path>] [-c name=value]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]
These are common Git commands used in various situations:
start a working area (see also: git help tutorial)
clone Clone a repository into a new directory
init Create an empty Git repository or reinitialize an existing one
...
Run Code Online (Sandbox Code Playgroud)
您可以通过检查接收的参数以及除了之外的其他任何内容来执行此类操作node,<app>.js然后显示帮助文本.
program
.version('1.0.0')
.command('get [accountId]')
.description('retrieves account info for the specified account')
.option('-v, --verbose', 'display extended logging information')
.action(getAccount)
.parse(process.argv)
if (process.argv.length < 3) {
program.help()
}
Run Code Online (Sandbox Code Playgroud)