DocsGeneratorTemplates

Templates

Some Generators are so common across CLI tools that it doesn't make sense for every spec to reimplement them from scratch.

The most notable examples are the generators that provide suggestions for filepaths and folders. Implementing these generators are surprisingly involved and require advanced properties, like filterTerm and trigger.

So as a shorthand, Fig offers Templates. template is a property are defined inside of Arguments.

The completion spec for cd demonstrate how templates are used. Rather than writing a complicated generator, we can just drop in the folders template.

const completion: Fig.Spec = {
  name: "cd",
  description: "Change the shell's working directory.",
  args: {
        name: "directory"
        template: "folders"
  }
};

Templates are syntactic sugar for complicated Generators. Conceptually, they do the same thing: help Fig provide suggestions for an argument. However, rather than saying how to generate the suggestions, they merely say what to generate… and leave the underlying implementation to Fig.

See "Generators" for more information on how to use templates.

Currently there are only two types of templates.

  1. filepaths
  2. folders

Filtering Template Suggestions

Notes & Tips

  • the filterTemplateSuggestions is only available if you define the the template inside a generator object
  • if you define template outside a generator object, this will override
  • If you don't plan on using filterTemplateSuggestions, we recommend defining template outside the generator object. It is easier to read inside the spec.
  • Both templates have a trigger attached to them which cause Fig to re-run the suggestion object when a user types a "/"
    • e.g. if a user is in their Home (~) directory and inserts cd Desktop/ Fig will render suggestions from the Desktop directory