How to use the parseArgs function from commander

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

commander.parseArgs is a function that parses command-line arguments using the configuration set up by the Commander.js library and returns an object containing the parsed options and arguments.

93
94
95
96
97
98
99
100
101
102
var parsed = program.parseOptions(program.normalize(argv.slice(2)));
program.args = parsed.args;

// handle unknown options
if (parsed.unknown.length > 0) {
  program.parseArgs([], parsed.unknown);
}

// handle <dir>
if (program.args.length === 1) {
fork icon27
star icon199
watch icon8

165
166
167
168
169
170
171
172
173
174
});
// Hack to get unknown option errors to work. https://github.com/visionmedia/commander.js/pull/121
var parsed = commander.parseOptions(process.argv.slice(2));
commander.args = parsed.args;
if (parsed.unknown.length !== 0) {
    commander.parseArgs([], parsed.unknown);
}
var argv = commander.opts();
if (!(argv.init ||
    argv.test !== undefined ||
fork icon0
star icon0
watch icon1

+ 31 other calls in file

How does commander.parseArgs work?

commander.parseArgs is a function provided by the Commander.js library that can be used to parse command-line arguments. When called, it reads the command-line arguments and processes them according to the configuration set up by the Commander.js library. The function then returns an object containing the parsed options and arguments.

The commander.parseArgs function accepts two optional parameters:

  • args (default: process.argv.slice(2)) - An array of arguments to parse, typically obtained from process.argv.slice(2).
  • parseOptions (default: true) - A boolean indicating whether to parse options.

When parseOptions is true, the commander.parseArgs function will parse the command-line arguments and extract options and their corresponding values based on the configuration set up using the Commander.js API. It will then return an object containing the parsed options and their values.

If parseOptions is set to false, the commander.parseArgs function will only extract non-option arguments and return them as an array.

Overall, commander.parseArgs is a powerful function that can be used to quickly and easily parse command-line arguments in a Node.js application. It is especially useful when used in conjunction with the other features of the Commander.js library.

Ai Example

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

program
  .option("-n, --name ", "The name of the user")
  .option("-a, --age ", "The age of the user")
  .parse(process.argv);

const { name, age } = program.parseArgs();

console.log(`Name: ${name}`);
console.log(`Age: ${age}`);

In this example, we first set up a Commander.js program object using the .option() method to define two options: --name and --age. We then call the .parse() method to parse the command-line arguments. Next, we call program.parseArgs() to extract the parsed options and their values from the command-line arguments. We store the values in name and age, respectively. Finally, we log the values of name and age to the console. If we run the script with the following command-line arguments: css Copy code

Other functions in commander

Sorted by popularity

function icon

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