WIP - Change to allow something like `-f=file`
This commit is contained in:
parent
216cdcae53
commit
cec7d20a14
45
argparser.js
45
argparser.js
|
|
@ -5,37 +5,60 @@ module.exports = parse;
|
||||||
|
|
||||||
function parse(argv) {
|
function parse(argv) {
|
||||||
argv = process.argv.slice(2);
|
argv = process.argv.slice(2);
|
||||||
let output = {};
|
let finalOutput = {};
|
||||||
|
|
||||||
argv.forEach((arg) => {
|
argv.forEach((arg) => {
|
||||||
// We'll treat `--long` flags and flags without dash prefixes the same,
|
// We'll treat `--long` flags and flags without dash prefixes the same,
|
||||||
// so just discard double-dash prefixes
|
// so just discard double-dash prefixes
|
||||||
if (arg.startsWith('--')) { arg = arg.substring(2); }
|
if (arg.startsWith('--')) { arg = arg.substring(2); }
|
||||||
|
|
||||||
// Handle grouped short flags (eg `ps -aux`)
|
// Handle short flags, including
|
||||||
// (Let's not let grouped ones have values) //... FIXME, maybe?
|
// un-grouping grouped ones (eg `ps -aux`)
|
||||||
if (arg.startsWith('-')) {
|
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) {
|
if (arg.length < 2) {
|
||||||
// It's not grouped; just remove the '-' and treat it like the rest, later
|
// It's not grouped; just remove the '-' and treat it like the rest, later
|
||||||
arg = arg.substring(1);
|
arg = arg.substring(1);
|
||||||
|
let [key, value] = processArg(arg);
|
||||||
|
finalOutput[key] = value;
|
||||||
|
return;
|
||||||
} else {
|
} else {
|
||||||
arg = arg.substring(1);
|
arg = arg.substring(1);
|
||||||
let ungrouped = arg.split('');
|
let ungrouped = arg.split('');
|
||||||
ungrouped.forEach(a => output[a] = true);
|
ungrouped.forEach(a => output[a] = true);
|
||||||
|
console.log('ungr', ungrouped);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let split = arg.split('=')
|
console.log('out:', output);
|
||||||
if (split.length > 2) { throw new Error(`Too many '=' in argument ${arg}`); }
|
|
||||||
if (split.length === 2) {
|
|
||||||
let [key, value] = split;
|
|
||||||
output[key] = value;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
output[arg] = true;
|
output[arg] = true;
|
||||||
});
|
});
|
||||||
|
|
||||||
return output;
|
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];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue