def fish_script
cli@0.2.0.0 · cli.bend:7294
import cli@0.2.0.0/cli.bend as Cli
import Base
7294def fish_script(+name: String, +items: List<&2, Item>) -> String:
7295 +all = all_places(items)
7296 +lists = place_lists(all, [])
7297 +found = stops(all, 0n, lists)
7298 +id = String.append("__", ident(name))
7299 +cq = Fish.quote(name)
7300 text_lines(List.concat(&2, String, [
7301 ["# fish completion; needs fish 3.3 or later.",
7302 "",
7303 "# Prints the number of the path that subcommand $argv[2] of path $argv[1]",
7304 "# enters; paths are numbered in pre-order from 0, the root.",
7305 String.concat(["function ", id, "_dest"]),
7306 " switch \"$argv[1] $argv[2]\""],
7307 fish_dests(found, found),
7308 [" case '*'", " return 1", " end", "end", "",
7309 "# Succeeds when option $argv[2] takes the next word as its value at path",
7310 "# $argv[1].",
7311 String.concat(["function ", id, "_valued"]),
7312 " switch $argv[1]"],
7313 bash_arms(~fish_valued, found),
7314 [" case '*'", " return 1", " end", "end", "",
7315 "# Prints how path $argv[1] takes words: 1 if it has subcommands, else 0; the",
7316 "# slot of its variadic positional, or 0; 1 if that slot is rest(), else 0.",
7317 String.concat(["function ", id, "_shape"]),
7318 " switch $argv[1]"],
7319 bash_arms(~fish_shape, found),
7320 [" case '*'", " echo 0 0 0", " end", "end"],
7321 fish_values_fn(id, lists),
7322 fish_engine(id),
7323 ["",
7324 "# Option lines also hold at v, so that fish can read grouped short options",
7325 "# before a value, as in -vC DIR.",
7326 String.concat(["complete -c ", cq, " -f"])],
7327 fish_lines(found, String.concat(["complete -c ", cq, " -n '", id, "_is "]),
7328 id),
7329 ]))