def bash_script

cli@0.2.0.0 · cli.bend:6713

import cli@0.2.0.0/cli.bend as Cli
import Base

6713def bash_script(+name: String, +items: List<&2, Item>) -> String:
6714  +all = all_places(items)
6715  +found = stops(all, 0n, place_lists(all, []))
6716  +id = String.append("_", ident(name))
6717  text_lines(List.concat(&2, String, [
6718    ["# bash completion; needs bash 4 or later.",
6719      "",
6720      "# Sets the tables of command path $path: opts, the options to offer; subs and",
6721      "# dests, each subcommand name and the path it enters; many, the slot of the",
6722      "# variadic positional, or 0; rest, 1 when that slot is rest().",
6723      String.append(id, "_scope() {"),
6724      "  opts=() subs=() dests=() many=0 rest=0",
6725      "  case $path in"],
6726    bash_arms(~bash_scope_arm, found),
6727    ["  esac",
6728      "}",
6729      "",
6730      "# Sets kind to what the option or positional $1 (a name, or a 1-based slot)",
6731      "# takes at $path: w, a word from values; f, a file; d, a directory; s, other",
6732      "# text. kind is empty for an option that takes no value and for a positional",
6733      "# that completes nothing; opt is 1 when the value can only follow \"=\".",
6734      String.append(id, "_arg() {"),
6735      "  kind= opt=0 values=()",
6736      "  case $path in"],
6737    bash_arms(~bash_arg_arm, found),
6738    ["  esac", "}"],
6739    bash_engine(id, Sh.quote(name)),
6740  ]))

Uses (15)

Used by (1)