Skip to content

Commander

Complete solution for Node.js command-line interfaces.

Overview

Commander.js is a complete solution for Node.js command-line interface programs that takes inspiration from Ruby's commander.

Features

  • Options with flags
  • Required arguments
  • Variadic arguments
  • Automatic help generation
  • Subcommands
  • Git-style subcommands
  • Custom event listeners

Getting Started

bash
npm install commander

Basic Example

javascript
const { Command } = require('commander');
const program = new Command();

program
  .name('my-cli')
  .description('CLI to do some things')
  .version('1.0.0');

program
  .command('split')
  .description('Split a string into substrings')
  .argument('<string>', 'string to split')
  .option('--first', 'display just the first substring')
  .action((str, options) => {
    if (options.first) {
      console.log(str.split(',')[0]);
    } else {
      console.log(str);
    }
  });

program.parse();

PAPER-CODE Integration

PAPER-CODE provides:

  • Commander.js project templates
  • CLI structure patterns
  • Testing frameworks
  • Publishing configurations

Resources