MCPcopy Index your code
hub / github.com/nodejs/node / extractFlagNames

Function extractFlagNames

test/parallel/test-cli-node-cli-manpage-options.mjs:66–88  ·  view source on GitHub ↗

* Function that given a string containing backtick enclosed cli flags * separated by `, ` returns the name of flags present in the string * e.g. `extractFlagNames('`-x`, `--print "script"`')` === `['x', 'print']` * @param {string} str target string * @returns {string[]} the name of the detected

(str)

Source from the content-addressed store, hash-verified

64 * @returns {string[]} the name of the detected flags
65 */
66function extractFlagNames(str) {
67 const match = str.match(/`[^`]*?`/g);
68 if (!match) {
69 return [];
70 }
71 return match.map((flag) => {
72 // Remove the backticks, and leading dash from the flag
73 flag = flag.slice(2, -1);
74
75 // If the flag contains parameters make sure to remove those
76 const nameDelimiters = ['=', ' ', '['];
77 const nameCutOffIdx = Math.min(...nameDelimiters.map((d) => {
78 const idx = flag.indexOf(d);
79 if (idx > 0) {
80 return idx;
81 }
82 return flag.length;
83 }));
84 flag = flag.slice(0, nameCutOffIdx);
85
86 return flag;
87 });
88}

Calls 5

matchMethod · 0.65
mapMethod · 0.65
sliceMethod · 0.65
minMethod · 0.45
indexOfMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…