#!/usr/bin/env node
const process = require('process');
/**
* Formats command-line options with basic sanity checks.
* @param {object} options - The parsed command-line options object.
* @returns {string} - Formatted output string.
*/
function formatOptions(options) {
let output = '';
// Basic sanity checks
if (!options.name) {
return "Error: --name is required.";
}
if (typeof options.age !== 'number' || options.age < 0) {
return "Error: --age must be a non-negative number.";
}
if (options.verbose !== undefined && typeof options.verbose !== 'boolean') {
return "Error: --verbose must be a boolean value.";
}
output += `Name: ${options.name}\n`;
output += `Age: ${options.age}\n`;
if (options.verbose) {
output += `Verbose mode enabled.\n`;
}
if (options.email) {
output += `Email: ${options.email}\n`;
}
if (options.id) {
output += `ID: ${options.id}\n`;
}
return output;
}
// Example usage (simulating command-line parsing)
const parsedArgs = {
name: 'John Doe',
age: 30,
verbose: true,
email: 'john.doe@example.com',
id: '12345'
};
const formattedOutput = formatOptions(parsedArgs);
console.log(formattedOutput);
//Another example with error
const parsedArgsError = {
name: '',
age: -5
};
const formattedOutputError = formatOptions(parsedArgsError);
console.log(formattedOutputError);
Add your comment