Skip to content

feat(commander): support positional argument completions#129

Open
AmirSa12 wants to merge 1 commit into
mainfrom
feat/commander-positional-completions
Open

feat(commander): support positional argument completions#129
AmirSa12 wants to merge 1 commit into
mainfrom
feat/commander-positional-completions

Conversation

@AmirSa12

@AmirSa12 AmirSa12 commented Jun 11, 2026

Copy link
Copy Markdown
Member

in this pr commander adapter now read the positional args. same as the other two adapters we have which finally makes us able not to skip the tests for this case.
before this PR, commander completions only worked for flags.

pnpm tsx examples/demo.commander.ts complete -- lint ""
main.ts  Main file
index.ts Index file
:4

@changeset-bot

changeset-bot Bot commented Jun 11, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: da9fed9

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Jun 11, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@bomb.sh/tab@129

commit: da9fed9

@AmirSa12

AmirSa12 commented Jun 11, 2026

Copy link
Copy Markdown
Member Author

@shadowspawn your review is very appreciated! Thanks in advance ❤️

so commander already exposes what we need on each command .registeredArguments (https://github.com/tj/commander.js/blob/ba6d13ddb4243e5913367734f8c159089ffe7834/lib/command.js#L31C5-L31C35) so we now walk that list and register each positional with tab's command.argument

Comment thread src/commander.ts
function processArguments(tabCommand: TabCommand, cmd: CommanderCommand): void {
for (const arg of cmd.registeredArguments) {
const choices = arg.argChoices;
if (choices?.length) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ho ho! Support for completing choices, nice.

])
);

program.argument('[project]', 'Project name');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can not directly mix arguments and subcommands on same command in current Commander. While the completion works, the runtime does not. (Can achieve it with a default command, but more advanced and would require working out how to feed the default command into tab!)

Particularly for an example file, I suggest arguments on a leaf command are cleaner. As the lint command has.

% pnpm tsx ./examples/demo.commander.ts complete --
dev	Start dev server
serve	Start the server
build	Build the project
deploy	Deploy the application
lint	Lint source files
copy	Copy files
my-app	My application
my-lib	My library
my-tool	My tool
:4
% pnpm tsx ./examples/demo.commander.ts my-lib
error: unknown command 'my-lib'

% pnpm tsx ./examples/demo.commander.ts complete -- lint ""
main.ts	Main file
index.ts	Index file
:4
% pnpm tsx ./examples/demo.commander.ts lint main.ts
Linting files...

Comment thread src/commander.ts
processArguments(t, command);
}

function processArguments(tabCommand: TabCommand, cmd: CommanderCommand): void {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The processArguments integration is very straight-forward and makes good use of Commander. Nice!

Comment thread tests/cli.test.ts

describe.each(cliTools)('cli completion tests for %s', (cliTool) => {
// Commander does not have custom completions for arguments yet.
const shouldSkipTest = cliTool === 'commander';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Goodbye test suppression. Woop!

@shadowspawn

Copy link
Copy Markdown
Contributor

The integration looks clean and good.

Adding a root argument to the demo when there are subcommands is not actually functional with Commander. (Of interest. The cac example does not have any explicit arguments, but because the tests check against snapshot the tests "pass".)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants