The Command Line and the process Object: Building a CLI
Teach your Node programs to listen to the terminal and talk back
node app.js, Node hands your program a special helper called process. It carries the words you typed after the file name, the settings of your computer, and the pipes for reading input and printing output. Once you know how to use it, you can build a real command-line tool - a program that takes instructions from the terminal.
- read the words typed after
node app.jsusingprocess.argv, - read computer settings with environment variables (
process.env), - print output with
process.stdoutand read typed input fromprocess.stdin, - end your program with an exit code using
process.exit, - put it all together into a small CLI (command-line interface) tool.
1. What is a command-line tool?
A command-line tool (also called a CLI, short for command-line interface) is a program you run by typing its name in the terminal, often with a few extra words that tell it what to do. You have used many already! When you type:
node app.js
npm install express
git commit -m "hello"
...each of those is a command-line tool. The first word is the program (node, npm, git), and the words after it are arguments - the extra instructions.
Analogy. Think of a CLI like ordering at a sandwich counter. You do not just say "sandwich" - you say "sandwich, no onions, extra cheese". The word "sandwich" is the program, and "no onions" and "extra cheese" are the arguments that change what you get. Your Node program is the sandwich maker: it reads your order and builds exactly what you asked for.
In this topic you will build your own sandwich counter - a program the user can boss around from the terminal.