We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Help and Exit

A REPL is only useful if it does something! Our REPL will work using the concept of "commands". A command is a single word that maps to an action.

We're going to support two commands in this step:

  • help: prints a help message describing how to use the REPL
  • exit: exits the program

Assignment

export function commandExit();

This function should print Closing the Pokedex... Goodbye! then immediately exit the program. I used process.exit(0) to exit the program.

export type CLICommand = {
  name: string;
  description: string;
  callback: (commands: Record<string, CLICommand>) => void;
};

Then I created an object literal of supported commands:

export function getCommands(): Record<string, CLICommand> {
  return {
    exit: {
      name: "exit",
      description: "Exit the Pokedex",
      callback: commandExit,
    },
    // can add more commands here
  };
}
Welcome to the Pokedex!
Usage:

help: Displays a help message
exit: Exit the Pokedex

Generate the "usage" section by iterating over my registry of commands. That way the help command will always be up-to-date with the available commands. I recommend:

  • Put your handler callback functions in their own files (e.g. command_exit.ts, command_help.ts)
  • Pass the command registry to the callback functions (although exit will ignore it) like this export function commandHelp(commands: Record<string, CLICommand>)
  • Define the CLICommand type in its own file (e.g. command.ts)
  • You can import types explicitly in TypeScript like this: import type { CLICommand } from "./command.js";
  1. Test your code again manually.

Run and submit the CLI tests from the root of the repo.