From 21e062fae75729c467afdd0ce5c8f0e5683fb2ba Mon Sep 17 00:00:00 2001 From: Andre Cipriani Bandarra Date: Mon, 6 Apr 2026 16:49:34 +0100 Subject: [PATCH] Use clap for command-line arguments in pull example --- Cargo.toml | 1 + examples/pull.rs | 28 ++++++++++++++++++++++------ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4797ab0..8efdcb3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ futures-util = "0.3" async-stream = "0.3" [dev-dependencies] +clap = { version = "4.5", features = ["derive", "env"] } dialoguer = "0.12.0" dotenvy = "0.15.7" tokio = { version = "1.49.0", features = ["full"] } diff --git a/examples/pull.rs b/examples/pull.rs index 89a9c1e..8f2a871 100644 --- a/examples/pull.rs +++ b/examples/pull.rs @@ -1,21 +1,37 @@ -use std::{env, error::Error, io::Write}; +use std::{error::Error, io::Write}; +use clap::Parser; use futures_util::StreamExt; use ollama_rs::{OllamaClient, types::pull::PullRequest}; -const MODEL: &str = "functiongemma"; +#[derive(Parser, Debug)] +#[command(author, version, about, long_about = None)] +struct Args { + /// The model to pull + #[arg(short, long)] + model: String, + + /// The Ollama server address + #[arg( + short, + long, + env = "OLLAMA_SERVER", + default_value = "http://localhost:11434" + )] + server_address: String, +} #[tokio::main] async fn main() -> Result<(), Box> { let _ = dotenvy::dotenv(); - let server_address = env::var("OLLAMA_SERVER")?; - let ollama_client = OllamaClient::new(server_address); + let args = Args::parse(); + let ollama_client = OllamaClient::new(args.server_address); - let request = PullRequest::builder(MODEL).stream(true).build(); + let request = PullRequest::builder(&args.model).stream(true).build(); let mut stream = ollama_client.pull(request); while let Some(response) = stream.next().await { let response = response?; - println!("{:?}", response); + println!("{}", response.status); std::io::stdout().flush()?; } Ok(())