最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Commander.js display help when called with no commands - Stack Overflow

programmeradmin2浏览0评论

I'm using commander.js to write a simple node.js program that interacts with an API. All calls require the use of subcommands. For example:

apicommand get

Is called as follows:

program
  .version('1.0.0')
  mand('get [accountId]')
  .description('retrieves account info for the specified account')
  .option('-v, --verbose', 'display extended logging information')
  .action(getAccount);

What I want to do now is display a default message when apicommand is called without any subcommands. Just like when you call git without a subcommand:

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
...

I'm using commander.js to write a simple node.js program that interacts with an API. All calls require the use of subcommands. For example:

apicommand get

Is called as follows:

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);

What I want to do now is display a default message when apicommand is called without any subcommands. Just like when you call git without a subcommand:

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
...
Share Improve this question asked Jun 2, 2017 at 20:05 toddgtoddg 2,9062 gold badges20 silver badges34 bronze badges 1
  • Check process.argv, it's an array that contains the parameters. – user5734311 Commented Jun 2, 2017 at 20:12
Add a comment  | 

3 Answers 3

Reset to default 16

You can do something like this by checking what arguments were received and if nothing other than node and <app>.js then display the help text.

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()
}

What I want to do now is display a default message when apicommand is called without any subcommands. Just like when you call git without a subcommand

The help is automatically displayed from Commander 5 onwards if you call without a subcommand.

(Disclosure: I am a maintainer of Commander.)

When you're trying to pass a command, it stores the commands in process.argv array.

You can add a conditon like at the end of your code like -:

if(process.argv.length  <= 2)
console.log(program.help());
else 
program.parse();
发布评论

评论列表(0)

  1. 暂无评论