Skip to content

Making a CLI

Installing Commander

We're going to be using Commander to create our CLI.

bash
npm install commander

INFO

Notice that we did not use the -D flag when installing commander. This is because we want the users to get the code in the Commander package, as it is required to make the CLI actually work.

Setting up your CLI

In cli/index.js, we will make our Commander program:

js
// cli/index.js
import { program } from 'commander'

program.version('0.1.0').description('A CLI health tracker')

program.parse(process.argv)

TIP

The line program.parse(process.argv) should always go at the end of this file. It is what allows program access to the user's terminal commands, like meal add "Pizza" 2000.

Create a command

Next, we will add controllers. A controller receives a request from the user, and talks to the models to make things happen.

To create a controller for meals:

js
// cli/meal.js
import { Command } from 'commander'
import Meal from '../models/Meal.js'

const mealController = new Command('meal')

mealController
  .command('add <name> <calories>')
  .description('Log a new meal')
  .action((name, calories) => {
    const meal = new Meal(name, parseInt(calories))
    console.log(meal)
  })

export default mealController

What's this code doing?

  • Make a new mealController to handle user input like node cli meal
  • Make an add subcommand to handle node cli meal add "Pizza" 2000
  • Take the arguments (e.g. "Pizza", 2000) and pass them to the Meal model

Register the command

We have a program in cli/index.js and a mealController in cli/meal.js - we need to hook them up. In index.js, we do

js
import { program } from 'commander'
import mealController from './meal.js'

program.version('0.1.0').description('A CLI health tracker')

program.addCommand(mealController) 

program.parse(process.argv)

We've added the mealController to our program - now it's ready to use.

Use the command

The CLI should now be fully functioning. From the project root, open a terminal and run

bash
node cli meal add 'Lasagne' 1100
console
Meal { name: 'Lasagne', calories: 1100 }