Patrones de dise帽o en Node.js
Qu茅 es Node.js y c贸mo impulsa tu negocio
Patrones de dise帽o esenciales en Node.js
Patr贸n Singleton y Factory en JavaScript
Implementaci贸n pr谩ctica de Singleton y Factory en JavaScript
Implementaci贸n del patr贸n Observer con EventEmitter en Node.js
Implementaci贸n de Middlewares en Node.js sin Express
Decorators e inyecci贸n de dependencias en JavaScript
Flujo de Datos con Node.js
Aprende qu茅 son Buffer y Streams en Node.js
C贸mo utilizar streams y pipelines en Node.js
C贸mo funciona el Event Loop en Node.js
Qu茅 es Libuv y c贸mo maneja la asincron铆a en Node.js
Estrategias para ejecutar c贸digo as铆ncrono en Node.js
Debugging y Diagn贸stico en Node.js
C贸mo utilizar el Debugger en Node.js para solucionar problemas
Uso de Diagnostic Channels en Node.js para observabilidad y diagn贸stico
Instrumentaci贸n y m茅tricas clave en performance para aplicaciones Node.js
Control de errores globales y manejo de se帽ales en Node.js
Implementaci贸n Eficiente de Logs con Pino en Node.js
Performance en Node.js
An谩lisis del event loop en aplicaciones Node.js usando Nsolid
C贸mo Diagnosticar y Solucionar Memory Leaks en Aplicaciones Node.js
Optimizar rendimiento en Node.js con Worker Threads y Child Processes
Optimiza y Escala Aplicaciones Node.js con T茅cnicas de Caching
Creando CLIs con Node.js
C贸mo crear aplicaciones CLI con Node.js
C贸mo Crear un CLI con Minimist y Manejar Argumentos en Node.js
Creaci贸n de un CLI con Node.js y Google Generative AI
Creaci贸n de Chat con IA usando CLI en Node
C贸mo Crear e Instalar tu Propio CLI de Node con npm
You don't have access to this class
Keep learning! Join and start boosting your career
Creating an effective CLI(Command Line Interface) application requires proper handling of arguments received from the terminal. Using Node.js and the Minimist module, it is possible to handle user input as text, boolean or alias arguments in a simple way.
The first problem that arises when developing CLI applications is to correctly read the command input provided by the user. It is common for these applications to use modifiers, known as flags, or different commands to be executed, which makes it relevant to have adequate tools to interpret these arguments.
To read and manage arguments from the terminal, we can use the Minimist module. This popular library allows you to easily interact with user-supplied variables from within the Node.js process.
The arguments received in Node.js are located in the process.argv
array. Important to remember:
Therefore, the relevant user arguments start from position 2, so Minimist processes them from there:
const minimist = require('minimist');const argv = minimist(process.argv.slice(2));
Minimist handles different types of arguments, which include:
It is also possible to definedefaults and short aliases for ease of use in the CLI:
const argv = minimist(process.argv.slice(2), { string: ['name', 'role', 'company'], boolean: ['greeting'], default: { greeting: false }, alias: { s: 'greeting', n: 'name', r: 'role', c: 'company' } });
To implement Minimist effectively in a new CLI project, it is recommended to follow these steps:
NiceCLI.js
.pnpm init
to generate an appropriate package.json
.pnpm install minimist
#!/usr/bin/env node
chmod +x NiceCLI.js
./NiceCLI.js --name Adrian --rol CTO -c NotSource --salutation
Or, alternatively:
./NiceCLI.js --name=Adrian --rol=CTO -c=NotSource -s
Both forms are valid and Minimist will automatically perform the appropriate parsing:
(--name
) and short(-n
) format.(=
) work interchangeably.(false
) change to true
when used.With these steps, managing the arguments of your CLI applications will be simple and clear, allowing you to provide better usability and efficiency for the users of your command line tool.
Have you already had experience using Minimist or other similar tools? Share your impressions in the comments!
Contributions 0
Questions 0
Want to see more contributions, questions and answers from the community?