Untitled
const dotenv = require('dotenv'); // Load environment variables from .env file dotenv.config(); // Parse command-line arguments manually const args = process.argv.slice(2); // Initialize `nameUser` to "unknown user" let nameUser = "unknown user"; // Check if `--name` is present in the arguments const nameIndex = args.indexOf('--name'); if (nameIndex !== -1 && args[nameIndex + 1] && !args[nameIndex + 1].startsWith('--')) { // Use the value following `--name` if it exists and is not another argument nameUser = args[nameIndex + 1]; } else if (process.env.NAME) { // Use the value from `.env` file if `--name` is not provided nameUser = process.env.NAME; } // Output the greeting console.log(`Hello, ${nameUser}!`);
Leave a Comment