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:
km-anthropic
2025-08-07 15:18:00 -07:00
parent dfcaac854e
commit b6238ad00e
5 changed files with 65 additions and 133 deletions

View File

@@ -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"]);
});
});