humane_commands/
opt_arg.rs1use std::{ffi::OsStr, process::Command};
2
3#[cfg(test)]
4mod test {
5 use std::process::Command;
6
7 use crate::opt_arg::OptionalArgExtension as _;
8
9 #[test]
10 fn opt_arg_some() {
11 let mut cmd = Command::new("echo");
12 cmd.opt_arg(Some("test"));
13 assert_eq!(cmd.get_args().into_iter().collect::<Vec<_>>(), ["test"]);
14 }
15 #[test]
16 fn opt_arg_none() {
17 let mut cmd = Command::new("echo");
18 cmd.arg("test").opt_arg(Option::<&str>::None);
19 assert_eq!(cmd.get_args().into_iter().collect::<Vec<_>>(), ["test"]);
20 }
21 #[test]
22 fn opt_arg_flag_some() {
23 let mut cmd = Command::new("echo");
24 cmd.opt_arg(("foo", Some("bar")));
25 assert_eq!(
26 cmd.get_args().into_iter().collect::<Vec<_>>(),
27 ["foo", "bar"]
28 );
29 }
30 #[test]
31 fn opt_arg_flag_none() {
32 let mut cmd = Command::new("echo");
33 cmd.arg("test").opt_arg(("foo", Option::<&str>::None));
34 assert_eq!(cmd.get_args().into_iter().collect::<Vec<_>>(), ["test"]);
35 }
36}
37
38pub trait OptionalArgExtension<T> {
39 fn opt_arg(&mut self, val: T) -> &mut Self;
67}
68
69impl<Value: AsRef<OsStr>> OptionalArgExtension<Option<Value>> for Command {
70 fn opt_arg(&mut self, val: Option<Value>) -> &mut Self {
71 if let Some(value) = val {
72 self.arg(value);
73 }
74 self
75 }
76}
77
78
79impl<Name: AsRef<OsStr>, Value: AsRef<OsStr>, I: IntoIterator<Item = Value>> OptionalArgExtension<(Name, I)>
80 for Command
81{
82 fn opt_arg(&mut self, val: (Name, I)) -> &mut Self {
83 let (flag, i) = val;
84 let mut i = i.into_iter();
85 if let Some(value) = i.next() {
86 self.arg(flag.as_ref()).arg(value.as_ref());
87 self.args(i);
88 }
89 self
90 }
91}