How to use the mutex function from commander

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

commander.mutex is a feature of the Commander.js library that allows for mutually exclusive options, ensuring that only one of a set of options can be used at a time.

431
432
433
434
435
436
437
438
439
440
  process.exit(0);
};
// verbose logs outputs process.uptime() with this line we can sync uptime to absolute time on the computer
reporter.verbose(`current time: ${new Date().toISOString()}`);

const mutex = commander.mutex;
if (mutex && typeof mutex === 'string') {
  const parts = mutex.split(':');
  const mutexType = parts.shift();
  const mutexSpecifier = parts.join(':');
fork icon0
star icon0
watch icon1

+ 6 other calls in file

How does commander.mutex work?

commander.mutex is a feature of the Commander.js library that allows for the creation of mutually exclusive options, which ensures that only one of a set of options can be used at a time. This is useful in situations where certain options are conflicting or redundant and should not be used together.

To use the feature, you define the mutually exclusive options using the commander.option method, and then call the commander.mutex method to specify which options should be mutually exclusive.

For example, if you have two options --foo and --bar that should not be used together, you can define them as follows:

javascript
commander .option('--foo', 'foo option') .option('--bar', 'bar option');

To make them mutually exclusive, you call commander.mutex with an array of the option names that should be mutually exclusive:

javascript
commander .option('--foo', 'foo option') .option('--bar', 'bar option') .mutex('--foo', '--bar');

Now, if the user specifies both --foo and --bar options together, Commander.js will display an error message and exit the program, ensuring that only one of the options can be used at a time.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const commander = require("commander");

commander
  .option("--foo", "foo option")
  .option("--bar", "bar option")
  .mutex("--foo", "--bar");

commander.parse(process.argv);

if (commander.foo) {
  console.log("foo option used");
} else if (commander.bar) {
  console.log("bar option used");
}

In this example, we define two options --foo and --bar using the commander.option method. We then call the commander.mutex method with the option names as arguments to specify that they are mutually exclusive. If the user specifies both --foo and --bar options together, Commander.js will display an error message and exit the program, ensuring that only one of the options can be used at a time. Finally, we use the commander.foo and commander.bar properties to determine which option was used and display a corresponding message.

Other functions in commander

Sorted by popularity

function icon

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