config: fix and refactor arg parsing.
This commit is contained in:
parent
d3675bfcf8
commit
1d33816d89
@ -35,6 +35,7 @@ function Config(module) {
|
||||
this.env = Object.create(null);
|
||||
this.args = Object.create(null);
|
||||
this.argv = [];
|
||||
this.pass = [];
|
||||
this.query = Object.create(null);
|
||||
this.hash = Object.create(null);
|
||||
}
|
||||
@ -45,16 +46,9 @@ function Config(module) {
|
||||
*/
|
||||
|
||||
Config.alias = {
|
||||
conf: {},
|
||||
env: {
|
||||
'seed': 'seeds',
|
||||
'node': 'nodes'
|
||||
},
|
||||
arg: {
|
||||
'seed': 'seeds',
|
||||
'node': 'nodes',
|
||||
'n': 'network'
|
||||
}
|
||||
'seed': 'seeds',
|
||||
'node': 'nodes',
|
||||
'n': 'network'
|
||||
};
|
||||
|
||||
/**
|
||||
@ -112,8 +106,8 @@ Config.prototype.open = function open(file) {
|
||||
return;
|
||||
|
||||
const path = this.getFile(file);
|
||||
let text = null;
|
||||
|
||||
let text;
|
||||
try {
|
||||
text = fs.readFileSync(path, 'utf8');
|
||||
} catch (e) {
|
||||
@ -664,7 +658,6 @@ Config.prototype.parseConfig = function parseConfig(text) {
|
||||
eq = col;
|
||||
|
||||
let key, value;
|
||||
|
||||
if (eq === -1) {
|
||||
key = line.trim();
|
||||
value = '';
|
||||
@ -675,7 +668,7 @@ Config.prototype.parseConfig = function parseConfig(text) {
|
||||
|
||||
key = key.replace(/\-/g, '').toLowerCase();
|
||||
|
||||
const alias = Config.alias.conf[key];
|
||||
const alias = Config.alias[key];
|
||||
|
||||
if (alias)
|
||||
key = alias;
|
||||
@ -702,25 +695,46 @@ Config.prototype.parseArg = function parseArg(argv) {
|
||||
|
||||
assert(Array.isArray(argv));
|
||||
|
||||
let key;
|
||||
let last = null;
|
||||
let pass = false;
|
||||
|
||||
for (let i = 2; i < argv.length; i++) {
|
||||
let arg = argv[i];
|
||||
let value, alias, equals;
|
||||
const arg = argv[i];
|
||||
|
||||
assert(typeof arg === 'string');
|
||||
|
||||
if (arg === '--') {
|
||||
pass = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (pass) {
|
||||
this.pass.push(arg);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (arg.length === 0) {
|
||||
last = null;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (arg.indexOf('--') === 0) {
|
||||
// e.g. --opt
|
||||
arg = arg.split('=');
|
||||
key = arg[0];
|
||||
const parts = arg.split('=');
|
||||
|
||||
if (arg.length > 1) {
|
||||
let key = parts[0];
|
||||
let value = null;
|
||||
let empty = false;
|
||||
|
||||
if (parts.length > 1) {
|
||||
// e.g. --opt=val
|
||||
value = arg.slice(1).join('=').trim();
|
||||
equals = true;
|
||||
value = parts.slice(1).join('=').trim();
|
||||
last = null;
|
||||
empty = false;
|
||||
} else {
|
||||
value = 'true';
|
||||
equals = false;
|
||||
last = null;
|
||||
empty = true;
|
||||
}
|
||||
|
||||
key = key.replace(/\-/g, '');
|
||||
@ -731,41 +745,46 @@ Config.prototype.parseArg = function parseArg(argv) {
|
||||
if (value.length === 0)
|
||||
continue;
|
||||
|
||||
// Do not allow one-letter aliases.
|
||||
if (key.length > 1) {
|
||||
alias = Config.alias.arg[key];
|
||||
const alias = Config.alias[key];
|
||||
if (alias)
|
||||
key = alias;
|
||||
}
|
||||
|
||||
this.args[key] = value;
|
||||
|
||||
if (empty)
|
||||
last = key;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (arg[0] === '-') {
|
||||
// e.g. -abc
|
||||
arg = arg.substring(1);
|
||||
|
||||
for (key of arg) {
|
||||
alias = Config.alias.arg[key];
|
||||
last = null;
|
||||
for (let j = 1; j < arg.length; j++) {
|
||||
let key = arg[j];
|
||||
const alias = Config.alias[key];
|
||||
if (alias)
|
||||
key = alias;
|
||||
this.args[key] = 'true';
|
||||
equals = false;
|
||||
last = key;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
// e.g. foo
|
||||
value = arg.trim();
|
||||
const value = arg.trim();
|
||||
|
||||
if (value.length === 0)
|
||||
if (value.length === 0) {
|
||||
last = null;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (key && !equals) {
|
||||
this.args[key] = value;
|
||||
key = null;
|
||||
if (last) {
|
||||
this.args[last] = value;
|
||||
last = null;
|
||||
} else {
|
||||
this.argv.push(value);
|
||||
}
|
||||
@ -792,26 +811,30 @@ Config.prototype.parseEnv = function parseEnv(env) {
|
||||
assert(env && typeof env === 'object');
|
||||
|
||||
for (let key of Object.keys(env)) {
|
||||
let value = env[key];
|
||||
|
||||
assert(typeof value === 'string');
|
||||
|
||||
if (key.indexOf(prefix) !== 0)
|
||||
continue;
|
||||
|
||||
assert(typeof env[key] === 'string');
|
||||
|
||||
const value = env[key].trim();
|
||||
|
||||
key = key.substring(prefix.length);
|
||||
key = key.replace(/_/g, '').toLowerCase();
|
||||
|
||||
if (key.length === 0)
|
||||
continue;
|
||||
|
||||
value = value.trim();
|
||||
|
||||
if (value.length === 0)
|
||||
continue;
|
||||
|
||||
const alias = Config.alias.env[key];
|
||||
|
||||
if (alias)
|
||||
key = alias;
|
||||
// Do not allow one-letter aliases.
|
||||
if (key.length > 1) {
|
||||
const alias = Config.alias[key];
|
||||
if (alias)
|
||||
key = alias;
|
||||
}
|
||||
|
||||
this.env[key] = value;
|
||||
}
|
||||
@ -878,7 +901,6 @@ Config.prototype.parseForm = function parseForm(query, map) {
|
||||
const index = pair.indexOf('=');
|
||||
|
||||
let key, value;
|
||||
|
||||
if (index === -1) {
|
||||
key = pair;
|
||||
value = '';
|
||||
@ -898,7 +920,7 @@ Config.prototype.parseForm = function parseForm(query, map) {
|
||||
if (value.length === 0)
|
||||
continue;
|
||||
|
||||
const alias = Config.alias.env[key];
|
||||
const alias = Config.alias[key];
|
||||
|
||||
if (alias)
|
||||
key = alias;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user