finish WIP - Complete rewrite; remove commented-out material + console.logs
This commit is contained in:
parent
1a1b189f7a
commit
86e16a9855
91
argparser.js
91
argparser.js
|
|
@ -61,16 +61,12 @@ function parse(argv) {
|
|||
|
||||
let out = []
|
||||
grouped.forEach((a, i) => {
|
||||
console.log('current', a);
|
||||
let next = grouped[i+1];
|
||||
console.log('next', next);
|
||||
|
||||
if ((a[0].type === 'option')
|
||||
&& next
|
||||
&& (next[0].type === 'arg_or_operand')) {
|
||||
console.log('next is a value');
|
||||
let next_shortened = next.map(n => n.arg);
|
||||
console.log('short', next_shortened);
|
||||
let o = {};
|
||||
o[a[0].arg] = next_shortened;
|
||||
out.push(o);
|
||||
|
|
@ -85,88 +81,5 @@ function parse(argv) {
|
|||
});
|
||||
}
|
||||
});
|
||||
console.log('out', out);
|
||||
}
|
||||
|
||||
/*
|
||||
// this is good, but doesn't handle multiple operands in a row
|
||||
|
||||
let out = [];
|
||||
|
||||
mapped.forEach((a, i) => {
|
||||
console.log(a, i);
|
||||
if ((a.type === 'flag') && (mapped[i+1].type === 'arg_or_operand')) {
|
||||
console.log('next is a value');
|
||||
let pair = {};
|
||||
pair[a.arg] = mapped[i+1].arg;
|
||||
out.push(pair);
|
||||
mapped.splice(i, 1);
|
||||
} else {
|
||||
let pair = {};
|
||||
pair[a.arg] = true;
|
||||
out.push(pair);
|
||||
}
|
||||
});
|
||||
//console.log(out);
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
argv.forEach((arg) => {
|
||||
// We'll treat `--long` flags and flags without dash prefixes the same,
|
||||
// so just discard double-dash prefixes
|
||||
if (arg.startsWith('--')) { arg = arg.substring(2); }
|
||||
|
||||
// Handle short flags, including
|
||||
// un-grouping grouped ones (eg `ps -aux`)
|
||||
if (arg.startsWith('-')) {
|
||||
console.log('startswith -; length:', arg.length);
|
||||
console.log('lose first char:', arg.substring(1));
|
||||
console.log('split keys vals:', arg.split('='));
|
||||
|
||||
// If there's a key=value assignment,
|
||||
// don't treat it like a group
|
||||
if (arg.split('=').length > 1) {
|
||||
let [key, value] = processArg(arg);
|
||||
finalOutput[key] = value;
|
||||
return;
|
||||
}
|
||||
|
||||
if (arg.length < 2) {
|
||||
// It's not grouped; just remove the '-' and treat it like the rest, later
|
||||
arg = arg.substring(1);
|
||||
let [key, value] = processArg(arg);
|
||||
finalOutput[key] = value;
|
||||
return;
|
||||
} else {
|
||||
arg = arg.substring(1);
|
||||
let ungrouped = arg.split('');
|
||||
ungrouped.forEach(a => output[a] = true);
|
||||
console.log('ungr', ungrouped);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
console.log('out:', output);
|
||||
|
||||
|
||||
output[arg] = true;
|
||||
});
|
||||
|
||||
return finalOutput;
|
||||
}
|
||||
|
||||
function processArg(a) {
|
||||
let out = {};
|
||||
let split = a.split('=')
|
||||
if (split.length > 2) { throw new Error(`Too many '=' in argument ${a}`); }
|
||||
let [key, value] = split;
|
||||
if (split.length === 2) {
|
||||
return [key, value];
|
||||
} else {
|
||||
return [key, true];
|
||||
}
|
||||
}
|
||||
*/
|
||||
return out;
|
||||
}
|
||||
Loading…
Reference in New Issue