Name the parts of a command line, install commander and describe TaskKit’s add and remind commands with arguments and a --due option, type what an action receives because commander passes any, let commander check every line and write the help, and pass options through npm start with --.
Fundamentals
TypeScript
`commander`: a real command-line interface. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
Rent is due on Friday, so you tell TaskKit both things at once:
bash
npm start add "pay rent" friday
On an empty list, under the banner, TaskKit prints:
Outputtext
Added: Pay rentAdded: Friday2 tasks added, room for 3 moreList 40% fullYour list, A to Z:#2 [todo] Friday, due: not set#1 [todo] Pay rent, due: not set
Friday is now a task, and Pay rent has no date. TaskKit cannot do
better: to its switch, every word after add is a task.
A paper form solves this. Its printed boxes, Task and Due,
tell you where each answer goes, and the same form tells you what you
left out. In this lesson TaskKit gets a form of its own, through a
package called commander. By the end, add takes a due date, a typo
gets an error that guesses what you meant, and npm start help prints
a help text that is always up to date, because nobody writes it by
hand.
What a command line is made of
A command-line interface is everything a program understands on
the line that starts it: which words it takes, in what order, and
what it answers. It has nothing to do with TypeScript's interface
(lesson 6.4): here the word has its plain meaning, the place where a
person and a program meet. TaskKit has had one since lesson 2.8,
written by hand; today it lives in command.ts. Lesson 2.8 called
every word after the file name a command-line argument. A real
interface sorts those words into three kinds:
A command names the job: add, remind, help. It comes
first.
An option is a named setting that starts with --, such as
--due friday. It is the flag of lesson 1.2, node --version, with
one difference: an option can take a value, the word after it.
You can leave an option out, and it can stand anywhere after the
command.
An argument is what the job works on: "pay rent". In
TaskKit's interface, every word that is neither the command nor an
option, nor an option's value, is an argument.
One more word joins the line now, and it is npm's, not TaskKit's.
npm start runs TaskKit and hands it the words you type after it, but
npm has options of its own, and it reads every word starting with --
as one of them. Two dashes standing alone, --, tell npm "the rest is
not mine": everything after them goes to TaskKit untouched. Press
Next step to follow one line from your keyboard to TaskKit's
code, or let it play:
npm start -- add --due friday "pay rent" ⏎
You type one line. npm reads it first: npm start and the --
are its own words.
add · --due · friday · pay rent
npm runs node index.ts and hands it every word after --.
Node keeps them in process.argv.
add · due = "friday" · "pay rent"
commander sorts them: the command, an option with its value,
and an argument.
action(["pay rent"], { due: "friday" })
commander calls the action you wrote for add, with the words
already in place.
npm keeps the words before --, and commander sorts the
rest into a command, an option and an argument
All 4 steps shown.Step 1 of 4: npm start -- add --due friday "pay rent" ⏎Step 2 of 4: add · --due · friday · pay rentStep 3 of 4: add · due = "friday" · "pay rent"Step 4 of 4: action(["pay rent"], { due: "friday" })
All 4 steps shown.
The last step is the one TaskKit's switch never had: your code gets
the task and the date apart, each one in its own place.
Install commander
Lesson 14.1 already ran commander through its four questions: it reads
command lines, it has hundreds of millions of downloads a week, it is
looked after, and its types are built in. Its newest version, 15.0.0,
came out on 29 May 2026, so lesson 14.2's rule, let a day-old version
wait a few days, is long met. In the taskkit folder, run:
added 1 package is commander alone: it needs no packages of its own,
so the lock gains one entry, and package.json gains
"commander": "^15.0.0". Your time, in milliseconds (ms), will differ,
and so may the version if a newer commander 15 is out.
Describe a command
With commander you do not read process.argv yourself. You describe
TaskKit's interface: the program's name, its commands, and what each
command takes. commander reads the line and checks it against that
description.
The description starts with an object that stands for the whole
program. commander hands you a class for it, Command, which you
import by name, import { Command } from "commander";, and new
builds one (lesson 13.1). This Command is commander's, not the
type Command in command.ts, which goes away later in this lesson:
Each method gives back the same program, so the calls are chained
(lesson 2.4), one per line. .name is the name the help prints.
.description is the sentence under it. In this lesson "the
description" is what you tell commander about TaskKit; the .d.ts file
of lesson 9.3 is always called the type description. .version takes
TaskKit's version, "0.1", and adds an option that prints it,
--version.
Then each command gets its own description. add takes tasks and a
due date:
.command("add") is the one call that does not give back program:
it makes a new command and gives back that, so the four calls under it
describe add, not the whole program.
.argument("[titles...]", …) says what add works on. The brackets
say whether the argument is needed: [titles] in square brackets
may be left out, <titles> in angle brackets may not. The three
dots say it takes as many words as you type, all collected in one
list, the way a rest parameter does (lesson 5.3).
.option("--due <date>", …) adds the option. <date> says it needs
a value, and commander keeps that value under the option's name,
due.
.action(…) hands commander the function to run when the line says
add: a callback (lesson 5.4). commander calls it with the
arguments first, titles, and then an object holding the options.
async stands in front of an arrow function's brackets as it
stands in front of function (lesson 11.4).
The second text in .argument and .option describes that argument
or option in the help.
The function does what the add case of the old switch did. The one
new part is options.due, which goes into the store with each
task.
Last, the line that does the work:
parseAsync reads process.argv, checks it against the description,
and calls the right action. It gives back a promise that is done when
the action is done, so await waits for add's action, which is
async because it waits for askTitles and save (lesson 11.4).
The types are yours to write
titles: string[] and { due?: string } above are not decoration.
commander brought its own type description, a .d.ts file
(lesson 14.1), but there it says that an action takes
...args: any[]. It cannot know, from the
words in "[titles...]", which types your function will get, so every
parameter it passes is any, the escape hatch of lesson 8.5. Leave
the types out, and the check lets everything through.
To try the pair below, save the Bad version as try.ts in taskkit.
Run the check, then run the file the way commander would get a line:
Then save the Good version over it and run npm run check again.
Delete try.ts before the steps further down.
Bad — no types on the parameters.
npm run check prints only its two > lines, and Node stops on the
marked line. Node's message starts with a line that names your file's
path and ends with a long list of at lines; both are cut here:
Good — the same mistake, with the
types written in: the check stops it before anything runs.
The check stops it, the message broken here to fit:
titles is a list, not a string. Untyped, the mistake shows only on a
run that reaches that line. Typed, the check finds it on every run of
npm run check.
The types come from what you wrote in .argument and .option, and
you write them to match it: an argument with three dots gives a list,
string[]. An option you can leave out may be missing, so its
property is optional, due?, and its type is string | undefined.
That is the type due already has in TaskKit's Task (lessons 6.2
and 6.3).
commander checks, and writes the help
From that one description, commander does two jobs your switch did
by hand. The first is checking. Before any action runs, commander
looks at the line in three steps: is there a command it knows, are the
options ones that command has, and are the arguments it needs there.
The first step that fails stops the line, and commander prints what is
wrong. remind, which you describe further down, takes its tasks in
angle brackets, <titles...>, so it needs at least one. Choose a line
on the tabs, press Next step to walk it
through the checks, or let it play:
% npm start -- add--duefriday"pay rent"
Typed
✓passes: Acommanditknows?
✓passes: Optionsitknows?
✓passes: Tasksitneeds?
addorremind
add
TaskKit printsAdded: Pay rent
commander checks every line against your description
before your code runs
All 5 steps shown.Step 1 of 5: Typed: add --due friday pay rentStep 2 of 5: A command it knows?: yesStep 3 of 5: Options it knows?: yesStep 4 of 5: Tasks it needs?: yesStep 5 of 5: Inside: Added: Pay rent
All 5 steps shown.
% npm start -- ad
Typed
✕fails: Acommanditknows?
not checked yet: Optionsitknows?
not checked yet: Tasksitneeds?
addorremind
ad
TaskKit answerserror: unknown command 'ad'
commander checks every line against your description
before your code runs
All 3 steps shown.Step 1 of 3: Typed: adStep 2 of 3: A command it knows?: noStep 3 of 3: Stopped: error: unknown command 'ad'
All 3 steps shown.
% npm start -- add--soon"pay rent"
Typed
✓passes: Acommanditknows?
✕fails: Optionsitknows?
not checked yet: Tasksitneeds?
addorremind
add
TaskKit answerserror: unknown option '--soon'
commander checks every line against your description
before your code runs
All 4 steps shown.Step 1 of 4: Typed: add --soon pay rentStep 2 of 4: A command it knows?: yesStep 3 of 4: Options it knows?: noStep 4 of 4: Stopped: error: unknown option '--soon'
commander checks every line against your description
before your code runs
All 5 steps shown.Step 1 of 5: Typed: remindStep 2 of 5: A command it knows?: yesStep 3 of 5: Options it knows?: yesStep 4 of 5: Tasks it needs?: noStep 5 of 5: Stopped: error: missing required argument 'titles'
All 5 steps shown.
None of those messages is in TaskKit's code. For a misspelled command
commander even guesses what you meant:
The second job is the help. commander builds it from the same
description, so the day you add a command or an option, the help
lists it. It adds a help command of its own:
The first line is the usage: how a line for this program is built.
Read [options] and [command] as you read [titles...]: they may be
left out. The -V and -h beside each option are short names for it,
which commander adds by itself. Ask about one command, and you get
its own page:
Every line of it comes from a string you wrote in .description,
.argument or .option. TaskKit's old help line,
Commands: add "task", remind "task", help, version, was one more
string to keep in step with the switch, and nothing would have
told you if it fell behind.
TaskKit gets real commands
commander is installed, and the description of add is written. Now
it goes in, and command.ts goes out.
1
Let the store keep a due date
In store.ts, add takes the due date as a second, optional
parameter (lesson 5.3) and keeps it with the task:
The Task type already has due?: string, so the check accepts it.
A call with no date, like the one in TaskKit's tests, still works:
due is then undefined, and the list says due: not set.
2
Describe TaskKit in index.ts
At the top of index.ts, readCommand goes, and two imports come
in: commander's class, and askTitles, which add now calls itself.
Then delete everything from try { to the end of the file: the
switch and its catch. Under await store.load();, put the first
two blocks from "Describe a command": the program, then add. Then
describe remind, the remind case of the old switch moved into an
action, and put parseAsync inside a try like the one that was
there:
remind takes <titles...> in angle brackets: a reminder of nothing
makes no sense, so commander refuses it. The catch stays for errors
from your own code, such as a tasks.json that cannot be saved.
3
Keep commander's errors red
Since lesson 14.3, TaskKit's errors are red. commander prints its own,
so tell it how: configureOutput takes an object whose outputError
is a function commander calls with each error's text, and with
write, the function that prints it.
It goes on program, before any .command(…). Each command copies
the program's settings at the moment it is made, so set after add
and remind, it would turn only the program's own errors red, and
remind's missing argument would print in your normal colour.
text and write need no types here: in this place commander's type
description does say what they are, a and a function.
A few things work differently now. npm start with no command prints
the whole help instead of Which command?. ADD gets
error: unknown command 'ADD', with no hint about lower case:
commander's guess only offers names spelled almost the same, and it
counts upper and lower case as different letters. And the old
version command is commander's --version option now:
npm start -- --version prints 0.1.
Predict before you run
One option, two tasks:
Predict
What due date does the list show for Call Mom?
The common mistake: an option npm keeps
TaskKit has run with npm start since lesson 9.2, and no line needed
--: a plain npm start add "buy milk" reaches TaskKit whole. So it
is easy to leave it out the first time a line has an option.
Common mistake
Typing an option for TaskKit without --
npm reads --due as one of its own options and prints two warnings
before TaskKit starts, the second one broken here to fit. The line
under them is what npm really runs, and --due is not in it:
Under the banner, TaskKit adds two tasks, Walk the dog and Friday,
both with due: not set: the cold open of this lesson, one more time.
npm warn Unknown cli config "--due". This will stop working in the next major version of npm.
The fix
Put -- right after npm start, and every word after it goes to
TaskKit:
Unknown cli config is npm saying "I took an option I do not know":
cli is short for command-line interface, and config for configuration,
npm's settings. When you see it, look for the -- that is missing.
"The next major version" is the next npm whose first number goes up,
12 after 11, which may no longer let the line through with only a
warning. For now a warning is not an error, so npm still runs TaskKit,
and nothing stops a wrong task from going into the list.
Pass all three tasks to complete the lesson with practice. Reach the end without them and it counts as read only.
Exercises
commander is not installed on this page, so the exercises read its
help, do by hand one small part of what it does for you, and type an
action the way commander calls it.
Recap
Recap · 3 questions
Check yourself
What does -- right after npm start do?
Check yourself
Why do you write the types of an action's parameters?
Check yourself
Where does the text of npm start help come from?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
commander: a real command-line interface
What things are
Word
What it means
Command-line interface
What a program understands on its command line
Command
The job, the first word TaskKit gets: add
Option
A named setting, maybe with a value: --due friday
Argument
What the job works on: "pay rent"
--
npm hands every word after it to TaskKit
commander
A package that reads, checks and explains a command line
Usage
The help's first line: how a line is built
Code
Write
What it does
npm install commander
Add commander to package.json and the lock
new Command()
The object that stands for the program
.name(), .description(), .version()
What the help says
.command("add")
A new command; the calls after it describe it
.argument("<titles...>")
Needed, and as many words as typed
.argument("[titles...]")
The same, but may be left out
.option("--due <date>")
An option that needs a value
.action(…)
Run your function with the arguments, then the options
await program.parseAsync()
Read the line, check it, run the action
.configureOutput({ outputError })
Print commander's errors your way
Where to go next
You can now name the parts of a command line, describe TaskKit's
commands, arguments and options to commander, type what its actions
get, and let it check every line and write the help.
Next is date-fns: working with due dates. --due friday reaches
TaskKit as the word friday, and the list prints it as text, just as
you typed it; that lesson turns it into a real date.
Before you move on, run npm start -- add --help. It prints the same
page as npm start help add. Then add .alias("a") under
.command("add"): it gives add a second, shorter name. Run
npm start help again, find where the help shows it, and add a task
with npm start a "water the plants".
string
4
Delete command.ts
Nothing imports command.ts any more. Delete it in VS Code (Visual
Studio Code, your editor). Its checks and its switch, with the help
line that was in index.ts, are now one description.
5
Check, run, and test
Delete tasks.json first, so the list starts empty. Then:
The check prints only its two > lines, and help prints the text
you read above. The last line prints, under the banner:
Try npm start ad: the error is red now. Then run the tests:
They still end with ℹ pass 14 and ℹ fail 0: store.add with one
parameter works as it did.
npm start help add
The help page of one command
Rules
Rule
What it means
Options after --
npm start -- add --due friday "pay rent"
Type the action's parameters
Untyped, they are any
[titles...] gives string[]
Three dots collect a list
An option may be missing
Type it due?: string
Settings before commands
Each command copies them when it is made
Unknown cli config
npm took your option: add the --
Exercise · Read the code
Which line does commander refuse
Say TaskKit grows two more commands, and its help lists them like
this (an id is short for identifier: the number that names a task, the
2 in #2):
Which line does commander stop before any of TaskKit's code runs?
Outputtext
added 1 package, and audited 22 packages in 491ms14 packages are looking for funding run `npm fund` for detailsfound 0 vulnerabilities
Outputtext
console.log(titles.toUpperCase()); ^TypeError: titles.toUpperCase is not a function
What npm run check printstext
> check> tsctry.ts:9:24 - error TS2339: Property 'toUpperCase' does not exist ontype 'string[]'.9 console.log(titles.toUpperCase()); ~~~~~~~~~~~Found 1 error in try.ts:9
Outputtext
error: unknown command 'ad'(Did you mean add?)
Outputtext
Usage: taskkit [options] [command]Your tasks, in your terminalOptions: -V, --version output the version number -h, --help display help for commandCommands: add [options] [titles...] add tasks to your list remind <titles...> remind you of tasks in two seconds help [command] display help for command
Outputtext
Usage: taskkit add [options] [titles...]add tasks to your listArguments: titles the tasks; if none, TaskKit asksOptions: --due <date> when the tasks are due -h, --help display help for command
Outputtext
npm warn "friday" is being parsed as a normal command line argument.npm warn Unknown cli config "--due". This will stop working in the nextmajor version of npm.> start> node index.ts add walk the dog friday
bash
npm install commander
index.tsts
const program = new Command();program .name("taskkit") .description("Your tasks, in your terminal") .version(version);
index.tsts
program .command("add") .description("add tasks to your list") .argument("[titles...]", "the tasks; if none, TaskKit asks") .option("--due <date>", "when the tasks are due") .action(async (titles: string[], options: { due?: string }) => { if (titles.length === 0) { titles = await askTitles(); } for (const title of titles) { const message = store.add(title, options.due); if (message.startsWith("Added:")) { console.log(chalk.green(message)); } else { console.log(chalk.yellow(message)); } } if (store.count() === 0) { console.log(`Add what? Try: npm start add "buy milk"`); } await store.save(); });
index.tsts
await program.parseAsync();
bash
npm run checknode try.ts add "pay rent"
try.tsts
import { Command } from "commander";const program = new Command();program .command("add") .argument("[titles...]") .option("--due <date>") .action((titles, options) => { console.log(titles.toUpperCase()); });await program.parseAsync();
try.tsts
import { Command } from "commander";const program = new Command();program .command("add") .argument("[titles...]") .option("--due <date>") .action((titles: string[], options: { due?: string }) => { console.log(titles.toUpperCase()); });await program.parseAsync();
import { readCommand } from "./command.ts";import { Command } from "commander";import { askTitles } from "./ask.ts";
index.tsts
program .command("remind") .description("remind you of tasks in two seconds") .argument("<titles...>", "what to remind you of") .action((titles: string[]) => { for (const title of titles) { const reminder = capitalize(title.trim()); console.log("Reminder set:", reminder); remindLater(reminder).catch((error: unknown) => { console.log(messageOf(error)); }); } });try { await program.parseAsync(); printSummary(store.count(), store.limit); printList(store.list());} catch (error) { console.log(chalk.red(messageOf(error)));}
index.tsts
program .name("taskkit") .description("Your tasks, in your terminal") .version(version); .version(version) .configureOutput({ outputError: (text, write) => write(chalk.red(text)), });
Write the body of readDue. It gets the words after -- as a list
and gives back the word right after --due, or undefined when there
is no --due: one small part of what commander does for an option.
Loading the editor…
Exercise · Fix the error
Types that match the description
addAction is the action of a command described with
.argument("<titles...>") and .option("--due <date>"). The two
calls below pass what commander would pass for add "pay rent" and
for add --due friday "call Mom", and TypeScript refuses both. Fix the
two types in the first line so they match what commander passes.
Change nothing else.
Loading the editor…
Outputtext
Added: Pay rent1 task added, room for 4 moreList 20% fullYour list, A to Z:#1 [todo] Pay rent, due: friday
Outputtext
done <ids...> mark tasks as done list [status] show your tasks