How to use the on function from commander

Find comprehensive JavaScript commander.on code examples handpicked from public code repositorys.

commander.on is a method used in the Commander.js library to add event listeners to command line arguments.

35
36
37
38
39
40
41
42
43
44

for (const command of commands) {
  await Promise.resolve(command(program, client))
}

program.on('command:*', () => {
  console.error(`Invalid command: "${program.args.join(' ')}"`)
  console.error()
  program.outputHelp()
  process.exit(1)
fork icon99
star icon0
watch icon1

+ 11 other calls in file

37
38
39
40
41
42
43
44
45
46
  .option('--source-map', 'Enables building input\'s source map')
  .option('--source-map-inline-sources', 'Enables inlining sources inside source maps')
  .option('--with-rebase', 'Enable URLs rebasing')
  .option('--watch', 'Runs CLI in watch mode');

program.on('--help', function () {
  console.log('');
  console.log('Examples:\n');
  console.log('  %> cleancss one.css');
  console.log('  %> cleancss -o one-min.css one.css');
fork icon25
star icon276
watch icon0

+ 11 other calls in file

How does commander.on work?

The commander.on method in the Commander.js library is used to attach event listeners to various events that are emitted during the command-line parsing process, such as "option" or "command". These event listeners can be used to customize the behavior of the CLI application, for example to add custom validation for options or to execute additional code when a certain command is called. The on method takes two arguments: the name of the event to listen for, and a callback function that will be called when the event is emitted.

103
104
105
106
107
108
109
110
111
112
113
114


function parseArgs() {
    program
        .usage('bump')


    program.on('--help', function() {
        console.log('  Example usage:');
        console.log('');
        console.log('  $ ./release.js minor');
    });
fork icon12
star icon23
watch icon0

+ 2 other calls in file

28
29
30
31
32
33
34
35
36
37
    .usage('[parent-command] [options]');

commander
    .option('-A, --showAuth', 'Show API authorization details');

commander.on('--help', function () {
    configHelp();
});

// Use our customHelp when -h/--help is used
fork icon0
star icon15
watch icon2

+ 5 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
const { program } = require("commander");

program
  .version("1.0.0")
  .option("-n, --name ", "Your name")
  .on("option:name", (name) => {
    console.log(`Hello ${name}!`);
  })
  .parse(process.argv);

In this example, the on() method is used to listen for the option:name event which is triggered when the --name option is used in the command. When the event is triggered, the corresponding callback function is executed which logs a greeting message to the console.

77
78
79
80
81
82
83
84
85
86
    program.args.join(' ')
  )
  process.exit(1)
})

program.on('--help', () => {
  logger.info('')
  logger.info('Logging:')
  logger.info('  $ export LEVEL=info')
  logger.info(`    Level default is 'info'`)
fork icon0
star icon5
watch icon0

+ 7 other calls in file

94
95
96
97
98
99
100
101
102
103
        .option('--version', 'output version numbers', function () {
                mage.setTask('show-versions');
        });

exports.run = function () {
        program.on('*', function (args) {
                console.error('unrecognized command:', args[0]);

                program.outputHelp();
                process.exit(1);
fork icon19
star icon0
watch icon2

182
183
184
185
186
187
188
189
190
191
192
}


if (args.indexOf('--help') >= 0 || args.indexOf('-h') >= 0) {
  const examples = command && command.examples || [];
  if (examples.length) {
    commander.on('--help', () => {
      console.log('  Examples:\n');
      for (const example of examples) {
        console.log(`    $ yarn ${example}`);
      }
fork icon0
star icon0
watch icon1

+ 13 other calls in file

196
197
198
199
200
201
202
203
204
205
      console.log(`    $ yarn ${example}`);
    }
    console.log();
  });
}
commander.on('--help', () => console.log('  ' + getDocsInfo(commandName) + '\n'));

commander.parse(startArgs.concat(args));
commander.help();
process.exit(1);
fork icon0
star icon0
watch icon1

+ 13 other calls in file

Other functions in commander

Sorted by popularity

function icon

commander.Option is the most popular function in commander (1786 examples)