How to use the parseAsync function from commander

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

commander.parseAsync is a method that asynchronously parses command line arguments using the defined options and commands.

435
436
437
438
439
440
441
442
443
444
445
446
        } else {
            program.help({error: true})
        }
    })


    await program.parseAsync(process.argv)
}


Promise.resolve().then(async () => {
    try {
fork icon73
star icon177
watch icon16

+ 3 other calls in file

1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
      refund: '0.02',
      relayerURL: program.relayer
    });
  });
try {
  await program.parseAsync(process.argv);
  process.exit(0);
} catch (e) {
  console.log('Error:', e);
  process.exit(1);
fork icon24
star icon10
watch icon0

How does commander.parseAsync work?

commander.parseAsync is an asynchronous version of commander.parse, which parses the process.argv and execute the associated command in async manner.

68
69
70
71
72
73
74
75
76
77
  .action(main({ linkExtractor, linkChecker }));

return {
  start(...args) {
    if(args.length > 0) {
      return program.parseAsync(...args);
    }
    else {
      return program.parseAsync(process.argv);
    }
fork icon4
star icon6
watch icon0

634
635
636
637
638
639
640
641
642
643
644
  .description('Create a diff file for locally available files')
  .action(diffLocalCommand);


async function main () {
  try {
    await commander.parseAsync();
  } catch (error) {
    console.error(error.name, error.message);
    process.exit(1);
  }
fork icon0
star icon4
watch icon2

Ai Example

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

program
  .option("-p, --port ", "Set the port number", "8080")
  .option("-v, --verbose", "Enable verbose logging")
  .parseAsync()
  .then((options) => {
    console.log(options);
  });

In this example, we define two options: -p/--port and -v/--verbose. The parseAsync method is then called on the program object, which returns a promise that resolves to an object containing the parsed options. We can then access and use the options as needed, for example, logging them to the console as shown here.

Other functions in commander

Sorted by popularity

function icon

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