How to use the CommanderError function from commander

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

commander.CommanderError is an error class in the Commander.js library that represents an error that occurred during command-line argument parsing or execution.

11
12
13
14
15
16
17
18
19
20
 * Expose classes. The FooT versions are just types, so return Commander original implementations!
 */

exports.Argument = commander.Argument;
exports.Command = commander.Command;
exports.CommanderError = commander.CommanderError;
exports.Help = commander.Help;
exports.InvalidArgumentError = commander.InvalidArgumentError;
exports.InvalidOptionArgumentError = commander.InvalidArgumentError; // Deprecated
exports.Option = commander.Option;
fork icon4
star icon27
watch icon2

+ 3 other calls in file

720
721
722
723
724
725
726
727
728
729
730
 * @param {Array<Number>|String} data - The data to write
 */
const writeFileToDisk = (m8FilePath, data) => {
  // Do not overwrite file (We could revisit this at a later date but for safety, let's not for now.)
  if (existsSync(m8FilePath)) {
    throw new commander.CommanderError(1, 'm8.export.FileExists', `Cannot write to file at ${m8FilePath}: File exists`)
  }


  writeFileSync(m8FilePath, data)
}
fork icon1
star icon29
watch icon0

+ 7 other calls in file

How does commander.CommanderError work?

commander.CommanderError is an error class in the Commander.js library that represents an error that occurred during command-line argument parsing or execution. When a parsing or execution error occurs, Commander.js will throw an instance of commander.CommanderError. You can catch this error using a try...catch block or by listening for the error event on the program object. commander.CommanderError instances have a code property that indicates the type of error that occurred. Possible values include: commander.missingArgument: an expected argument was not provided. commander.unknownOption: an unknown option was provided. commander.optionMissingArgument: an option that requires an argument was not provided one. In essence, commander.CommanderError provides a way to handle errors that occur during command-line argument parsing and execution in a structured way, allowing you to detect and handle errors programmatically.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const commander = require("commander");

const program = new commander.Command();

program
  .command("hello ")
  .description("say hello to the given name")
  .action((name) => {
    console.log(`Hello, ${name}!`);
  });

try {
  program.parse(process.argv);
} catch (err) {
  if (err instanceof commander.CommanderError) {
    console.error(`Error: ${err.message}`);
    process.exit(1);
  } else {
    throw err;
  }
}

In this example, we first import the commander module. We then create a new Command instance using new commander.Command(). We then use the command method to define a new command named 'hello' that takes a required argument named 'name'. We then use the description method to set a description for the command. We then use the action method to specify the function to run when the command is executed. In this case, we simply log a message to the console that says "Hello, {name}!". We then wrap the call to program.parse(process.argv) in a try...catch block to handle any errors that occur during parsing or execution. Inside the catch block, we check whether the error is an instance of commander.CommanderError. If it is, we log an error message to the console and exit the process with an error code of 1. If the error is not a commander.CommanderError, we re-throw the error to be handled by a higher-level error handler. This example demonstrates how you can use commander.CommanderError to handle errors that occur during command-line argument parsing and execution in a structured way.

Other functions in commander

Sorted by popularity

function icon

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