CodeBlanks Logo Icon CodeBlanks

Putting it together

Build a working CLI app with process.argv.

Putting it together: a process.argv CLI

In the final lesson you will combine all pieces into a command-line application. The process.argv array carries the arguments given on the command line.

In Node.js process.argv has three parts: argv[0] is the Node path, argv[1] is the path of the file being run, and argv[2] onward are the user's arguments. So the first command is at process.argv[2].

Example: in node todo.js list, process.argv[2] is 'list'. In node todo.js add Domates, argv[2] is 'add' and argv[3] is 'Domates'. You read these arguments to drive the app's behaviour.

Command handling

A CLI app reads the user's command and behaves accordingly. The basic pattern is: read the command with const command = process.argv[2];, then branch with conditions like if (command === 'list').

This project will support three commands: list lists tasks, add adds a new task, and done completes a task. When no command is given, listing runs by default.

Example: if (command === 'add') { addTask(tasks, process.argv[3]); }. In node todo.js add Domates this adds a new task titled 'Domates'. Each command is handled in its own branch.

Practice Full CLI app

The addTask, listTasks and completeTask functions are provided in the starting code. Add three tasks ('Ekmek al', 'Süt al', 'Yumurta al'). Read the command from process.argv[2]. Always print the tasks with listTasks(tasks).

Starter code

index.js
function addTask(tasks, title) {
  tasks.push({ title, done: false });
}
function listTasks(tasks) {
  tasks.forEach((t, i) => console.log((t.done ? '[x] ' : '[ ] ') + t.title));
}
function completeTask(tasks, index) {
  if (index < tasks.length) {
    tasks[index].done = true;
  }
}
// Add three tasks, read the command from process.argv and list

Sample solution

index.js
function addTask(tasks, title) {
  tasks.push({ title, done: false });
}
function listTasks(tasks) {
  tasks.forEach((t, i) => console.log((t.done ? '[x] ' : '[ ] ') + t.title));
}
function completeTask(tasks, index) {
  if (index < tasks.length) {
    tasks[index].done = true;
  }
}
const tasks = [];
addTask(tasks, 'Ekmek al');
addTask(tasks, 'Süt al');
addTask(tasks, 'Yumurta al');
const command = process.argv[2];
listTasks(tasks);

Expected output

[ ] Ekmek al
[ ] Süt al
[ ] Yumurta al

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does process.argv[2] contain?

  • The file path
  • The Node path
  • The user's first command Correct answer
  • The task list

Explanation: argv[0] is Node, argv[1] the file, argv[2] the user's first command.

2 Match the CLI parts

Term Meaning
process.argv Argument array
argv[0] Node path
argv[1] File path
argv[2] User's first command

3 Read the command

Question
const command = process.argv[____];
Answer
const command = process.argv[2];

Explanation: argv[2] holds the user's first command.

All lessons in Beginner