Parse Command Line Args
NodeJS
Medium
4 views
Problem Description
Read --topic and --level from process.argv and print them as JSON.
Output Format
Print JSON.
Constraints
Support args like: --topic node --level easy.
Official Solution
function getArg(flag, fallback) {
const i = process.argv.indexOf(flag);
if (i === -1) return fallback;
const v = process.argv[i + 1];
return v && !v.startsWith('--') ? v : fallback;
}
const out = {
topic: getArg('--topic', 'node'),
level: getArg('--level', 'easy')
};
console.log(JSON.stringify(out, null, 2));
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!