use crate::compsys::ported::_arguments::_arguments;
use crate::ported::params::{getaparam, getiparam};
fn help_branch_specs() -> Vec<String> {
[
":",
"*-i[specify option name exclude patterns]:option name exclude pattern",
"*-s[specify option aliases]:pattern and replacement as \"(this that)\"",
"*:helpspec (pattern\\:message\\:action)",
]
.iter()
.map(|s| s.to_string())
.collect()
}
fn flag_branch_specs() -> Vec<String> {
[
"-A",
"-([AMO]*|[0CRSWnsw])",
":",
"!-n[set $NORMARG]",
"-s[enable single-letter option stacking (-x -y == -xy)]",
"-w[(rarely needed) enable single-letter option stacking with arguments (-x X -y == -xy X)]",
"-W[(rarely needed) enable single-letter option stacking with arguments in the same word (-x X -y == -xXy)]",
"-C[modify $curcontext for `->action' (instead of $context)]",
"-R[when `->action' matches, return 300]",
"-S[honour `--' as end-of-options guard]",
"-A[do not complete options after non-options]:pattern matching unknown options (e.g., '-*')",
"-O[pass elements of array variable to function calls in actions]:array variable name:_parameters -g array",
"-M[specify matchspec for completing option names and values]:matchspec for completing option names and values [ 'r\\:|[_-]=* r\\:|=*' ]",
"-0[have ${(v)opt_args} be NUL-joined rather than colon-escaped and colon-joined]",
"--[derive optspecs from `${command} --help' output]",
"1::optional delimiter:(\\:)",
"*:spec (e.g., \"(-t --to)\"*{-t+,--to=}\"[specify recipient]\\:recipient's address\\:_email_addresses)",
]
.iter()
.map(|s| s.to_string())
.collect()
}
fn dashdash_before_current(words: &[String], current: i64) -> bool {
let idx = match words.iter().position(|w| w == "--") {
Some(p) => (p + 1) as i64, None => words.len() as i64 + 1,
};
idx < current
}
pub fn __arguments(_args: &[String]) -> i32 {
let _fn_scope = crate::compsys::ported::shared::FnScope::enter("__arguments");
let words = getaparam("words").unwrap_or_default();
let current = getiparam("CURRENT");
let specs = if dashdash_before_current(&words, current) {
help_branch_specs() } else {
flag_branch_specs()
};
crate::compsys::ported::shared::call_compfn("_arguments", &specs, || _arguments(&specs))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn dashdash_detection_matches_zsh_subscript_semantics() {
let w = vec!["_arguments".to_string(), "-".to_string()];
assert!(!dashdash_before_current(&w, 2));
let w = vec!["_arguments".to_string(), "--".to_string(), "".to_string()];
assert!(dashdash_before_current(&w, 3));
assert!(!dashdash_before_current(&w, 2));
}
#[test]
fn help_branch_specs_lead_with_separator_and_catch_all() {
let s = help_branch_specs();
assert_eq!(s[0], ":");
assert_eq!(s.last().unwrap(), "*:helpspec (pattern\\:message\\:action)");
assert!(s.iter().any(|x| x.starts_with("*-i[")));
assert!(s.iter().any(|x| x.starts_with("*-s[")));
}
#[test]
fn flag_branch_specs_carry_option_guard_and_all_flags() {
let s = flag_branch_specs();
assert_eq!(s[0], "-A");
assert_eq!(s[1], "-([AMO]*|[0CRSWnsw])");
assert_eq!(s[2], ":");
for lead in [
"!-n[", "-s[", "-w[", "-W[", "-C[", "-R[", "-S[", "-A[", "-O[", "-M[", "-0[", "--[",
] {
assert!(s.iter().any(|x| x.starts_with(lead)), "missing {lead}");
}
assert!(s
.last()
.unwrap()
.contains("\\:recipient's address\\:_email_addresses"));
}
}