Skip to content

Commit

Permalink
doc: Explain new completion system
Browse files Browse the repository at this point in the history
  • Loading branch information
juanibiapina committed Jun 20, 2024
1 parent b930e27 commit c9d6d9e
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ file. The special comments are:
Usage comment, when present, has specific syntactic rules and is used to
parse command line arguments. See [Validating arguments](#validating-arguments)
and [Parsing arguments](#parsing-arguments) for more information.
- `Options:` A description of the options the script accepts. This is used to
display help information and generate completions. See
[Completions](#completions) for more details.
- Extended documentation: Any other comment lines in this initial block will be
considered part of the extended documentation.

Expand Down Expand Up @@ -255,6 +258,42 @@ if [[ "${args[long]}" == "true" ]]; then
fi
```

## Completions

`sub` automatically provides completions for subcommand names.

To enable completions for positional arguments in the `Usage` comment, add an
`Options:` comment with a list of arguments. An option must have the format:
`name (completion_type): description`. Completion type is optional. Currently,
the only supported completion type is `script`, which allows for dynamic
completions like the following example:

```sh
# Usage: {cmd} <name>
# Options:
# name (script): A name

# check if we're being requested completions
if [[ "$_HAT_COMPLETE" == "true" ]]; then
if [[ "$_HAT_COMPLETE_ARG" == "name" ]]; then
echo "Alice"
echo "Bob"
echo "Charlie"
# note that you can run any command here to generate completions
fi

# make sure to exit when generating completions to prevent the script from running
exit 0
fi

# read arguments
declare -A args="($_HAT_ARGS)"

# say hello
echo "Hello, ${args[name]}!"
```


## Nested subcommands

`sub` supports nested directories for hierarchical command structures. For
Expand Down

0 comments on commit c9d6d9e

Please sign in to comment.