* 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)
| 64 | * @returns {string[]} the name of the detected flags |
| 65 | */ |
| 66 | function 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 | } |