/**
* Parses log stream arguments with sanity checks.
*
* @param {string[]} args Array of command-line arguments.
* @returns {object} An object containing parsed arguments and error information.
*/
function parseLogStreamArgs(args) {
const parsedArgs = {};
let errors = [];
// Basic argument parsing with type validation
if (args.length < 2) {
errors.push("Missing required arguments: <log_file> <level>");
return { parsedArgs: null, errors };
}
const logFile = args[0];
const level = args[1];
let format = "default";
// Validate log file
if (!logFile || typeof logFile !== 'string') {
errors.push("Invalid log file path.");
return { parsedArgs: null, errors };
}
// Validate log level
const validLevels = ["debug", "info", "warn", "error", "critical"];
if (!validLevels.includes(level.toLowerCase())) {
errors.push(`Invalid log level. Valid levels are: ${validLevels.join(", ")}`);
return { parsedArgs: null, errors };
}
parsedArgs.logFile = logFile;
parsedArgs.level = level.toLowerCase();
//Optional format argument
if(args.length > 2){
format = args[2];
if(format !== "json" && format !== "text"){
errors.push("Invalid format. Valid formats are 'json' and 'text'.");
return {parsedArgs: null, errors};
}
parsedArgs.format = format;
}
return { parsedArgs, errors };
}
Add your comment