1use std::ffi::OsString;
5use std::path::PathBuf;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
9pub enum Mode {
10 Text,
12 #[default]
14 Json,
15 Rpc,
17}
18
19impl Mode {
20 fn as_str(self) -> &'static str {
21 match self {
22 Mode::Text => "text",
23 Mode::Json => "json",
24 Mode::Rpc => "rpc",
25 }
26 }
27}
28
29#[derive(Debug, Clone, Default)]
40pub struct PiCliBuilder {
41 mode: Mode,
42 provider: Option<String>,
43 model: Option<String>,
44 api_key: Option<String>,
45 system_prompt: Option<String>,
46 session: Option<String>,
47 session_dir: Option<PathBuf>,
48 session_id: Option<String>,
49 fork: Option<String>,
50 continue_session: bool,
51 no_session: bool,
52 name: Option<String>,
53 no_tools: bool,
54 tools: Option<String>,
55 exclude_tools: Option<String>,
56 thinking: Option<String>,
57 print: bool,
58 approve: bool,
59 no_approve: bool,
60 offline: bool,
61 no_mcp: bool,
62 working_directory: Option<PathBuf>,
63 extra_args: Vec<OsString>,
64 prompt: Option<String>,
65}
66
67impl PiCliBuilder {
68 pub fn new() -> Self {
69 Self::default()
70 }
71
72 pub fn mode(mut self, mode: Mode) -> Self {
73 self.mode = mode;
74 self
75 }
76
77 pub fn provider(mut self, provider: impl Into<String>) -> Self {
81 self.provider = Some(provider.into());
82 self
83 }
84
85 pub fn model(mut self, model: impl Into<String>) -> Self {
91 self.model = Some(model.into());
92 self
93 }
94
95 pub fn api_key(mut self, key: impl Into<String>) -> Self {
98 self.api_key = Some(key.into());
99 self
100 }
101
102 pub fn system_prompt(mut self, prompt: impl Into<String>) -> Self {
103 self.system_prompt = Some(prompt.into());
104 self
105 }
106
107 pub fn session(mut self, session: impl Into<String>) -> Self {
109 self.session = Some(session.into());
110 self
111 }
112
113 pub fn session_dir(mut self, dir: impl Into<PathBuf>) -> Self {
114 self.session_dir = Some(dir.into());
115 self
116 }
117
118 pub fn session_id(mut self, id: impl Into<String>) -> Self {
120 self.session_id = Some(id.into());
121 self
122 }
123
124 pub fn fork(mut self, source: impl Into<String>) -> Self {
126 self.fork = Some(source.into());
127 self
128 }
129
130 pub fn continue_session(mut self, enabled: bool) -> Self {
132 self.continue_session = enabled;
133 self
134 }
135
136 pub fn no_session(mut self, enabled: bool) -> Self {
138 self.no_session = enabled;
139 self
140 }
141
142 pub fn name(mut self, name: impl Into<String>) -> Self {
144 self.name = Some(name.into());
145 self
146 }
147
148 pub fn no_tools(mut self, enabled: bool) -> Self {
150 self.no_tools = enabled;
151 self
152 }
153
154 pub fn tools(mut self, tools: impl Into<String>) -> Self {
162 self.tools = Some(tools.into());
163 self
164 }
165
166 pub fn exclude_tools(mut self, tools: impl Into<String>) -> Self {
169 self.exclude_tools = Some(tools.into());
170 self
171 }
172
173 pub fn thinking(mut self, level: impl Into<String>) -> Self {
175 self.thinking = Some(level.into());
176 self
177 }
178
179 pub fn print(mut self, enabled: bool) -> Self {
184 self.print = enabled;
185 self
186 }
187
188 pub fn approve(mut self, enabled: bool) -> Self {
193 self.approve = enabled;
194 self
195 }
196
197 pub fn no_approve(mut self, enabled: bool) -> Self {
199 self.no_approve = enabled;
200 self
201 }
202
203 pub fn offline(mut self, enabled: bool) -> Self {
205 self.offline = enabled;
206 self
207 }
208
209 pub fn no_mcp(mut self, enabled: bool) -> Self {
212 self.no_mcp = enabled;
213 self
214 }
215
216 pub fn working_directory(mut self, dir: impl Into<PathBuf>) -> Self {
220 self.working_directory = Some(dir.into());
221 self
222 }
223
224 pub fn get_working_directory(&self) -> Option<&std::path::Path> {
226 self.working_directory.as_deref()
227 }
228
229 pub fn extra_args<I, S>(mut self, args: I) -> Self
232 where
233 I: IntoIterator<Item = S>,
234 S: Into<OsString>,
235 {
236 self.extra_args.extend(args.into_iter().map(Into::into));
237 self
238 }
239
240 pub fn prompt(mut self, prompt: impl Into<String>) -> Self {
242 self.prompt = Some(prompt.into());
243 self
244 }
245
246 pub fn assembled_args(&self) -> Vec<OsString> {
248 let mut a: Vec<OsString> = vec!["--mode".into(), self.mode.as_str().into()];
249 let mut flag = |name: &str, v: &Option<String>| {
250 if let Some(v) = v {
251 a.push(name.into());
252 a.push(v.into());
253 }
254 };
255 flag("--provider", &self.provider);
256 flag("--model", &self.model);
257 flag("--api-key", &self.api_key);
258 flag("--system-prompt", &self.system_prompt);
259 flag("--session", &self.session);
260 flag("--session-id", &self.session_id);
261 flag("--fork", &self.fork);
262 flag("--name", &self.name);
263 flag("--tools", &self.tools);
264 flag("--exclude-tools", &self.exclude_tools);
265 flag("--thinking", &self.thinking);
266 if let Some(dir) = &self.session_dir {
267 a.push("--session-dir".into());
268 a.push(dir.into());
269 }
270 if self.continue_session {
271 a.push("--continue".into());
272 }
273 if self.no_session {
274 a.push("--no-session".into());
275 }
276 if self.no_tools {
277 a.push("--no-tools".into());
278 }
279 if self.print {
280 a.push("--print".into());
281 }
282 if self.approve {
283 a.push("--approve".into());
284 }
285 if self.no_approve {
286 a.push("--no-approve".into());
287 }
288 if self.offline {
289 a.push("--offline".into());
290 }
291 if self.no_mcp {
292 a.push("--no-mcp".into());
293 }
294 a.extend(self.extra_args.iter().cloned());
295 if let Some(p) = &self.prompt {
296 a.push(p.into());
297 }
298 a
299 }
300}
301
302#[cfg(test)]
303mod tests {
304 use super::*;
305
306 #[test]
307 fn assembled_args_order_flags_then_extras_then_prompt() {
308 let args = PiCliBuilder::new()
309 .mode(Mode::Json)
310 .provider("anthropic")
311 .no_session(true)
312 .extra_args(["--verbose"])
313 .prompt("hello")
314 .assembled_args();
315 let s: Vec<String> = args
316 .iter()
317 .map(|a| a.to_string_lossy().into_owned())
318 .collect();
319 assert_eq!(s[0..2], ["--mode", "json"]);
320 assert!(s.contains(&"--no-session".to_string()));
321 let vi = s.iter().position(|x| x == "--verbose").unwrap();
322 assert_eq!(s.last().unwrap(), "hello");
323 assert!(vi < s.len() - 1, "extras precede the positional prompt");
324 }
325
326 #[test]
327 fn rpc_mode_default_has_no_prompt() {
328 let args = PiCliBuilder::new().mode(Mode::Rpc).assembled_args();
329 assert_eq!(args.len(), 2);
330 assert_eq!(args[1], "rpc");
331 }
332
333 #[test]
334 fn no_mcp_flag() {
335 let args = PiCliBuilder::new().no_mcp(true).assembled_args();
336 assert_eq!(args.last().unwrap(), "--no-mcp");
337 let args = PiCliBuilder::new().no_mcp(false).assembled_args();
338 assert!(!args.iter().any(|a| a == "--no-mcp"));
339 }
340}