Skip to main content

QueryCommand

Struct QueryCommand 

Source
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

Source

pub fn new(prompt: impl Into<String>) -> Self

Create a new query command with the given prompt.

Source

pub fn model(self, model: impl Into<String>) -> Self

Set the model to use (e.g. “sonnet”, “opus”, or a full model ID).

Source

pub fn system_prompt(self, prompt: impl Into<String>) -> Self

Set a custom system prompt (replaces the default).

Source

pub fn append_system_prompt(self, prompt: impl Into<String>) -> Self

Append to the default system prompt.

Source

pub fn output_format(self, format: OutputFormat) -> Self

Set the output format.

Source

pub fn max_budget_usd(self, budget: f64) -> Self

Set the maximum budget in USD.

Source

pub fn permission_mode(self, mode: PermissionMode) -> Self

Set the permission mode.

Source

pub fn allowed_tools( self, tools: impl IntoIterator<Item = impl Into<String>>, ) -> Self

Add allowed tools (e.g. “Bash”, “Read”, “mcp__my-server__*”).

Source

pub fn allowed_tool(self, tool: impl Into<String>) -> Self

Add a single allowed tool.

Source

pub fn disallowed_tools( self, tools: impl IntoIterator<Item = impl Into<String>>, ) -> Self

Add disallowed tools.

Source

pub fn mcp_config(self, path: impl Into<String>) -> Self

Add an MCP config file path.

Source

pub fn add_dir(self, dir: impl Into<String>) -> Self

Add an additional directory for tool access.

Source

pub fn effort(self, effort: Effort) -> Self

Set the effort level.

Source

pub fn max_turns(self, turns: u32) -> Self

Set the maximum number of turns.

Source

pub fn json_schema(self, schema: impl Into<String>) -> Self

Set a JSON schema for structured output validation.

Source

pub fn continue_session(self) -> Self

Continue the most recent conversation.

Source

pub fn resume(self, session_id: impl Into<String>) -> Self

Resume a specific session by ID.

Source

pub fn session_id(self, id: impl Into<String>) -> Self

Use a specific session ID.

Source

pub fn fallback_model(self, model: impl Into<String>) -> Self

Set a fallback model for when the primary model is overloaded.

Source

pub fn no_session_persistence(self) -> Self

Disable session persistence (sessions won’t be saved to disk).

Source

pub fn dangerously_skip_permissions(self) -> Self

Bypass all permission checks. Only use in sandboxed environments.

Source

pub fn agent(self, agent: impl Into<String>) -> Self

Set the agent for the session.

Source

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"}}

Source

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.

Source

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).

Source

pub fn include_partial_messages(self) -> Self

Include partial message chunks as they arrive.

Only works with --output-format stream-json.

Source

pub fn input_format(self, format: InputFormat) -> Self

Set the input format.

Source

pub fn strict_mcp_config(self) -> Self

Only use MCP servers from --mcp-config, ignoring all other MCP configurations.

Source

pub fn settings(self, settings: impl Into<String>) -> Self

Path to a settings JSON file or a JSON string.

Source

pub fn fork_session(self) -> Self

When resuming, create a new session ID instead of reusing the original.

Source

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?;
Source

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);
Source

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.

Trait Implementations§

Source§

impl ClaudeCommand for QueryCommand

Source§

type Output = CommandOutput

The typed result of executing this command.
Source§

fn args(&self) -> Vec<String>

Build the CLI argument list for this command.
Source§

async fn execute(&self, claude: &Claude) -> Result<CommandOutput>

Execute the command using the given claude client.
Source§

impl Clone for QueryCommand

Source§

fn clone(&self) -> QueryCommand

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for QueryCommand

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more