Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for CLI tab completions #741

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,33 @@ WSL-based installation.
See [here](INSTALL_FROM_SOURCE.md).


## Shell Tab Completion

Tab completion is available for the following shells: Bash, Zsh, and Tcsh.

To enable tab completion, run the following command to generate the appropriate script:

```bash
fairseq2 --print-completion {bash|zsh|tcsh}
```

You can either save the output to the shell's completion directory for permanent use or apply it directly to your current session.

For example, to enable Bash completion for the current session, use:
```bash
eval "$(fairseq2 --print-completion bash)"
```

For a permanent setup, run the following command to store the completion script:
```bash
echo "eval $(fairseq2 --print-completion bash)" >> "$BASH_COMPLETION_COMPAT_DIR/shtab"
```

Here, `BASH_COMPLETION_COMPAT_DIR` is the path to your system's shell completion directory.

This ensures that tab completion will be enabled every time you start a new shell session.


## Contributing
We always welcome contributions to fairseq2! Please refer to
[Contribution Guidelines](CONTRIBUTING.md) to learn how to format, test, and
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"torcheval~=0.0.6",
"tqdm~=4.62",
"typing_extensions~=4.12",
"shtab~=1.7",
],
extras_require={
"arrow": ["pyarrow>=13.0.0", "pandas~=2.0.0"],
Expand Down
6 changes: 4 additions & 2 deletions src/fairseq2/recipes/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from types import FrameType
from typing import Generic, Protocol, TypeVar, final, runtime_checkable

import shtab
import yaml
from rich.console import Console
from typing_extensions import override
Expand Down Expand Up @@ -139,6 +140,7 @@ def __call__(self) -> None:

def _run_command(self) -> None:
parser = ArgumentParser(self._name, description=self._description)
shtab.add_argument_to(parser, ["-s", "--print-completion"])

self.init_parser(parser)

Expand Down Expand Up @@ -484,7 +486,7 @@ def init_parser(self, parser: ArgumentParser) -> None:
type=Path,
nargs="*",
help="yaml configuration file(s)",
)
).complete = shtab.FILE # type: ignore[attr-defined]

parser.add_argument(
"--config",
Expand Down Expand Up @@ -527,7 +529,7 @@ def init_parser(self, parser: ArgumentParser) -> None:
type=Path,
nargs=OPTIONAL,
help="directory to store recipe artifacts",
)
).complete = shtab.DIRECTORY # type: ignore[attr-defined]

@override
def __call__(self, args: Namespace) -> None:
Expand Down
5 changes: 3 additions & 2 deletions src/fairseq2/recipes/llama/convert_checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from typing import final
from warnings import catch_warnings

import shtab
from typing_extensions import override

from fairseq2.console import get_error_console
Expand Down Expand Up @@ -43,13 +44,13 @@ def init_parser(self, parser: ArgumentParser) -> None:
"input_dir",
type=Path,
help="checkpoint directory",
)
).complete = shtab.DIRECTORY # type: ignore[attr-defined]

parser.add_argument(
"output_dir",
type=Path,
help="output directory to store reference checkpoint",
)
).complete = shtab.DIRECTORY # type: ignore[attr-defined]

@override
def __call__(self, args: Namespace) -> None:
Expand Down
Loading