wgetopt: fix long option match to always match prefix

This commit is contained in:
Neeraj Jaiswal
2023-02-18 21:53:04 +05:30
committed by Johannes Altmanninger
parent 1adfce18ee
commit 844174367b

View File

@@ -450,19 +450,22 @@ fn _find_matching_long_opt(
// Test all long options for either exact match or abbreviated matches.
for (option_index, p) in self.longopts.iter().enumerate() {
// Check if current option is prefix of long opt
if p.name.starts_with(&self.nextchar[..nameend]) {
// Exact match found.
pfound = Some(*p);
*indfound = option_index;
*exact = true;
break;
} else if pfound.is_none() {
// First nonexact match found.
pfound = Some(*p);
*indfound = option_index;
} else {
// Second or later nonexact match found.
*ambig = true;
if nameend == p.name.len() {
// The current option is exact match of this long option
pfound = Some(*p);
*indfound = option_index;
*exact = true;
break;
} else if pfound.is_none() {
// current option is first prefix match but not exact match
pfound = Some(*p);
*indfound = option_index;
} else {
// current option is second or later prefix match but not exact match
*ambig = true;
}
}
}
return pfound;