cmake-action/src/utils.ts
Alfi Maulana 8f79315420
feat: add function for parsing arguments with quotes (#532)
* feat: add `parse` function

Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>

* chore: remoev unused shell-quote from dependencies

Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>

* test: update `context.test.ts` test

Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>

---------

Signed-off-by: Alfi Maulana <alfi.maulana.f@gmail.com>
2024-11-26 18:43:30 +08:00

19 lines
609 B
TypeScript

const regex = /"([^"]*)"|'([^']*)'|`([^`]*)`|(\S+)/g;
/**
* Converts a space-separated string into a list of arguments.
*
* This function parses the provided string, which contains arguments separated by spaces and possibly enclosed in quotes, into a list of arguments.
*
* @param str - The space-separated string to parse.
* @returns A list of arguments.
*/
export function parse(str: string): string[] {
const args: string[] = [];
let match: RegExpExecArray | null;
while ((match = regex.exec(str)) !== null) {
args.push(match[1] ?? match[2] ?? match[3] ?? match[4]);
}
return args;
}