Skip to content

Commit 2ed25bf

Browse files
dicksontsaiclaude
authored andcommitted
Add extra_args field to ClaudeCodeOptions for forward compatibility (anthropics#111)
## Summary - Adds `extra_args` field to `ClaudeCodeOptions` to support passing arbitrary CLI flags - Enables forward compatibility with future CLI flags without requiring SDK updates - Supports both valued flags (`--flag value`) and boolean flags (`--flag`) ## Changes - Add `extra_args: dict[str, str | None]` field to `ClaudeCodeOptions` - Implement logic in `SubprocessCLITransport` to handle extra args: - `None` values create boolean flags (e.g., `{"verbose": None}` → `--verbose`) - String values create flags with arguments (e.g., `{"output": "json"}` → `--output json`) - Add comprehensive tests for the new functionality ## Test plan - [x] Added unit tests for settings file path handling - [x] Added unit tests for settings JSON object handling - [x] Added unit tests for extra_args with both valued and boolean flags - [x] All tests pass (`python -m pytest tests/`) - [x] Type checking passes (`python -m mypy src/`) 🤖 Generated with [Claude Code](https://claude.ai/code) Co-authored-by: Claude <noreply@anthropic.com> Signed-off-by: Rushil Patel <rpatel@codegen.com>
1 parent 8236c50 commit 2ed25bf

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

src/claude_code_sdk/_internal/transport/subprocess_cli.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,15 @@ def _build_command(self) -> list[str]:
144144
["--mcp-config", json.dumps({"mcpServers": self._options.mcp_servers})]
145145
)
146146

147+
# Add extra args for future CLI flags
148+
for flag, value in self._options.extra_args.items():
149+
if value is None:
150+
# Boolean flag without value
151+
cmd.append(f"--{flag}")
152+
else:
153+
# Flag with value
154+
cmd.extend([f"--{flag}", str(value)])
155+
147156
# Add prompt handling based on mode
148157
if self._is_streaming:
149158
# Streaming mode: use --input-format stream-json

src/claude_code_sdk/types.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,6 @@ class ClaudeCodeOptions:
128128
cwd: str | Path | None = None
129129
settings: str | None = None
130130
add_dirs: list[str | Path] = field(default_factory=list)
131+
extra_args: dict[str, str | None] = field(
132+
default_factory=dict
133+
) # Pass arbitrary CLI flags

tests/test_transport.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,56 @@ async def _test():
174174
assert "/this/directory/does/not/exist" in str(exc_info.value)
175175

176176
anyio.run(_test)
177+
178+
def test_build_command_with_settings_file(self):
179+
"""Test building CLI command with settings as file path."""
180+
transport = SubprocessCLITransport(
181+
prompt="test",
182+
options=ClaudeCodeOptions(settings="/path/to/settings.json"),
183+
cli_path="/usr/bin/claude",
184+
)
185+
186+
cmd = transport._build_command()
187+
assert "--settings" in cmd
188+
assert "/path/to/settings.json" in cmd
189+
190+
def test_build_command_with_settings_json(self):
191+
"""Test building CLI command with settings as JSON object."""
192+
settings_json = '{"permissions": {"allow": ["Bash(ls:*)"]}}'
193+
transport = SubprocessCLITransport(
194+
prompt="test",
195+
options=ClaudeCodeOptions(settings=settings_json),
196+
cli_path="/usr/bin/claude",
197+
)
198+
199+
cmd = transport._build_command()
200+
assert "--settings" in cmd
201+
assert settings_json in cmd
202+
203+
def test_build_command_with_extra_args(self):
204+
"""Test building CLI command with extra_args for future flags."""
205+
transport = SubprocessCLITransport(
206+
prompt="test",
207+
options=ClaudeCodeOptions(
208+
extra_args={
209+
"new-flag": "value",
210+
"boolean-flag": None,
211+
"another-option": "test-value",
212+
}
213+
),
214+
cli_path="/usr/bin/claude",
215+
)
216+
217+
cmd = transport._build_command()
218+
cmd_str = " ".join(cmd)
219+
220+
# Check flags with values
221+
assert "--new-flag value" in cmd_str
222+
assert "--another-option test-value" in cmd_str
223+
224+
# Check boolean flag (no value)
225+
assert "--boolean-flag" in cmd
226+
# Make sure boolean flag doesn't have a value after it
227+
boolean_idx = cmd.index("--boolean-flag")
228+
# Either it's the last element or the next element is another flag
229+
assert boolean_idx == len(cmd) - 1 or cmd[boolean_idx + 1].startswith("--")

0 commit comments

Comments
 (0)