Patrones de dise帽o en Node.js

1

Qu茅 es Node.js y c贸mo impulsa tu negocio

2

Patrones de dise帽o esenciales en Node.js

3

Patr贸n Singleton y Factory en JavaScript

4

Implementaci贸n pr谩ctica de Singleton y Factory en JavaScript

5

Implementaci贸n del patr贸n Observer con EventEmitter en Node.js

6

Implementaci贸n de Middlewares en Node.js sin Express

7

Decorators e inyecci贸n de dependencias en JavaScript

Flujo de Datos con Node.js

8

Aprende qu茅 son Buffer y Streams en Node.js

9

C贸mo utilizar streams y pipelines en Node.js

10

C贸mo funciona el Event Loop en Node.js

11

Qu茅 es Libuv y c贸mo maneja la asincron铆a en Node.js

12

Estrategias para ejecutar c贸digo as铆ncrono en Node.js

Debugging y Diagn贸stico en Node.js

13

C贸mo utilizar el Debugger en Node.js para solucionar problemas

14

Uso de Diagnostic Channels en Node.js para observabilidad y diagn贸stico

15

Instrumentaci贸n y m茅tricas clave en performance para aplicaciones Node.js

16

Control de errores globales y manejo de se帽ales en Node.js

17

Implementaci贸n Eficiente de Logs con Pino en Node.js

Performance en Node.js

18

An谩lisis del event loop en aplicaciones Node.js usando Nsolid

19

C贸mo Diagnosticar y Solucionar Memory Leaks en Aplicaciones Node.js

20

Optimizar rendimiento en Node.js con Worker Threads y Child Processes

21

Optimiza y Escala Aplicaciones Node.js con T茅cnicas de Caching

Creando CLIs con Node.js

22

C贸mo crear aplicaciones CLI con Node.js

23

C贸mo Crear un CLI con Minimist y Manejar Argumentos en Node.js

24

Creaci贸n de un CLI con Node.js y Google Generative AI

25

Creaci贸n de Chat con IA usando CLI en Node

26

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

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

0 D铆as
6 Hrs
32 Min
37 Seg

C贸mo Crear un CLI con Minimist y Manejar Argumentos en Node.js

23/26
Resources

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.

What are the challenges when creating a CLI?

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.

How to work with arguments from the terminal in Node.js?

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:

  • Position 0 corresponds to the location of the Node executable.
  • Position 1 is the script being executed.

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));

What kind of arguments can Minimist handle?

Minimist handles different types of arguments, which include:

  • String: for arguments that receive text (name, role or company).
  • Boolean: arguments that indicate true or false depending on their presence.

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' } });

How to implement Minimist in a new project?

To implement Minimist effectively in a new CLI project, it is recommended to follow these steps:

  1. Create a new JavaScript file in your editor, for example NiceCLI.js.
  2. Start a Node project in the CLI directory using pnpm init to generate an appropriate package.json.
  3. Install Minimist using the command
pnpm install minimist
  1. Add to the beginning of the script the shebang line:
#!/usr/bin/env node
  1. Grant execution permissions to the script using the command:
chmod +x NiceCLI.js
  1. Run the script by providing arguments directly from the terminal:
./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:

  • Arguments in long(--name) and short(-n) format.
  • Spacing or equal sign(=) work interchangeably.
  • Default boolean values(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

Sort by:

Want to see more contributions, questions and answers from the community?