pub struct QueryCommand { /* private fields */ }Expand description
Builder for claude -p <prompt> (oneshot print-mode queries).
This is the primary command for programmatic use. It runs a single prompt through Claude and returns the result.
§Example
use claude_wrapper::{Claude, ClaudeCommand, QueryCommand, OutputFormat};
let claude = Claude::builder().build()?;
let output = QueryCommand::new("explain this error: file not found")
.model("sonnet")
.output_format(OutputFormat::Json)
.max_turns(1)
.execute(&claude)
.await?;Implementations§
Source§impl QueryCommand
impl QueryCommand
Sourcepub fn new(prompt: impl Into<String>) -> Self
pub fn new(prompt: impl Into<String>) -> Self
Create a new query command with the given prompt.
Sourcepub fn model(self, model: impl Into<String>) -> Self
pub fn model(self, model: impl Into<String>) -> Self
Set the model to use (e.g. “sonnet”, “opus”, or a full model ID).
Sourcepub fn system_prompt(self, prompt: impl Into<String>) -> Self
pub fn system_prompt(self, prompt: impl Into<String>) -> Self
Set a custom system prompt (replaces the default).
Sourcepub fn append_system_prompt(self, prompt: impl Into<String>) -> Self
pub fn append_system_prompt(self, prompt: impl Into<String>) -> Self
Append to the default system prompt.
Sourcepub fn output_format(self, format: OutputFormat) -> Self
pub fn output_format(self, format: OutputFormat) -> Self
Set the output format.
Sourcepub fn max_budget_usd(self, budget: f64) -> Self
pub fn max_budget_usd(self, budget: f64) -> Self
Set the maximum budget in USD.
Sourcepub fn permission_mode(self, mode: PermissionMode) -> Self
pub fn permission_mode(self, mode: PermissionMode) -> Self
Set the permission mode.
Sourcepub fn allowed_tools<I, T>(self, tools: I) -> Self
pub fn allowed_tools<I, T>(self, tools: I) -> Self
Add allowed tool patterns.
Accepts anything convertible into ToolPattern, including
bare strings (e.g. "Bash", "Bash(git log:*)",
"mcp__my-server__*") and values produced by
ToolPattern’s constructors.
use claude_wrapper::{QueryCommand, ToolPattern};
let cmd = QueryCommand::new("hi")
.allowed_tools(["Bash", "Read"]) // raw strings still work
.allowed_tool(ToolPattern::tool_with_args("Bash", "git log:*"))
.allowed_tool(ToolPattern::all("Write"));Sourcepub fn allowed_tool(self, tool: impl Into<ToolPattern>) -> Self
pub fn allowed_tool(self, tool: impl Into<ToolPattern>) -> Self
Add a single allowed tool pattern.
Sourcepub fn disallowed_tools<I, T>(self, tools: I) -> Self
pub fn disallowed_tools<I, T>(self, tools: I) -> Self
Add disallowed tool patterns.
Sourcepub fn disallowed_tool(self, tool: impl Into<ToolPattern>) -> Self
pub fn disallowed_tool(self, tool: impl Into<ToolPattern>) -> Self
Add a single disallowed tool pattern.
Sourcepub fn mcp_config(self, path: impl Into<String>) -> Self
pub fn mcp_config(self, path: impl Into<String>) -> Self
Add an MCP config file path.
Sourcepub fn add_dir(self, dir: impl Into<String>) -> Self
pub fn add_dir(self, dir: impl Into<String>) -> Self
Add an additional directory for tool access.
Sourcepub fn json_schema(self, schema: impl Into<String>) -> Self
pub fn json_schema(self, schema: impl Into<String>) -> Self
Set a JSON schema for structured output validation.
Sourcepub fn continue_session(self) -> Self
pub fn continue_session(self) -> Self
Continue the most recent conversation.
Sourcepub fn session_id(self, id: impl Into<String>) -> Self
pub fn session_id(self, id: impl Into<String>) -> Self
Use a specific session ID.
Sourcepub fn fallback_model(self, model: impl Into<String>) -> Self
pub fn fallback_model(self, model: impl Into<String>) -> Self
Set a fallback model for when the primary model is overloaded.
Sourcepub fn no_session_persistence(self) -> Self
pub fn no_session_persistence(self) -> Self
Disable session persistence (sessions won’t be saved to disk).
Sourcepub fn dangerously_skip_permissions(self) -> Self
pub fn dangerously_skip_permissions(self) -> Self
Bypass all permission checks. Only use in sandboxed environments.
Sourcepub fn agents_json(self, json: impl Into<String>) -> Self
pub fn agents_json(self, json: impl Into<String>) -> Self
Set custom agents as a JSON object.
Example: {"reviewer": {"description": "Reviews code", "prompt": "You are a code reviewer"}}
Sourcepub fn tools(self, tools: impl IntoIterator<Item = impl Into<String>>) -> Self
pub fn tools(self, tools: impl IntoIterator<Item = impl Into<String>>) -> Self
Set the list of available built-in tools.
Use "" to disable all tools, "default" for all tools, or
specific tool names like ["Bash", "Edit", "Read"].
This is different from allowed_tools which controls MCP tool permissions.
Sourcepub fn file(self, spec: impl Into<String>) -> Self
pub fn file(self, spec: impl Into<String>) -> Self
Add a file resource to download at startup.
Format: file_id:relative_path (e.g. file_abc:doc.txt).
Sourcepub fn include_partial_messages(self) -> Self
pub fn include_partial_messages(self) -> Self
Include partial message chunks as they arrive.
Only works with --output-format stream-json.
Sourcepub fn input_format(self, format: InputFormat) -> Self
pub fn input_format(self, format: InputFormat) -> Self
Set the input format.
Sourcepub fn strict_mcp_config(self) -> Self
pub fn strict_mcp_config(self) -> Self
Only use MCP servers from --mcp-config, ignoring all other MCP configurations.
Sourcepub fn settings(self, settings: impl Into<String>) -> Self
pub fn settings(self, settings: impl Into<String>) -> Self
Path to a settings JSON file or a JSON string.
Sourcepub fn fork_session(self) -> Self
pub fn fork_session(self) -> Self
When resuming, create a new session ID instead of reusing the original.
Sourcepub fn worktree(self) -> Self
pub fn worktree(self) -> Self
Create a new git worktree for this session, providing an isolated working directory.
Sourcepub fn brief(self) -> Self
pub fn brief(self) -> Self
Enable brief mode, which activates the SendUserMessage tool for agent-to-user communication.
Sourcepub fn debug_filter(self, filter: impl Into<String>) -> Self
pub fn debug_filter(self, filter: impl Into<String>) -> Self
Enable debug logging with an optional filter (e.g., “api,hooks”).
Sourcepub fn debug_file(self, path: impl Into<String>) -> Self
pub fn debug_file(self, path: impl Into<String>) -> Self
Write debug logs to the specified file path.
Sourcepub fn betas(self, betas: impl Into<String>) -> Self
pub fn betas(self, betas: impl Into<String>) -> Self
Beta feature headers for API key authentication.
Sourcepub fn plugin_dir(self, dir: impl Into<String>) -> Self
pub fn plugin_dir(self, dir: impl Into<String>) -> Self
Load plugins from the specified directory for this session.
Sourcepub fn setting_sources(self, sources: impl Into<String>) -> Self
pub fn setting_sources(self, sources: impl Into<String>) -> Self
Comma-separated list of setting sources to load (e.g., “user,project,local”).
Sourcepub fn bare(self) -> Self
pub fn bare(self) -> Self
Run in minimal mode (--bare).
Skips hooks, LSP, plugin sync, attribution, auto-memory,
background prefetches, keychain reads, and CLAUDE.md
auto-discovery. Sets CLAUDE_CODE_SIMPLE=1 inside the child.
Anthropic auth is restricted to ANTHROPIC_API_KEY or
apiKeyHelper via --settings; OAuth and keychain are never
read. Third-party providers (Bedrock/Vertex/Foundry) use their
own credentials as normal.
Intended for headless/CI use where you want deterministic
context: provide everything explicitly via --system-prompt,
--append-system-prompt, --add-dir, --mcp-config,
--settings, --agents, and --plugin-dir. Skills still
resolve via explicit /skill-name references.
Sourcepub fn disable_slash_commands(self) -> Self
pub fn disable_slash_commands(self) -> Self
Disable all slash-command skills (--disable-slash-commands).
Sourcepub fn include_hook_events(self) -> Self
pub fn include_hook_events(self) -> Self
Include every hook lifecycle event in the stream-json output
(--include-hook-events). Only meaningful with
OutputFormat::StreamJson.
Sourcepub fn exclude_dynamic_system_prompt_sections(self) -> Self
pub fn exclude_dynamic_system_prompt_sections(self) -> Self
Move per-machine sections (cwd, env info, memory paths, git
status) out of the system prompt and into the first user
message (--exclude-dynamic-system-prompt-sections). Improves
cross-user prompt-cache reuse. Only applies with the default
system prompt; ignored with --system-prompt.
Sourcepub fn name(self, name: impl Into<String>) -> Self
pub fn name(self, name: impl Into<String>) -> Self
Set a display name for this session (--name). Shown in the
prompt box, /resume picker, and terminal title.
Sourcepub fn from_pr(self, pr: impl Into<String>) -> Self
pub fn from_pr(self, pr: impl Into<String>) -> Self
Resume a session linked to a PR by number or URL
(--from-pr <value>).
This wrapper only supports the valued form; the CLI’s no-value mode opens an interactive picker and would hang a headless caller.
Sourcepub fn retry(self, policy: RetryPolicy) -> Self
pub fn retry(self, policy: RetryPolicy) -> Self
Set a per-command retry policy, overriding the client default.
§Example
use claude_wrapper::{Claude, ClaudeCommand, QueryCommand, RetryPolicy};
use std::time::Duration;
let claude = Claude::builder().build()?;
let output = QueryCommand::new("explain quicksort")
.retry(RetryPolicy::new()
.max_attempts(5)
.initial_backoff(Duration::from_secs(2))
.exponential()
.retry_on_timeout(true))
.execute(&claude)
.await?;Sourcepub fn to_command_string(&self, claude: &Claude) -> String
pub fn to_command_string(&self, claude: &Claude) -> String
Return the full command as a string that could be run in a shell.
Constructs a command string using the binary path from the Claude instance and the arguments from this query. Arguments containing spaces or special shell characters are shell-quoted to be safe for shell execution.
§Example
use claude_wrapper::{Claude, QueryCommand};
let claude = Claude::builder().build()?;
let cmd = QueryCommand::new("explain quicksort")
.model("sonnet");
let command_str = cmd.to_command_string(&claude);
println!("Would run: {}", command_str);Sourcepub async fn execute_json(&self, claude: &Claude) -> Result<QueryResult>
pub async fn execute_json(&self, claude: &Claude) -> Result<QueryResult>
Execute the query and parse the JSON result.
This is a convenience method that sets OutputFormat::Json and
deserializes the response into a QueryResult.
Sourcepub fn execute_sync(&self, claude: &Claude) -> Result<CommandOutput>
pub fn execute_sync(&self, claude: &Claude) -> Result<CommandOutput>
Blocking analog of QueryCommand::execute that honours the
configured RetryPolicy.
Overrides the blanket
ClaudeCommandSyncExt::execute_sync
impl so retries still fire on the sync path.
Sourcepub fn execute_json_sync(&self, claude: &Claude) -> Result<QueryResult>
pub fn execute_json_sync(&self, claude: &Claude) -> Result<QueryResult>
Blocking mirror of QueryCommand::execute_json.
Trait Implementations§
Source§impl ClaudeCommand for QueryCommand
impl ClaudeCommand for QueryCommand
Source§impl Clone for QueryCommand
impl Clone for QueryCommand
Source§fn clone(&self) -> QueryCommand
fn clone(&self) -> QueryCommand
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for QueryCommand
impl RefUnwindSafe for QueryCommand
impl Send for QueryCommand
impl Sync for QueryCommand
impl Unpin for QueryCommand
impl UnsafeUnpin for QueryCommand
impl UnwindSafe for QueryCommand
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> ClaudeCommandSyncExt for Twhere
T: ClaudeCommand<Output = CommandOutput>,
impl<T> ClaudeCommandSyncExt for Twhere
T: ClaudeCommand<Output = CommandOutput>,
Source§fn execute_sync(&self, claude: &Claude) -> Result<CommandOutput, Error>
fn execute_sync(&self, claude: &Claude) -> Result<CommandOutput, Error>
ClaudeCommand::execute for commands
producing CommandOutput.