1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//! Locating the subcommand in mise's raw arguments without building the clap
//! tree, for code that runs before arguments are parsed.
/// Long and short forms of the top-level flags that consume a following argument.
///
/// Hardcoded rather than derived because `env.rs` needs it from `Lazy` statics
/// during startup — before anything has parsed arguments — and deriving it means
/// building the entire clap tree, which costs ~3.1M instructions. Doing that
/// there is what made every mise command ~6.3M instructions more expensive.
///
/// mise's `test_global_flags_with_values_matches_clap` asserts this equals what clap
/// reports, so adding a value-taking flag to its `Cli` without updating this list
/// fails CI rather than silently mis-parsing arguments.
pub const GLOBAL_FLAGS_WITH_VALUES: &[&str] = &[
"--cd",
"-C",
"--env",
"-E",
"--jobs",
"-j",
"--profile",
"-P",
"--shell",
"-s",
"--tool",
"-t",
"--log-level",
"--output",
];
/// The index of the first argument that is not a global flag or one of its
/// values, against [`GLOBAL_FLAGS_WITH_VALUES`].
///
/// For callers with no `Command` to hand, which would otherwise build the whole
/// tree just to read its top-level arguments.
pub fn first_non_global_arg_idx_cached(args: &[String]) -> Option<usize> {
first_non_global_arg_idx_with(|f| GLOBAL_FLAGS_WITH_VALUES.contains(&f), args)
}
/// As [`first_non_global_arg_idx_cached`], with `takes_value` deciding which
/// flags consume the next argument.
pub fn first_non_global_arg_idx_with(
takes_value: impl Fn(&str) -> bool,
args: &[String],
) -> Option<usize> {
let mut i = 1;
while i < args.len() {
let arg = &args[i];
if arg == "--" {
return None;
}
if !arg.starts_with('-') {
return Some(i);
}
let flag_takes_separate_value = if arg.starts_with("--") {
if arg.contains('=') {
false
} else {
let flag_name = arg.split('=').next().unwrap();
takes_value(flag_name)
}
} else if let Some(flag_name) = arg.get(..2) {
// `arg.get(..2)` (not `&arg[..2]`) avoids panicking when the arg is
// not valid UTF-8 in the first place: args are read lossily, so a
// malformed byte becomes a multi-byte U+FFFD and byte index 2 may not
// be a char boundary. A short flag is always ASCII, so a non-ASCII
// prefix simply matches no value-taking flag.
arg.len() == 2 && takes_value(flag_name)
} else {
false
};
if flag_takes_separate_value && i + 1 < args.len() {
i += 2;
} else {
i += 1;
}
}
None
}