Build a Basic CLI with Nodejs and Commander





Create a app.js file and add the below boilerplate code.
// import commander
const program = require("commander");

// set the version number ( You can run node app --version ) 
program.version("1.0.0").description("Client Management System");

// Create a add command
program
  .command("add <firstname> <lastname> <phone> <email>")
  .alias("a")
  .description("Add a customer")
  .action((firstname, lastname, phone, email) => {
    console.log({ firstname, lastname, phone, email });
  });

// Create a find command
program
  .command("find <name>")
  .alias("f")
  .description("Find a customer")
  .action(name => {
    console.log(`Customer found ${name}`);
  });

// Create a List command
program
  .command("list")
  .alias("l")
  .description("List all customers")
  .action(() => {
    console.log("List of customers");
  });

program.parse(process.argv);

Run node app --version

commander help


Run the add command

node commands.js add John Doe 0868907335 john.doe@gmail.com

{
  firstname: 'John',
  lastname: 'Doe',
  phone: '0868907335',
  email: 'john.doe@gmail.com'
}

Addition Resources

Commander npm package
Inquirer npm package
Build CLI Video