mirror of
https://github.com/threeal/cmake-action.git
synced 2026-02-02 03:50:55 +00:00
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>
This commit is contained in:
@@ -99,7 +99,7 @@ describe("get action context", () => {
|
||||
{
|
||||
name: "with additional options specified",
|
||||
inputs: {
|
||||
options: `BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON FOO="BAR BAZ"`,
|
||||
options: `BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON "FOO=BAR BAZ"`,
|
||||
},
|
||||
expectedContext: {
|
||||
configure: {
|
||||
@@ -150,7 +150,7 @@ describe("get action context", () => {
|
||||
"cxx-compiler": "clang++",
|
||||
"c-flags": "-Werror -Wall\n-Wextra",
|
||||
"cxx-flags": "-Werror -Wall\n-Wextra -Wpedantic",
|
||||
options: `BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON FOO="BAR BAZ"`,
|
||||
options: `BUILD_TESTING=ON BUILD_EXAMPLES=ON\nBUILD_DOCS=ON "FOO=BAR BAZ"`,
|
||||
args: `-Wdev -Wdeprecated\n--fresh --foo "bar baz"`,
|
||||
"run-build": "true",
|
||||
"build-args": `--target foo\n--parallel 8 --foo "bar baz"`,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { getInput } from "gha-utils";
|
||||
import path from "node:path";
|
||||
import { parse } from "shell-quote";
|
||||
import { parse } from "./utils.js";
|
||||
|
||||
export interface Context {
|
||||
sourceDir: string;
|
||||
|
||||
18
src/utils.ts
Normal file
18
src/utils.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user