Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/quiet-files-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bomb.sh/tab': patch
---

fix: keep the shell directive set by an option or argument handler, so handlers can enable file and directory completion
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,19 @@ my-cli complete -- install --port=""
:4
```

The last line is the shell directive. `:4` (`ShellCompDirectiveNoFileComp`) is the default and stops the shell from falling back to file completion. A handler can set another directive for its completion request, for example to let the shell complete file or directory paths:

```typescript
import t, { ShellCompDirective } from '@bomb.sh/tab';

devCmd.option('config', 'Config file', () => {
t.directive = ShellCompDirective.ShellCompDirectiveDefault; // files
});
devCmd.argument('dir', () => {
t.directive = ShellCompDirective.ShellCompDirectiveFilterDirs; // directories
});
```

## Documentation

See [bombshell docs](https://bomb.sh/docs/tab/).
Expand Down
3 changes: 1 addition & 2 deletions src/t.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,8 +387,6 @@ export class RootCommand extends Command {
}

private complete(toComplete: string) {
this.directive = ShellCompDirective.ShellCompDirectiveNoFileComp;

const seen = new Set<string>();
this.completions
.filter((comp) => {
Expand All @@ -412,6 +410,7 @@ export class RootCommand extends Command {

parse(args: string[]) {
this.completions = [];
this.directive = ShellCompDirective.ShellCompDirectiveNoFileComp;

const endsWithSpace = args[args.length - 1] === '';

Expand Down
63 changes: 63 additions & 0 deletions tests/directive.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { describe, it, expect, vi, afterEach } from 'vitest';
import { RootCommand, ShellCompDirective } from '../src/t';

function parse(root: RootCommand, args: string[]): string[] {
const log = vi.spyOn(console, 'log').mockImplementation(() => {});
root.parse(args);
return log.mock.calls.map(([line]) => String(line));
}

describe('completion directive', () => {
afterEach(() => {
vi.restoreAllMocks();
});

it('disables file completion by default', () => {
const root = new RootCommand();
root.command('build', 'Build').option('out', 'Output file', () => {});

expect(parse(root, ['build', '--out', ''])).toEqual([
`:${ShellCompDirective.ShellCompDirectiveNoFileComp}`,
]);
});

it('keeps a directive set by an option handler', () => {
const root = new RootCommand();
root.command('build', 'Build').option('out', 'Output file', () => {
root.directive = ShellCompDirective.ShellCompDirectiveDefault;
});

expect(parse(root, ['build', '--out', ''])).toEqual([
`:${ShellCompDirective.ShellCompDirectiveDefault}`,
]);
});

it('keeps a directive set by an argument handler', () => {
const root = new RootCommand();
root.command('serve', 'Serve').argument('dir', () => {
root.directive = ShellCompDirective.ShellCompDirectiveFilterDirs;
});

expect(parse(root, ['serve', ''])).toEqual([
`:${ShellCompDirective.ShellCompDirectiveFilterDirs}`,
]);
});

it('resets the directive for the next completion request', () => {
const root = new RootCommand();
const build = root.command('build', 'Build');
build.option('out', 'Output file', () => {
root.directive = ShellCompDirective.ShellCompDirectiveDefault;
});
build.option('mode', 'Build mode', (complete) => {
complete('production', '');
});

parse(root, ['build', '--out', '']);

expect(parse(root, ['build', '--mode', ''])).toEqual([
'production\t',
`:${ShellCompDirective.ShellCompDirectiveNoFileComp}`,
]);
});
});