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 {
174 self.print = enabled;
175 self
176 }
177
178 pub fn approve(mut self, enabled: bool) -> Self {
183 self.approve = enabled;
184 self
185 }
186
187 pub fn no_approve(mut self, enabled: bool) -> Self {
189 self.no_approve = enabled;
190 self
191 }
192
193 pub fn offline(mut self, enabled: bool) -> Self {
195 self.offline = enabled;
196 self
197 }
198
199 pub fn no_mcp(mut self, enabled: bool) -> Self {
202 self.no_mcp = enabled;
203 self
204 }
205
206 pub fn working_directory(mut self, dir: impl Into<PathBuf>) -> Self {
210 self.working_directory = Some(dir.into());
211 self
212 }
213
214 pub fn get_working_directory(&self) -> Option<&std::path::Path> {
216 self.working_directory.as_deref()
217 }
218
219 pub fn extra_args<I, S>(mut self, args: I) -> Self
222 where
223 I: IntoIterator<Item = S>,
224 S: Into<OsString>,
225 {
226 self.extra_args.extend(args.into_iter().map(Into::into));
227 self
228 }
229
230 pub fn prompt(mut self, prompt: impl Into<String>) -> Self {
232 self.prompt = Some(prompt.into());
233 self
234 }
235
236 pub fn assembled_args(&self) -> Vec<OsString> {
238 let mut a: Vec<OsString> = vec!["--mode".into(), self.mode.as_str().into()];
239 let mut flag = |name: &str, v: &Option<String>| {
240 if let Some(v) = v {
241 a.push(name.into());
242 a.push(v.into());
243 }
244 };
245 flag("--provider", &self.provider);
246 flag("--model", &self.model);
247 flag("--api-key", &self.api_key);
248 flag("--system-prompt", &self.system_prompt);
249 flag("--session", &self.session);
250 flag("--session-id", &self.session_id);
251 flag("--fork", &self.fork);
252 flag("--name", &self.name);
253 flag("--tools", &self.tools);
254 flag("--exclude-tools", &self.exclude_tools);
255 flag("--thinking", &self.thinking);
256 if let Some(dir) = &self.session_dir {
257 a.push("--session-dir".into());
258 a.push(dir.into());
259 }
260 if self.continue_session {
261 a.push("--continue".into());
262 }
263 if self.no_session {
264 a.push("--no-session".into());
265 }
266 if self.no_tools {
267 a.push("--no-tools".into());
268 }
269 if self.print {
270 a.push("--print".into());
271 }
272 if self.approve {
273 a.push("--approve".into());
274 }
275 if self.no_approve {
276 a.push("--no-approve".into());
277 }
278 if self.offline {
279 a.push("--offline".into());
280 }
281 if self.no_mcp {
282 a.push("--no-mcp".into());
283 }
284 a.extend(self.extra_args.iter().cloned());
285 if let Some(p) = &self.prompt {
286 a.push(p.into());
287 }
288 a
289 }
290}
291
292#[cfg(test)]
293mod tests {
294 use super::*;
295
296 #[test]
297 fn assembled_args_order_flags_then_extras_then_prompt() {
298 let args = PiCliBuilder::new()
299 .mode(Mode::Json)
300 .provider("anthropic")
301 .no_session(true)
302 .extra_args(["--verbose"])
303 .prompt("hello")
304 .assembled_args();
305 let s: Vec<String> = args
306 .iter()
307 .map(|a| a.to_string_lossy().into_owned())
308 .collect();
309 assert_eq!(s[0..2], ["--mode", "json"]);
310 assert!(s.contains(&"--no-session".to_string()));
311 let vi = s.iter().position(|x| x == "--verbose").unwrap();
312 assert_eq!(s.last().unwrap(), "hello");
313 assert!(vi < s.len() - 1, "extras precede the positional prompt");
314 }
315
316 #[test]
317 fn rpc_mode_default_has_no_prompt() {
318 let args = PiCliBuilder::new().mode(Mode::Rpc).assembled_args();
319 assert_eq!(args.len(), 2);
320 assert_eq!(args[1], "rpc");
321 }
322
323 #[test]
324 fn no_mcp_flag() {
325 let args = PiCliBuilder::new().no_mcp(true).assembled_args();
326 assert_eq!(args.last().unwrap(), "--no-mcp");
327 let args = PiCliBuilder::new().no_mcp(false).assembled_args();
328 assert!(!args.iter().any(|a| a == "--no-mcp"));
329 }
330}