fix: strip shell comment lines before parsing claude_args (#1055)
shell-quote treats # as a shell comment character, swallowing all subsequent content including flags on new lines. Strip comment lines (lines starting with #) before passing input to shell-quote. Fixes #802 Co-authored-by: VoidChecksum <Admin@CyberNord>
This commit is contained in:
parent
f328a5c889
commit
eb8baa46af
@ -79,6 +79,20 @@ function mergeMcpConfigs(configValues: string[]): string {
|
|||||||
return JSON.stringify(merged);
|
return JSON.stringify(merged);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Strip comment lines from a shell argument string.
|
||||||
|
* Lines whose first non-whitespace character is `#` are removed entirely.
|
||||||
|
* Inline `#` within a line (e.g. inside a quoted value) is left untouched
|
||||||
|
* because shell-quote handles quoting — we only need to remove full comment lines
|
||||||
|
* before shell-quote sees them.
|
||||||
|
*/
|
||||||
|
function stripShellComments(input: string): string {
|
||||||
|
return input
|
||||||
|
.split("\n")
|
||||||
|
.filter((line) => !line.trim().startsWith("#"))
|
||||||
|
.join("\n");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse claudeArgs string into extraArgs record for SDK pass-through
|
* Parse claudeArgs string into extraArgs record for SDK pass-through
|
||||||
* The SDK/CLI will handle --mcp-config, --json-schema, etc.
|
* The SDK/CLI will handle --mcp-config, --json-schema, etc.
|
||||||
@ -92,7 +106,7 @@ function parseClaudeArgsToExtraArgs(
|
|||||||
if (!claudeArgs?.trim()) return {};
|
if (!claudeArgs?.trim()) return {};
|
||||||
|
|
||||||
const result: Record<string, string | null> = {};
|
const result: Record<string, string | null> = {};
|
||||||
const args = parseShellArgs(claudeArgs).filter(
|
const args = parseShellArgs(stripShellComments(claudeArgs)).filter(
|
||||||
(arg): arg is string => typeof arg === "string",
|
(arg): arg is string => typeof arg === "string",
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@ -313,6 +313,42 @@ describe("parseSdkOptions", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("shell comment stripping", () => {
|
||||||
|
test("should parse flags before and after a comment line", () => {
|
||||||
|
const options: ClaudeOptions = {
|
||||||
|
claudeArgs: "--model 'claude-haiku'\n# comment\n--allowed-tools 'Edit'",
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = parseSdkOptions(options);
|
||||||
|
|
||||||
|
expect(result.sdkOptions.extraArgs?.["model"]).toBe("claude-haiku");
|
||||||
|
expect(result.sdkOptions.allowedTools).toEqual(["Edit"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("should parse flags correctly when no comments are present", () => {
|
||||||
|
const options: ClaudeOptions = {
|
||||||
|
claudeArgs: "--model 'claude-haiku'",
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = parseSdkOptions(options);
|
||||||
|
|
||||||
|
expect(result.sdkOptions.extraArgs?.["model"]).toBe("claude-haiku");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("should not strip inline # that appears inside a quoted value", () => {
|
||||||
|
const options: ClaudeOptions = {
|
||||||
|
claudeArgs: "--model 'claude-haiku' --prompt 'use color #ff0000'",
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = parseSdkOptions(options);
|
||||||
|
|
||||||
|
expect(result.sdkOptions.extraArgs?.["model"]).toBe("claude-haiku");
|
||||||
|
expect(result.sdkOptions.extraArgs?.["prompt"]).toBe(
|
||||||
|
"use color #ff0000",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("environment variables passthrough", () => {
|
describe("environment variables passthrough", () => {
|
||||||
test("should include OTEL environment variables in sdkOptions.env", () => {
|
test("should include OTEL environment variables in sdkOptions.env", () => {
|
||||||
// Set up test environment variables
|
// Set up test environment variables
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user