5

I have the following code

export const program = new Command();

program.version('0.0.1');

program
  .command('groups')
  .command('create')
  .action(() => console.log('creating'))
  .command('delete')
  .action(() => console.log('deleting-all'))

program.parse(process.argv)

What I want to achieve is something like

groups create and groups delete

The code however chains that delete to the create. It recognizes groups create and groups create delete (which I dont want) but does not recognize the groups delete

1 Answer 1

11

You want to add the delete subcommand to the groups command. e.g.

const { Command } = require('commander');

const program = new Command();

program.version('0.0.1');

const groups = program
  .command('groups');
groups
  .command('create')
  .action(() => console.log('creating'))
groups
  .command('delete')
  .action(() => console.log('deleting-all'))

program.parse(process.argv)

The related example file is: https://github.com/tj/commander.js/blob/master/examples/nestedCommands.js

Sign up to request clarification or add additional context in comments.

2 Comments

Can you add deeply nested commands? Like foo groups something other create?
Yes. Just add each new subcommand to the previous command and build up a chain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.