-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecodeArgs.js
More file actions
130 lines (127 loc) · 4.63 KB
/
Copy pathdecodeArgs.js
File metadata and controls
130 lines (127 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* Parse arguments into a decoded argument result.
* Keys:
* values: object with values for all options
* optionValues: option flag values only (no positional)
* command: entry from positional commands objects
* commandPath: names of the commands selected at each nesting level
* apply: flattened arguments ready to apply
* error: "description of error"
* optDesc: structure used to parse options
* @param {*} opts
* @param {*} args
*/
module.exports = function decodeArgs(optDesc, argv) {
let result = {optDesc, values: {}, optionValues: {}, apply: [], command: null, commandPath: []};
let args = argv.concat(), pos = optDesc.positional.concat();
// Default command: when this group has a '' entry and the first token isn't
// a named sub-command, dispatch to the default *without* consuming the token
// — its args/options belong to the default. fncli injects '' so `fncli(fn)`
// runs fn while `completions` stays a normal sibling command.
if (optDesc.commands && optDesc.commands[''] && !optDesc.commands[args[0]]) {
result.command = optDesc.commands[''];
result.commandPath.push('');
optDesc = result.command.optDesc;
pos = optDesc.positional.concat();
}
let arg, m, ix = 0, allowOptions = true, inRest = null;
while (ix < args.length) {
arg = args[ix++];
if (arg === '--' && allowOptions) {
allowOptions = false;
continue;
}
if (allowOptions && (m = arg.match(/^--([\w-]+)(?:=(.*))?/))) {
let [, optName, optVal] = m;
optName = kebabToCamelCase(optName);
let {name, hasArg} = optDesc.options[optName] || {};
if (!name) {
result.error = "Unknown option";
} else if (hasArg) {
if (!optVal) optVal = args[ix++]; // may access off end of array -- that's ok
if (!optVal) result.error = "Option missing value";
} else {
if (optVal) result.error = "Didn't expect value for flag argument";
optVal = true;
}
result.optionValues[name] = optVal;
result.values[name] = optVal;
} else if (allowOptions && arg.match(/^-./)) {
// Process short args
arg = arg.substring(1);
do {
flag = arg[0];
arg = arg.substring(1);
let {name, hasArg} = optDesc.options[flag] || {};
let optVal = null;
if (!name) {
result.error = "Unknown option";
} else if (hasArg) {
optVal = arg;
arg = '';
if (!optVal) optVal = args[ix++];
if (!optVal) result.error = "Option missing value";
} else {
optVal = true;
}
result.optionValues[name] = optVal;
result.values[name] = optVal;
} while (arg);
} else if (inRest) {
// Process as many of the rest args as we can
const start = ix - 1;
if (allowOptions) {
// Advance ix to first option-like arg
while (ix < args.length && !(args[ix].length >= 2 && args[ix][0] === '-')) ix++;
} else {
ix = args.length;
}
const restArgs = args.slice(start, ix);
result.apply = result.apply.concat(restArgs);
result.values[inRest] = result.values[inRest].concat(restArgs);
} else {
let {name, rest} = pos.shift() || {};
if (!name) result.error = "Too many arguments";
if (optDesc.commands) {
// switch to handling optDesc from command. Don't include the
// command name in the result. Nested groups re-enter here on
// the next positional, consuming one path segment per level.
const next = optDesc.commands[arg];
if (!next) {
// Unknown command: leave `command`/`commandPath` at the last group we
// did resolve, so usage describes *that* group (its sub-commands)
// rather than reprinting the top level with a stray prefix.
result.error = "Command not found";
} else {
result.command = next;
result.commandPath.push(arg);
optDesc = next.optDesc;
pos = optDesc.positional.concat();
}
continue;
}
if (rest) {
inRest = name;
result.apply.push(arg);
result.values[inRest] = [arg];
} else {
result.apply.push(arg);
result.values[name] = arg;
}
}
}
for (let {rest, required} of pos) {
if (required) {
result.error = "Missing required argument";
} else if (!rest) {
result.apply.push(undefined);
}
}
if (optDesc.optionParamIndex != null) {
result.apply.splice(optDesc.optionParamIndex, 0, result.optionValues);
}
return result;
}
function kebabToCamelCase(str) {
return str.replace(/-([a-z])/g, ([, letter]) => letter.toUpperCase());
}