mirror of
https://github.com/anthropics/claude-code-action.git
synced 2026-01-22 22:44:13 +08:00
refactor: use industry-standard shell-quote for argument parsing
- Replace custom parseShellArgs with battle-tested shell-quote package - Simplify code by removing unnecessary -p filtering (Claude handles it) - Update tests to use shell-quote directly - Add example workflow showing claude_args usage This provides more robust argument parsing while reducing code complexity.
This commit is contained in:
@@ -1,68 +1,9 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { parse as parseShellArgs } from "shell-quote";
|
||||
|
||||
// Import the function directly from run-claude.ts for testing
|
||||
// We'll need to export it first
|
||||
function parseShellArgs(argsString?: string): string[] {
|
||||
if (!argsString || argsString.trim() === "") {
|
||||
return [];
|
||||
}
|
||||
|
||||
const args: string[] = [];
|
||||
let current = "";
|
||||
let inSingleQuote = false;
|
||||
let inDoubleQuote = false;
|
||||
let escapeNext = false;
|
||||
|
||||
for (let i = 0; i < argsString.length; i++) {
|
||||
const char = argsString[i];
|
||||
|
||||
if (escapeNext) {
|
||||
current += char;
|
||||
escapeNext = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === "\\") {
|
||||
if (inSingleQuote) {
|
||||
current += char;
|
||||
} else {
|
||||
escapeNext = true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === "'" && !inDoubleQuote) {
|
||||
inSingleQuote = !inSingleQuote;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === '"' && !inSingleQuote) {
|
||||
inDoubleQuote = !inDoubleQuote;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (char === " " && !inSingleQuote && !inDoubleQuote) {
|
||||
if (current) {
|
||||
args.push(current);
|
||||
current = "";
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
current += char;
|
||||
}
|
||||
|
||||
if (current) {
|
||||
args.push(current);
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
describe("parseShellArgs", () => {
|
||||
describe("shell-quote parseShellArgs", () => {
|
||||
test("should handle empty input", () => {
|
||||
expect(parseShellArgs("")).toEqual([]);
|
||||
expect(parseShellArgs(undefined)).toEqual([]);
|
||||
expect(parseShellArgs(" ")).toEqual([]);
|
||||
});
|
||||
|
||||
@@ -116,4 +57,11 @@ describe("parseShellArgs", () => {
|
||||
"You are helpful",
|
||||
]);
|
||||
});
|
||||
|
||||
test("should filter out non-string results", () => {
|
||||
// shell-quote can return objects for operators like | > < etc
|
||||
const result = parseShellArgs("echo hello");
|
||||
const filtered = result.filter(arg => typeof arg === "string");
|
||||
expect(filtered).toEqual(["echo", "hello"]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user