NodeJs - Hiểu rõ hơn về process.argv 🍾 và ứng dụng

Để hiểu rõ về nó thì tốt nhất là ta nên chạy ngay đi!

🏃‍♀️ Chạy ngay đi

Tạo 1 file process-args.js:

process.argv.forEach((val, index) => {
  console.log(`${index}: ${val}`);
});

Hoặc là load an ES module, set "type": "module" trong package.json hoặc lấy tên file là process-args.mjs:

import { argv } from 'process';

// print process.argv
argv.forEach((val, index) => {
  console.log(`${index}: ${val}`);
});

Chạy:

node process-args one two=three four

Dưới đây là output từ local của mình:

0: /home/{user-name}/.fnm/node-versions/v16.13.0/installation/bin/node
1: /home/{user-name}/w/blogs/process-args.js
2: one
3: two=three
4: four

💁‍♀️ Giải thích

process.argv trả về 1 mảng gồm các string chứa lần lượt:

  1. PATH của NodeJs
  2. PATH của file source code
  3. Từ vị trí này là các argument được phân cách bằng dấu cách
  4. ...

process.argv giúp truyền mảng giá trị từ command-line dưới dạng string.

Cách lấy mảng các giá trị khi người dùng gõ command-line argument không theo 1 thứ tự nhất định

Để các value này đúng thứ tự ta có thể áp dụng nguyên tắc đánh dấu value của rất nhiều CLI khi sử dụng options.

Ví dụ NextJs CLI đánh dấu giá trị port bằng cách đặt trước giá trị 4000 1 argument nữa là -p.

node process-args -p 4000 -H 192.168.1.2

Cách xử lý đầu vào như trên là lấy argument kế bên mốc được đánh dấu:

const args = process.argv;
console.log('🚀 ~ args', args);

let port;
let host;

// first item is NodeJs PATH, second is src PATH
for (let i = 2; i < args.length; ++i) {
  const arg = args[i];
  const nextArg = args[i + 1];

  if (arg === '-p') {
    port = Number(nextArg);
  } else if (arg === '-H') {
    host = nextArg;
  }
}

console.log('🍾 ~ port', port);
console.log('🌎 ~ host', host);

Output:

🚀 ~ args [
  '/home/{user}/.fnm/node-versions/v16.13.0/installation/bin/node',
  '/home/{user}/w/blogs/process-args.js',
  '-p',
  '4000',
  '-H',
  '192.168.1.2'
]
🍾 ~ port 4000
🌎 ~ host 192.168.1.2

Về convention của CLI command-language syntax ta có thể tham khảo Angular CLI command-language syntax. Có thể đặt tên option theo camelCase or dash-case, như --myOptionName và --my-option-name.

✈️ Tham khảo


Photo by Krzysztof Niewolny on Unsplash.💁‍♀️✈️