
Commander.js Has a Free API — Here's How to Build Professional CLI Tools in Node.js
Commander.js is the most popular library for building command-line interfaces in Node.js. It handles argument parsing, subcommands, help generation, and option validation. Installation npm install commander Basic CLI import { program } from " commander " ; program . name ( " my-cli " ) . description ( " A CLI tool for managing projects " ) . version ( " 1.0.0 " ); program . command ( " greet " ) . description ( " Greet a user " ) . argument ( " <name> " , " Name to greet " ) . option ( " -l, --loud " , " Greet loudly " ) . action (( name , options ) => { const msg = `Hello, ${ name } !` ; console . log ( options . loud ? msg . toUpperCase () : msg ); }); program . parse (); my-cli greet World # Hello, World! my-cli greet World --loud # HELLO, WORLD! my-cli --help # Auto-generated help Options and Arguments program . command ( " deploy " ) . description ( " Deploy application " ) . requiredOption ( " -e, --env <environment> " , " Target environment " ) . option ( " -p, --port <number> "
Continue reading on Dev.to JavaScript
Opens in a new tab

