-
Notifications
You must be signed in to change notification settings - Fork 1
/
createAliases.js
594 lines (531 loc) · 24.2 KB
/
createAliases.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
const packageJson = require('./package.json');
const exec = require('child-process-promise').exec;
const enableDockerExperimental = envToBool('ENABLE_DOCKER_EXPERIMENTAL', true);
const envVars = enableDockerExperimental ? 'DOCKER_CLI_EXPERIMENTAL=enabled' : '';
const binary = 'docker';
const binaryAbbrev = binary.charAt(0);
// "alias d" already taken https://github.com/robbyrussell/oh-my-zsh/blob/master/lib/directories.zsh
// A workaround would be to use upper when only using 'D' but stick with lower for all other aliases because its faster to type
// However "D" is rather unintuitive. So stick to "d" but allow for configuring.
const binaryAbbrevUpper = envToBool('BINARY_ABBREV_UPPER', false);
const binaryAbbrevStandalone = binaryAbbrevUpper ? binary.charAt(0).toUpperCase() : binary.charAt(0);
// All permutations, i.e. all parameters in all orders are way to many
// e.g. docker run -diPt
// dridPt dritdP dri ...
// Fist Simplification: Use alphabetical order without duplicates, e.g abc, ab, ac, bc; but not ba, cba, etct.
// This still results in about 50.000 abbrevs for shortParams only!
// So, limit number of params per command (recursion depth)
const numberOfMaxParamsPerAlias = process.env.NUMBER_OF_MAX_PARAMS_PER_ALIAS || 4;
// Use long params (more than one char), e.g. "--rm" or "--tls" up to this string length.
// For longer params no alias is created
const numberOfCharsOfLongParamsToUseAsAlias = process.env.NUMBER_OF_CHARS_OF_LONG_PARAMS_TO_USE_AS_ALIAS || 2;
// This contains a couple of commands that result in shorter abbreviations.
// Why? The algorithm creates compromises, e.g. stop vs. start results in dsto and dsta, no one get ds or dst
// These "predefineds" are pretty much opinionated, trying to create shorter abbrevs for commands that are frequently used
// Note that binary is added to abbrev an command automatically
// e.g. b : build -> db : docker build
let predefinedAbbrevCmds = {
a: 'app',
b: 'build',
br: 'builder',
bx: 'buildx',
c: 'container',
cf: 'config',
cm: 'commit',
co: 'compose',
cx: 'context',
ex: 'exec',
img: 'image',
imgs: 'images',
n: 'network',
l: 'pull',
lg: 'logs',
p: 'push',
ps: 'ps',
r: 'run',
s: 'swarm',
se: 'search',
st: 'stack',
sta: 'start',
svc: 'service',
t: 'tag',
};
const predefinedAbbrevCmdsByCommand = swapKeyValue(predefinedAbbrevCmds);
const ignoredCommands = [
];
// E.g.: rrm: 'run --rm'
let predefinedAbbrevParams = {
};
const longParamAbbrevs = {
entrypoint : 'ep'
};
// Create abbreviations for longParams containing hyphen (log-level -> ll)
// Note: this will result in about 23k aliases (numberOfMaxParamsPerAlias=4) or 10k (numberOfMaxParamsPerAlias=3)
// From less than 1k before :-o
const createAliasesForLongParamsWithHyphen = false;
// Docker implements some synonym commands, that lead to a huge number of almost redundant aliases
// As we're facing a huge number of aliases anyway we can reduce them drastically by ignoring them
// Note that the following container/image sub commands are deliberately not excluded (as they only exist as subcommand)
// - prune,
// - inspect (docker image inspect is more specific than docker inspect)
const filterLegacySubCommands = envToBool('FILTER_LEGACY_SUBCOMMANDS', false); // docker ps, docker rmi, etc
const filterLegacySubCommandReplacements = envToBool('FILTER_LEGACY_SUBCOMMANDS_REPLACEMENTS', true); // docker container ls, docker image rm, etc
const legacyCommandReplacements = {
'container attach': 'attach',
'container commit': 'commit',
'container cp': 'cp',
'container create': 'create',
'container diff': 'diff',
'container exec': 'exec',
'container export': 'export',
'container kill' : 'kill',
'container logs' : 'logs',
'container ls' : 'ps',
'container pause' : 'pause',
'container port' : 'port',
'container rename' : 'rename',
'container restart' : 'restart',
'container rm' : 'rm',
'container run' : 'run',
'container start' : 'start',
'container stats' : 'stats',
'container stop' : 'stop',
'container top' : 'top',
'container unpause' : 'unpause',
'container update' : 'update',
'container wait' : 'wait',
'image build' : 'build',
'image history' : 'history',
'image import' : 'import',
'image load' : 'load',
'image ls' : 'images',
'image pull' : 'pull',
'image push' : 'push',
'image rm' : 'rmi',
'image save' : 'save',
'image tag' : 'tag'
};
main();
function main() {
console.log(`# Created with docker-aliases ${packageJson.version}`);
let commands = {};
parseCommands('docker', undefined, commands)
.then(() => {
let aliases = createAbbrevs(commands);
printAliases(aliases)
})
.catch(err => {
console.error(err);
process.exit(1)
})
}
function createPredefinedAbbrevs(commands, abbrevs) {
const prepended = {};
Object.keys(predefinedAbbrevCmds).forEach(abbrev => {
addPredefinedAbbrev(predefinedAbbrevCmds, abbrev, prepended);
});
Object.keys(predefinedAbbrevParams).forEach(abbrev => {
addPredefinedAbbrev(predefinedAbbrevParams, abbrev, prepended);
});
Object.keys(prepended).forEach(predefinedAbbrev => {
let predefinedCommand = commands[prepended[predefinedAbbrev]];
if (!predefinedCommand) {
console.error(`Skipping predefined command, because not returned by binary: ${prepended[predefinedAbbrev]}`)
return
}
abbrevs[predefinedAbbrev] = predefinedCommand;
predefinedCommand.predefined = true;
predefinedCommand.abbrev = predefinedAbbrev;
});
}
function addPredefinedAbbrev(abbrevs, abbrev, prepended) {
let prependedAbbrev = `${binaryAbbrev}${abbrev}`;
let prependedCmdString = `${binary} ${abbrevs[abbrev]}`;
if (prepended[prependedAbbrev]) {
throw `Duplicate predefined abbrev: ${prependedAbbrev} - '${prepended[prependedAbbrev]}' and '${prependedCmdString}'`
}
prepended[prependedAbbrev] = prependedCmdString;
}
function parseCommands(command, parent, currentResult) {
const absoluteCommand = `${parent ? `${parent.cmdString} ${command}` : command}`;
return exec(`${envVars} ${absoluteCommand} --help`)
.then(execOut => {
let stdoutLines = execOut.stdout.split(/\r?\n/);
let nextSubCommands = findCommands(stdoutLines);
nextSubCommands = nextSubCommands.filter( subCommand => !ignoredCommands.includes(subCommand))
let commandObject = {cmd: command, parent: parent, cmdString: absoluteCommand};
commandObject.subcommands = nextSubCommands;
commandObject.params = findParams(stdoutLines);
currentResult[absoluteCommand] = commandObject;
if (nextSubCommands.length > 0) {
// Recurse into subcommands
let promises = [];
nextSubCommands.forEach(nextSubCommand => {
promises.push(parseCommands(nextSubCommand, commandObject, currentResult));
});
return Promise.all(promises);
} else {
// End recursion
return currentResult
}
})
}
function filterCommands(abbrevs) {
Object.keys(abbrevs).sort().forEach(abbrev => {
const abbrevObj = abbrevs[abbrev];
let cmdWithoutBinary = abbrevObj.cmdString.replace(new RegExp(`^${binary} `), '');
if ((filterLegacySubCommandReplacements && Object.keys(legacyCommandReplacements).includes(cmdWithoutBinary)) ||
(filterLegacySubCommands && Object.values(legacyCommandReplacements).includes(cmdWithoutBinary)) ) {
console.error(`Removing command, due to filtering options: ${abbrevObj.cmdString}`);
delete abbrevs[abbrev];
}
});
}
function createCommandAbbrevs(commands, abbrevs, conflicts) {
Object.keys(commands).sort().forEach(absoluteCommand => {
let commandObj = commands[absoluteCommand];
if (commandObj.predefined || abbrevs[commandObj.abbrev]) {
return
}
let currentSubCommand = commandObj.cmd;
let competingCommand;
// Use the same abbrevs also in nested commands, i.e. svc for docker services and docker stack services
let predefinedAbbrev = predefinedAbbrevCmdsByCommand[currentSubCommand];
if (predefinedAbbrev) {
commandObj.predefined = true;
const parentAbbrev = commandObj.parent ? commandObj.parent.abbrev : '';
let potentialAbbrev = `${parentAbbrev}${predefinedAbbrev}`
competingCommand = abbrevs[potentialAbbrev];
if (competingCommand) {
// Removed command gets a new abbrev on next loop
console.error(`Removing abbrev: ${potentialAbbrev} for cmd ${competingCommand.cmdString} in favor of predefined ${commandObj.cmdString}`);
removeAbbrev(abbrevs, competingCommand, commands);
}
setAbbrev(abbrevs, potentialAbbrev, commandObj);
return;
}
for (let i = 0; i < currentSubCommand.length + 1; i++) {
// Run to length+1 for sanity checking
if (i === currentSubCommand.length) {
throw `No matching abbreviation found for command: ${absoluteCommand}`
}
const parentAbbrev = commandObj.parent ? commandObj.parent.abbrev : '';
let potentialAbbrev = `${parentAbbrev}${currentSubCommand.substring(0, i + 1)}`;
if (!competingCommand) {
competingCommand = abbrevs[potentialAbbrev];
if (!competingCommand) {
if ((!conflicts.includes(potentialAbbrev) &&
// Abbrev already taken (e.g. predefined)
!abbrevs[potentialAbbrev] ) ||
// Last char of this command. Pick it even though there are conflicts.
// Example: "builds" & "builder" are processed. Then "build" is processed.
i === currentSubCommand.length - 1) {
setAbbrev(abbrevs, potentialAbbrev, commandObj);
break
}
} else {
if (!competingCommand.predefined) {
if (i === currentSubCommand.length - 1) {
// Last char of this command. It has to get this abbrev or the next iteration
// will result in "No matching abbreviation found".
// Just removing to competingCommand is not possible because the competing command
// is already processed and would lose its abrrev
// So: Command gets abbrev and start the loop again, so competing command
console.error(`Removing abbrev: ${potentialAbbrev} for cmd ${competingCommand.cmdString} in favor of ${commandObj.cmdString}`);
removeAbbrev(abbrevs, competingCommand, commands);
setAbbrev(abbrevs, potentialAbbrev, commandObj);
return
} else {
conflicts.push(potentialAbbrev);
delete abbrevs[potentialAbbrev];
delete commandObj.abbrev
}
} else {
competingCommand = undefined
}
}
} else {
if (competingCommand.cmdString.charAt(i)) {
const competingParentAbbrev = competingCommand.parent ? competingCommand.parent.abbrev : '';
const competingAbbrev = `${competingParentAbbrev}${competingCommand.cmd.substring(0, i + 1)}`;
if (competingAbbrev === potentialAbbrev) {
// Conflict persists
conflicts.push(potentialAbbrev);
} else {
if (!conflicts.includes(potentialAbbrev) &&
// Abbrev already taken (e.g. predefined)
!abbrevs[potentialAbbrev]) {
// We have found a compromise
setAbbrev(abbrevs, potentialAbbrev, commandObj);
if (!abbrevs[competingAbbrev]) {
updateAbbrev(abbrevs, competingAbbrev, competingCommand, commands);
} // else find abbrev with next pass
break
}
}
} else {
// competing command is shorter, it gets the shorter abbrev
let shorterAbbrev = potentialAbbrev.substring(0, i);
// Skip removing the conflict, it doesnt matter
setAbbrev(abbrevs, potentialAbbrev, commandObj);
updateAbbrev(abbrevs, shorterAbbrev, competingCommand, commands);
break;
}
}
}
});
}
function createAbbrevs(commands) {
const abbrevs = {};
const conflicts = [];
createPredefinedAbbrevs(commands, abbrevs)
// Use a multi pass creation here.
// Why? An abbreviation might be changed in a later stage, e.g. because of predefinedAbbrevCmds
// Then just unset the command that had the abbreviation and start again, so the "unset command" is processed again
// and gets a new abbreviation.
// If running in an endless loop here, find out which is the missing command like so:
//Object.keys(commands).filter( x => ! Object.values(abbrevs).map( abbrev => abbrev.cmdString).includes(x))
while (Object.keys(abbrevs).length < Object.keys(commands).length) {
createCommandAbbrevs(commands, abbrevs, conflicts);
}
filterCommands(abbrevs);
addParamAbbrevs(abbrevs);
changeBinaryAbbrevStandalone(abbrevs);
// Sorting by cmd instead of abbrev make comparing alias results easier after changes
return sortByCmdStringToArray(abbrevs)
}
function changeBinaryAbbrevStandalone(abbrevs) {
if (binaryAbbrev !== binaryAbbrevStandalone) {
abbrevs[binaryAbbrevStandalone] = abbrevs[binaryAbbrev];
delete abbrevs[binaryAbbrev];
abbrevs[binaryAbbrevStandalone].abbrev = binaryAbbrevStandalone;
}
}
function addParamAbbrevs(abbrevs) {
// Add params after the commands' abbrevs have been created.
// That is, commands' aliases take precedence.
let potentialParamAbbrevs = [];
Object.keys(abbrevs).sort().forEach(abbrev => {
const abbrevObj = abbrevs[abbrev];
createPotentialParamAbbrevs(abbrevObj, abbrevObj.params, potentialParamAbbrevs)
});
addValidParamAbbrevs(abbrevs, potentialParamAbbrevs);
}
function addLongParamAbbrev(param) {
if (longParamAbbrevs[param.longParam]) {
param.longParamAbbrev = longParamAbbrevs[param.longParam];
} else if (createAliasesForLongParamsWithHyphen && param.longParam.includes('-')) {
param.longParamAbbrev = param.longParam
.split('-')
.map( substring => substring[0])
.join('')
}
}
function createPotentialParamAbbrevs(cmd, params, paramAbbrevs = [], previousParams = []) {
if (params && previousParams.length <= numberOfMaxParamsPerAlias - 1) {
params.forEach(param => {
addLongParamAbbrev(param);
if (param.shortParam || param.longParamAbbrev ||
param.longParam.length <= numberOfCharsOfLongParamsToUseAsAlias) {
paramAbbrevs.push({cmd: cmd, params: previousParams.concat(param)});
// Recurse into all other parameters, that follow alphabetically after this one
let allParamsAfterThisOne = cmd.params.slice(cmd.params.indexOf(param) + 1);
createPotentialParamAbbrevs(cmd, allParamsAfterThisOne, paramAbbrevs, previousParams.concat(param))
}
});
}
}
function addValidParamAbbrevs(abbrevs, potentialParamAbbrev) {
potentialParamAbbrev.forEach(paramToAbbrev => {
const nonBooleanParams = paramToAbbrev.params.filter(param => param.arg);
if (nonBooleanParams.length > 1) {
// This combination of param is invalid for an alias, as there are is more than on param requiring an arg
return
}
const nonBooleanParam = nonBooleanParams.length > 0 ? nonBooleanParams[0] : undefined;
const longParamCmdString = createLongParamCmdString(paramToAbbrev, nonBooleanParam);
const longParamsAbbrev = filterBooleanParamsToString(paramToAbbrev, nonBooleanParam,'');
const shortParamCmdString = createShortParamCmdString(paramToAbbrev, nonBooleanParam);
const shortParamsAbbrev = filterBooleanParamsToString(paramToAbbrev, nonBooleanParam, '', true);
let nonBooleanCmdString = createNonBooleanCmdString(nonBooleanParam, shortParamCmdString);
const nonBooleanParamString = nonBooleanParamToString(nonBooleanParam);
let paramAbbrev = `${paramToAbbrev.cmd.abbrev}${longParamsAbbrev}${shortParamsAbbrev}${nonBooleanParamString}`;
let paramCmdString = `${paramToAbbrev.cmd.cmdString}${longParamCmdString}${shortParamCmdString}${nonBooleanCmdString}`;
if (abbrevs[paramAbbrev]) {
// TODO how to handle those?
console.error(`Parameter results in duplicate abbrev - ignoring: alias ${paramAbbrev}='${paramCmdString}' - conflicts with alias ${abbrevs[paramAbbrev].abbrev}='${abbrevs[paramAbbrev].cmdString}'`)
} else {
abbrevs[paramAbbrev] = {parent: paramToAbbrev.cmd, abbrev: paramAbbrev, cmdString: paramCmdString};
}
});
}
function filterBooleanParamsToString(paramToAbbrev, nonBooleanParam, joinBy, isShort = false) {
return paramToAbbrev.params
.filter(param => param !== nonBooleanParam && (isShort ? param.shortParam : !param.shortParam))
.map(param => findParamProperty(param))
.join(joinBy);
}
function findParamProperty(param) {
if (param.shortParam) {
return param.shortParam
} else if (param.longParamAbbrev) {
return param.longParamAbbrev
} else {
return param.longParam;
}
}
function createLongParamCmdString(paramToAbbrev, nonBooleanParam) {
let longParamsCmdString = filterBooleanParamsToString(paramToAbbrev, nonBooleanParam, ' --');
if (longParamsCmdString) {
longParamsCmdString = ` --${longParamsCmdString}`
}
return longParamsCmdString;
}
function createShortParamCmdString(paramToAbbrev, nonBooleanParam) {
let shortParamsCmdString = filterBooleanParamsToString(paramToAbbrev, nonBooleanParam, '', true);
if (shortParamsCmdString) {
shortParamsCmdString = ` -${shortParamsCmdString}`
}
return shortParamsCmdString;
}
function nonBooleanParamToString(nonBooleanParam) {
let nonBooleanParamString = '';
if (nonBooleanParam) {
nonBooleanParamString = findParamProperty(nonBooleanParam)
}
return nonBooleanParamString;
}
function createNonBooleanCmdString(nonBooleanParam, shortParamCmdString) {
let ret = '';
if (nonBooleanParam) {
if (nonBooleanParam.shortParam) {
if (!shortParamCmdString) {
ret += ' -'
}
ret += nonBooleanParam.shortParam;
} else {
ret = ` --${nonBooleanParam.longParam}`;
}
}
return ret;
}
function updateAbbrev(abbrevs, abbrev, commandObj, commands) {
commandObj.subcommands.forEach(subCommand => {
const subCommandObj = commands[`${commandObj.cmdString} ${subCommand}`];
if (!subCommandObj.abbrev) {
console.error(`Subcommand does not have abbrev while updating: ${subCommandObj.cmdString}`)
} else {
delete abbrevs[subCommandObj.abbrev];
const newSubCommandAbbrev = subCommandObj.abbrev.replace(new RegExp(`^${commandObj.abbrev}`), abbrev);
updateAbbrev(abbrevs, newSubCommandAbbrev, subCommandObj, commands)
}
});
abbrevs[abbrev] = commandObj;
commandObj.abbrev = abbrev;
}
function removeAbbrev(abbrevs, commandObj, commands) {
commandObj.subcommands.forEach(subCommand => {
const subCommandObj = commands[`${commandObj.cmdString} ${subCommand}`];
delete abbrevs[subCommandObj.abbrev];
delete subCommandObj.abbrev
removeAbbrev(abbrevs, subCommandObj, commands)
});
delete abbrevs[commandObj.abbrev];
delete commandObj.abbrev;
}
function setAbbrev(abbrevs, abbrev, commandObj) {
if (abbrevs[abbrev]) {
console.error(`Duplicates for abbrev ${abbrev}! ${abbrevs[abbrev].cmdString} & ${commandObj.cmdString}. Setting ${abbrev}=${commandObj.cmdString}`)
}
commandObj.abbrev = abbrev;
abbrevs[abbrev] = commandObj;
}
function swapKeyValue(json) {
let ret = {};
for (let key in json) {
ret[json[key]] = key;
}
return ret;
}
function sortByCmdStringToArray(abbrevs) {
return Object.values(abbrevs).sort((a, b) => {
if (a.cmdString < b.cmdString) {
return -1;
}
if (a.cmdString > b.cmdString) {
return 1;
}
return 0;
});
}
function findCommands(stdoutLines) {
let commandLines = [];
let afterCommandsLine = false;
stdoutLines.forEach(stdOutLine => {
if (afterCommandsLine &&
// Get rid of empty lines
stdOutLine &&
// Commands and params are indented, get rid of all other texts
stdOutLine.startsWith(' ')) {
commandLines.push(stdOutLine)
} else if (stdOutLine.startsWith('Commands:') ||
stdOutLine.startsWith('Available Commands:') ||
stdOutLine.startsWith('Management Commands:')) {
afterCommandsLine = true
} else if (stdOutLine.startsWith('Flags:')) {
// In docker 20 docker context is different: "Flags" are bellow "Available Commands"
afterCommandsLine = false
}
});
return commandLines.map(subCommand => subCommand.replace(/ *(\w-*)\** .*/, "$1").trim());
}
function findParams(stdoutLines) {
// exec() seems to be done width a line with of 80 chars (columns), so the docker .. --help commands get a lot more line breaks than neccsary.
// e.g. "docker service update --help" returns
// "--max-concurrent uint Number of job tasks to run concurrently (default equal to --replicas)"
// which is returned as three lines resulting in "--replicas" being interpreted as new parameter.
// So: Ignore when starting with "a lot" of spaces. Arbitrarily pick 20 :/
// It would be more robust to make exec() use more columns, but there's no obvious way to do this...
let params = stdoutLines.filter(stdoutLine => /^ {1,20}-{1,2}\w*/.test(stdoutLine));
let paramObjs = params.map(param => {
// Third group and space in front optional, because of
// "docker app image rm" -> " -f, --force"
const matchesShortParam = /-(\w), --([\w-]*) ?(\w*)?.*/.exec(param);
if (matchesShortParam === null) {
const matchesLongParam = /--([\w-]*) (\w*).*/.exec(param);
if (matchesLongParam === null) {
throw `Param parsing failed for param: ${param}`;
}
return {longParam: matchesLongParam[1], arg: matchesLongParam[2]}
} else {
return {shortParam: matchesShortParam[1], longParam: matchesShortParam[2], arg: matchesShortParam[3]}
}
});
// Sort alphabetically to get defined order
return paramObjs.sort((a, b) => {
if (a.longParam < b.longParam) {
return -1;
}
if (a.longParam > b.longParam) {
return 1;
}
return 0;
})
}
function printAliases(commandAliases) {
let nAliases = 0;
commandAliases.forEach(cmd => {
console.log(`alias ${cmd.abbrev}='${cmd.cmdString}'`);
nAliases++;
});
// Print to stderr in order to allow for piping stdout to aliases file
console.error(`Created ${nAliases} aliases.`)
}
function envToBool(envVar, defaultValue) {
let envVarVal = process.env[envVar];
if (!envVarVal) {
return defaultValue
} else {
return (envVarVal === 'true');
}
}