ProgramGuru

Command-Line To-Do Application

Command-Line To-Do Application — purpose and prerequisites

A command-line to-do app needs add, list, complete, and remove operations. Keep the core task-management functions independent from argument parsing and file I/O. This makes both storage adapters and CLI commands easy to test.

What you’ll learn

  • Explain the purpose of command-line to-do application.
  • Identify the typed contract, runtime responsibilities and common pitfalls.
  • Run the example and extend its behavior with a practical exercise.

How the idea works

The app adds one task, marks its ID as done, and returns a defensive copy of the list. A full CLI layer translates argv commands to these methods.

The simple length-based ID generator would reuse IDs after a future delete feature; use a monotonic ID generator or stable UUID for persisted tasks.

Visualize the flow

Command-Line To-Do Application — learning flowCLI command → Task service → Persist + displayCommand-Line To-Do Application1CLI command2Task service3Persist + displayProject core logic is executable; integrations may need setup.
A three-step view of the responsibilities and the observable result.

Run the checked TypeScript example

This source was compiled using TypeScript 5.8.3 with strict checking, then executed with Node.js 22.16.0. Its stdout below is the actual captured output. The core logic is runnable without a framework or a remote API; integration-specific examples appear in a separate lab when needed.

TypeScript source

type Todo = { id: number; title: string; done: boolean };
class TodoList {
  private tasks: Todo[] = [];
  add(title: string): Todo {
    if (!title.trim()) throw new Error("Title required");
    const task = { id: this.tasks.length + 1, title: title.trim(), done: false };
    this.tasks.push(task);
    return { ...task };
  }
  complete(id: number): boolean {
    const task = this.tasks.find(task => task.id === id);
    if (!task) return false;
    task.done = true;
    return true;
  }
  list(): Todo[] { return this.tasks.map(task => ({ ...task })); }
}
const todos = new TodoList();
const task = todos.add("Study TypeScript");
console.log(todos.complete(task.id));
console.log(todos.list());

Verified output

true
[ { id: 1, title: 'Study TypeScript', done: true } ]

Compile: npx tsc --strict --target es2022 --module commonjs command-line-do-application.ts; run: node command-line-do-application.js. For examples importing node: modules, install @types/node as a development dependency; for browser or React projects use the separate lab setup.

Trace the execution

The app adds one task, marks its ID as done, and returns a defensive copy of the list. A full CLI layer translates argv commands to these methods.

Before editing the code, predict what the output will be for one changed input. Run it and compare your prediction with the actual result.

Common mistake and correction

The simple length-based ID generator would reuse IDs after a future delete feature; use a monotonic ID generator or stable UUID for persisted tasks.

Determine whether the issue belongs to the type system, a trust boundary, or actual runtime behavior before choosing a correction.

Your turn: extend the example

Add remove and update commands, choose stable IDs, and save the list to a JSON file.

Record expected success and failure outcomes, run the code, and compare the observed behavior. Keep the exercise compatible with strict type checking.

Check your understanding

Which statement correctly describes command-line to-do application in the example?
Choose one answer. You can try again.

Answer and explanation

The app adds one task, marks its ID as done, and returns a defensive copy of the list.

The app adds one task, marks its ID as done, and returns a defensive copy of the list. A full CLI layer translates argv commands to these methods.

Key idea to remember

The app adds one task, marks its ID as done, and returns a defensive copy of the list. A full CLI layer translates argv commands to these methods.

Apply the same reasoning to the practice task, then integrate it into a real app only when the runtime and dependency setup are ready.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Expense Tracker →