Tarek.
← All posts

Building CLI Tools with Node.js: A Developer's Guide

· 1 min read · Admin

Argument Parsing with Commander

typescript
import { Command } from 'commander';const program = new Command();​program  .name('my-tool')  .description('A helpful CLI tool')  .version('1.0.0');​program  .command('init')  .argument('[name]', 'Project name')  .option('-t, --template <type>', 'Template to use', 'default')  .action(async (name, options) => {    console.log('Creating ' + name + ' with ' + options.template + ' template');  });​program.parse();

Interactive Prompts

typescript
import inquirer from "inquirer";​const answers = await inquirer.prompt([  { type: "input", name: "name", message: "Project name?" },  { type: "list", name: "framework", message: "Framework?", choices: ["React", "Vue", "Svelte"] },  { type: "confirm", name: "typescript", message: "Use TypeScript?", default: true },]);

Cross-Platform Compatibility

  • Use path.join() instead of string concatenation

  • Use os.homedir() instead of process.env.HOME

  • Use node:child_process instead of shell commands

  • Test on Windows, macOS, and Linux