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 {
86 self.model = Some(model.into());
87 self
88 }
89
90 pub fn api_key(mut self, key: impl Into<String>) -> Self {
93 self.api_key = Some(key.into());
94 self
95 }
96
97 pub fn system_prompt(mut self, prompt: impl Into<String>) -> Self {
98 self.system_prompt = Some(prompt.into());
99 self
100 }
101
102 pub fn session(mut self, session: impl Into<String>) -> Self {
104 self.session = Some(session.into());
105 self
106 }
107
108 pub fn session_dir(mut self, dir: impl Into<PathBuf>) -> Self {
109 self.session_dir = Some(dir.into());
110 self
111 }
112
113 pub fn session_id(mut self, id: impl Into<String>) -> Self {
115 self.session_id = Some(id.into());
116 self
117 }
118
119 pub fn fork(mut self, source: impl Into<String>) -> Self {
121 self.fork = Some(source.into());
122 self
123 }
124
125 pub fn continue_session(mut self, enabled: bool) -> Self {
127 self.continue_session = enabled;
128 self
129 }
130
131 pub fn no_session(mut self, enabled: bool) -> Self {
133 self.no_session = enabled;
134 self
135 }
136
137 pub fn name(mut self, name: impl Into<String>) -> Self {
139 self.name = Some(name.into());
140 self
141 }
142
143 pub fn no_tools(mut self, enabled: bool) -> Self {
145 self.no_tools = enabled;
146 self
147 }
148
149 pub fn tools(mut self, tools: impl Into<String>) -> Self {
155 self.tools = Some(tools.into());
156 self
157 }
158
159 pub fn exclude_tools(mut self, tools: impl Into<String>) -> Self {
162 self.exclude_tools = Some(tools.into());
163 self
164 }
165
166 pub fn thinking(mut self, level: impl Into<String>) -> Self {
168 self.thinking = Some(level.into());
169 self
170 }
171
172 pub fn print(mut self, enabled: bool) -> Self {
177 self.print = enabled;
178 self
179 }
180
181 pub fn approve(mut self, enabled: bool) -> Self {
186 self.approve = enabled;
187 self
188 }
189
190 pub fn no_approve(mut self, enabled: bool) -> Self {
192 self.no_approve = enabled;
193 self
194 }
195
196 pub fn offline(mut self, enabled: bool) -> Self {
198 self.offline = enabled;
199 self
200 }
201
202 pub fn no_mcp(mut self, enabled: bool) -> Self {
205 self.no_mcp = enabled;
206 self
207 }
208
209 pub fn working_directory(mut self, dir: impl Into<PathBuf>) -> Self {
213 self.working_directory = Some(dir.into());
214 self
215 }
216
217 pub fn get_working_directory(&self) -> Option<&std::path::Path> {
219 self.working_directory.as_deref()
220 }
221
222 pub fn extra_args<I, S>(mut self, args: I) -> Self
225 where
226 I: IntoIterator<Item = S>,
227 S: Into<OsString>,
228 {
229 self.extra_args.extend(args.into_iter().map(Into::into));
230 self
231 }
232
233 pub fn prompt(mut self, prompt: impl Into<String>) -> Self {
235 self.prompt = Some(prompt.into());
236 self
237 }
238
239 pub fn assembled_args(&self) -> Vec<OsString> {
241 let mut a: Vec<OsString> = vec!["--mode".into(), self.mode.as_str().into()];
242 let mut flag = |name: &str, v: &Option<String>| {
243 if let Some(v) = v {
244 a.push(name.into());
245 a.push(v.into());
246 }
247 };
248 flag("--provider", &self.provider);
249 flag("--model", &self.model);
250 flag("--api-key", &self.api_key);
251 flag("--system-prompt", &self.system_prompt);
252 flag("--session", &self.session);
253 flag("--session-id", &self.session_id);
254 flag("--fork", &self.fork);
255 flag("--name", &self.name);
256 flag("--tools", &self.tools);
257 flag("--exclude-tools", &self.exclude_tools);
258 flag("--thinking", &self.thinking);
259 if let Some(dir) = &self.session_dir {
260 a.push("--session-dir".into());
261 a.push(dir.into());
262 }
263 if self.continue_session {
264 a.push("--continue".into());
265 }
266 if self.no_session {
267 a.push("--no-session".into());
268 }
269 if self.no_tools {
270 a.push("--no-tools".into());
271 }
272 if self.print {
273 a.push("--print".into());
274 }
275 if self.approve {
276 a.push("--approve".into());
277 }
278 if self.no_approve {
279 a.push("--no-approve".into());
280 }
281 if self.offline {
282 a.push("--offline".into());
283 }
284 if self.no_mcp {
285 a.push("--no-mcp".into());
286 }
287 a.extend(self.extra_args.iter().cloned());
288 if let Some(p) = &self.prompt {
289 a.push(p.into());
290 }
291 a
292 }
293}
294
295#[cfg(test)]
296mod tests {
297 use super::*;
298
299 #[test]
300 fn assembled_args_order_flags_then_extras_then_prompt() {
301 let args = PiCliBuilder::new()
302 .mode(Mode::Json)
303 .provider("anthropic")
304 .no_session(true)
305 .extra_args(["--verbose"])
306 .prompt("hello")
307 .assembled_args();
308 let s: Vec<String> = args
309 .iter()
310 .map(|a| a.to_string_lossy().into_owned())
311 .collect();
312 assert_eq!(s[0..2], ["--mode", "json"]);
313 assert!(s.contains(&"--no-session".to_string()));
314 let vi = s.iter().position(|x| x == "--verbose").unwrap();
315 assert_eq!(s.last().unwrap(), "hello");
316 assert!(vi < s.len() - 1, "extras precede the positional prompt");
317 }
318
319 #[test]
320 fn rpc_mode_default_has_no_prompt() {
321 let args = PiCliBuilder::new().mode(Mode::Rpc).assembled_args();
322 assert_eq!(args.len(), 2);
323 assert_eq!(args[1], "rpc");
324 }
325
326 #[test]
327 fn no_mcp_flag() {
328 let args = PiCliBuilder::new().no_mcp(true).assembled_args();
329 assert_eq!(args.last().unwrap(), "--no-mcp");
330 let args = PiCliBuilder::new().no_mcp(false).assembled_args();
331 assert!(!args.iter().any(|a| a == "--no-mcp"));
332 }
333}