script method

String script(
  1. String executableName, {
  2. String? description,
  3. bool viaOption = false,
  4. String? commandName,
})

Generates the completion script (or spec) for executableName.

The script is independent of the commands and options of the executable: it asks the executable for the completion alternatives via the __complete request, so it needs to be installed only once.

description is a short description of the executable, included where the format supports it.

viaOption selects how the script is requested from the executable in the installation instructions of the script header: with the completion --shell <shell> command (the default) or, for an executable without subcommands, with the --completion <shell> option.

commandName makes the script complete that command name instead of executableName, for a wrapper script that forwards to the executable and has the same commands and options. The completion requests are then sent to executableName directly, bypassing the wrapper.

Implementation

String script(
  final String executableName, {
  final String? description,
  final bool viaOption = false,
  final String? commandName,
}) {
  final name = commandName ?? executableName;
  final header = scriptHeader(
    executableName,
    viaOption: viaOption,
    commandName: commandName,
  );
  // Requests go to the command as typed on the command line, unless the
  // script is for a wrapper: then they go to the executable directly.
  final program = name == executableName ? null : executableName;
  return switch (this) {
    CompletionShell.bash => bashCompletionScript(
      name,
      header,
      program: program,
    ),
    CompletionShell.zsh => zshCompletionScript(
      name,
      header,
      program: program,
    ),
    CompletionShell.fish => fishCompletionScript(
      name,
      header,
      program: program,
    ),
    CompletionShell.powershell => powershellCompletionScript(
      name,
      header,
      program: program,
    ),
    CompletionShell.carapace => carapaceCompletionSpec(
      name,
      header,
      program: executableName,
      description: description,
    ),
  };
}