Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS Commander structured subcommands

I am new to commander and I am trying to achieve a command tree like this:

|- build
|    |- browser (+ options)
|    |- cordova (+ options)
|    |- no subcommands, just options
|- config
|    |- create (+ options)

Is it possible to split these commands up into multiple files, for example somewhat like this:

Central File:

const program = new commander.Command();
program.command('build').description(...);
program.command('config').description(...);

File for build command:

program.command('browser').description(...);
program.command('cordova').description(...);
program.option(...);

File for config command:

program.command('create').description(...);

I know of the Git-Style subcommands, but these seem to require executable files (I only have regular JS files)

like image 972
Lehks Avatar asked Mar 23 '26 18:03

Lehks


1 Answers

There is an example in their documentation here:

const commander = require('commander');
const program = new commander.Command();
const brew = program.command('brew');
brew
  .command('tea')
  .action(() => {
    console.log('brew tea');
  });
brew
  .command('coffee')
  .action(() => {
    console.log('brew coffee');
  });

Example output:

$ node nestedCommands.js brew tea
brew tea

https://github.com/tj/commander.js/blob/master/examples/nestedCommands.js

like image 77
Kim T Avatar answered Mar 25 '26 07:03

Kim T