Walk argv against the live command tree to the deepest node that actually exists. The fallback for the [`ErrorKind`]s that carry no `Usage` context. Unlike [`path_from_usage`] this has to distinguish an accepted global flag from the one clap rejected. Known flags are skipped (along with their value); unknown flags stop the walk so we never attribute a root error to a later command.
(root: &clap::Command, argv: &[OsString])
| 96 | /// one clap rejected. Known flags are skipped (along with their value); unknown |
| 97 | /// flags stop the walk so we never attribute a root error to a later command. |
| 98 | fn walk_invoked(root: &clap::Command, argv: &[OsString]) -> (String, usize) { |
| 99 | let mut node = root; |
| 100 | let mut parts: Vec<String> = Vec::new(); |
| 101 | let mut index = 1; |
| 102 | let mut scope_start = 1; |
| 103 | |
| 104 | while index < argv.len() { |
| 105 | let Some(tok) = argv[index].to_str() else { |
| 106 | break; |
| 107 | }; |
| 108 | if tok == "--" { |
| 109 | break; |
| 110 | } |
| 111 | if tok.starts_with("--") { |
| 112 | let (name, inline_value) = tok |
| 113 | .split_once('=') |
| 114 | .map_or((tok, false), |(name, _)| (name, true)); |
| 115 | let long = name.trim_start_matches('-'); |
| 116 | let arg = node |
| 117 | .get_arguments() |
| 118 | .chain(root.get_arguments()) |
| 119 | .find(|arg| arg.get_long() == Some(long)); |
| 120 | let Some(arg) = arg else { |
| 121 | break; |
| 122 | }; |
| 123 | if !inline_value && arg.get_action().takes_values() { |
| 124 | index += 1; |
| 125 | } |
| 126 | index += 1; |
| 127 | continue; |
| 128 | } |
| 129 | if tok.starts_with('-') && tok.len() > 1 { |
| 130 | let mut consumes_next = false; |
| 131 | let mut known = true; |
| 132 | for short in tok[1..].chars() { |
| 133 | let arg = node |
| 134 | .get_arguments() |
| 135 | .chain(root.get_arguments()) |
| 136 | .find(|arg| arg.get_short() == Some(short)); |
| 137 | let Some(arg) = arg else { |
| 138 | known = false; |
| 139 | break; |
| 140 | }; |
| 141 | if arg.get_action().takes_values() { |
| 142 | consumes_next = tok.len() == 2; |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | if !known { |
| 147 | break; |
| 148 | } |
| 149 | if consumes_next { |
| 150 | index += 1; |
| 151 | } |
| 152 | index += 1; |
| 153 | continue; |
| 154 | } |
| 155 | match node.find_subcommand(tok) { |
no test coverage detected