How to use Commander npm package in Node.js?

Commander is an npm package used to define command-line arguments for your application. The commander package makes it easier for you to read and parse command-line arguments. To use the commander package, go through the following steps:

  1. Install the commander package in your project by running the following command:

    $npm install commander
  2. Import commander module using the require() method.

    const {program} = require('commander');
  3. Use the option() method to specify the command-line arguments that the user can enter while running the program.

    program.option('command-line option', 'description', default-value);
  4. Next, call the parse() method on the program object to parse the command line arguments entered by the user.

    program.parse();
  5. After that, use the opts() method to get an object having command-line arguments as properties and values passed to those arguments as values.

    let options = program.opts();
  6. Finally, access the command-line arguments using the dot notation.

Let's see an example that shows you how to use commander package in Node.js.

const {program} = require('commander');
program
  .option('-p, --port-number <port>', 'Port number for accessing the server')
  .option('-d, --domain <domainame>', 'Domain name of the server');

program.parse();
let options = program.opts();
console.log(options);

Here is how you run the program:

$node index.js -p 8080 -d 'tutorialsandyou.com'

Output

{ portNumber: '8080', domain: 'tutorialsandyou.com' }

All the command-line arguments that your application accepts are defined using the option() method. The option() method accepts 3 arguments:

program.option('command-line option', 'description', default-value);
  1. Command line argument in the form of a short flag or a long name is passed as the first argument. If both a short flag and a long name are passed, then you separate them by space, comma, or vertical bar.
  2. Description of the command line argument.
  3. Default value of the argument.

For example, suppose you want a user to enter a port number while running your Node.js project. In that case, you call the option() method like this:

program.option('-p, --port-number <port>', 'Port number for accessing the server');

Here, -p is the short flag and --port-number is the long name.

If you want to define more than one command-line argument, then chain the option() method.

program
  .option('-p, --port-number <port>', 'Port number for accessing the server')
  .option('-d, --domain <domainame>', 'Domain name of the server');

How to pass command-line arguments to the option() method?

Different ways of providing arguments to the option() method are explained below:

1. A short flag or single character is defined in the option() method.

You access the argument using that single character. For example, -p is defined in the option() method; in this case, you access it like this: options.shortflag.

const {program} = require('commander');
program.option('-p <portnumber>', 'Port Number');
program.parse();
let options = program.opts();
console.log(`Port number entered by the user is ${options.p}.`);

Now, run the program and pass 8080 value to the -p flag.

$node index.js -p 8080

Output

Port number entered by the user is 8080.

2. A long name is passed as a command-line argument to the option() method.

You access that argument using that long name. For example, in the option() method, you define the --port argument. In this case, you access this argument in the following way:

const {program} = require('commander');
program.option('--port <portnumber>', 'Port Number');
program.parse();
let options = program.opts();
console.log(`Port number entered by the user is ${options.port}.`);

Run the program and pass 8080 to the --port flag.

$node index.js --port 8080

Output

Port number entered by the user is 8080.

When the command-line argument is a multi-word, it is camel-cased by the parse() method. For example, --port-number changes to portNumber.

const {program} = require('commander');
program.option('--port-number <port>', 'Port Number');
program.parse();
let options = program.opts();
console.log(`Port number entered by the user is ${options.portNumber}.`);

3. When both short flag and long name are defined in the option() method, you access that argument using the long name.

const {program} = require('commander');
program.option('-p, --port <portnumber>', 'Port Number');
program.parse();
let options = program.opts();
console.log(`Port number entered by the user is ${options.port}.`);

You run the program in the following way:

$node index.js -p 8080
OR
$node index.js --port 8080

How to specify a value to the command line argument?

The command-line argument can have two types of values, boolean or a string.

If you want the command-line argument to accept a value, use angle brackets. Inside the angle brackets, you mention any name. For example:

const {program} = require('commander');
program.option('-p <portnumber>', 'Port Number');
program.parse();
let options = program.opts();
console.log(`Port number entered by the user is ${options.p}.`);

When a user runs a program like this $node index.js -p 8080 then the -p option will have a value of 8080. On the other hand, if a user does not pass the -p option while running the program $node index.js, then the -p will have a default value if it is defined or an undefined value.

const {program} = require('commander');
program.option('-d, --debug');
program.parse();
let options = program.opts();
console.log(`The value of --debug option is ${options.debug}.`);

If you don't use angle brackets, then that argument will have true value when the user passes that argument while running the program. On the other hand, if a user does not pass the argument then that argument will have a default value if it is defined or an undefined value.

$node index.js --debug

Output

The value of --debug option is true.

How to specify the default value to the command-line arguments?

By default, the value of an argument is undefined. But if you want to specify the argument's default value, then pass a value as a third argument to the option() method.

const {program} = require('commander');
program
  .option('-p, --port-number <port>', 'Port number for accessing the server', 8080)
  .option('-d, --domain <domainame>', 'Domain name');

program.parse();
let options = program.opts();
console.log(`The value of -d option is ${options.domain}.`);
console.log(`The value of -p option is ${options.portNumber}.`);

In the option() method, no default value is passed to the -d option, whereas 8080 is mentioned as a default value to the -p option.

The user can run this program in three different ways:

1. A user does not pass any command-line arguments

$node index.js

Output

The value of -d option is undefined.
The value of -p option is 8080.

Here the user hasn't specified any command-line arguments. In this case, undefined is assigned to the -d option, and 8080 is assigned to the -p option.

2. A user passes a value to one of the arguments

$node index.js -p 1234

Output

The value of -d option is undefined.
The value of -p option is 1234.

The user runs the program and passes 1234 to the -p option. As you can see, the -d option has an undefined value because the user has not passed any value to it. On the other hand, the -p option has a value of 1234.

3. A user passes a value to both arguments

$node index.js -p 80 -d 'tutorialsandyou.com'

Output

The value of -d option is tutorialsandyou.com.
The value of -p option is 80.

In this case, 80 is assigned to the -p option. The value of tutorialsandyou.com is passed to the -d option.

Recommended Posts