(args: &'a [OsString])
| 146 | } |
| 147 | |
| 148 | fn parse(args: &'a [OsString]) -> anyhow::Result<Self> { |
| 149 | let mut args = args.iter(); |
| 150 | let mut root_dir = None; |
| 151 | let mut maybe_subcommand = None; |
| 152 | while let Some(arg) = args.next() { |
| 153 | let is_arg_value = |s: &OsStr| !os_str_starts_with(arg, "-") || s == "-"; |
| 154 | // "parse" only up to the first subcommand |
| 155 | if is_arg_value(arg) { |
| 156 | maybe_subcommand = Some(&**arg); |
| 157 | break; |
| 158 | } else if arg == "--" { |
| 159 | break; |
| 160 | } |
| 161 | let root_dir_arg = if arg == "--root-dir" { |
| 162 | args.next() |
| 163 | .filter(|s| is_arg_value(s)) |
| 164 | .context("a value is required for '--root-dir <root_dir>' but none was supplied")? |
| 165 | } else if let Some(arg) = os_str_strip_prefix(arg, "--root-dir=") { |
| 166 | arg |
| 167 | } else { |
| 168 | continue; |
| 169 | }; |
| 170 | anyhow::ensure!( |
| 171 | root_dir.is_none(), |
| 172 | "the argument '--root-dir <root_dir>' cannot be used multiple times" |
| 173 | ); |
| 174 | root_dir = Some(RootDir(root_dir_arg.into())); |
| 175 | } |
| 176 | Ok(Self { |
| 177 | root_dir, |
| 178 | maybe_subcommand, |
| 179 | }) |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | fn os_str_starts_with(s: &OsStr, pref: &str) -> bool { |
no test coverage detected