bloomberg / stricli

Build complex CLIs with type safety and no dependencies
https://bloomberg.github.io/stricli
Apache License 2.0
630 stars 7 forks source link

The auto-complete example script does not work #17

Closed amckinney closed 2 days ago

amckinney commented 5 days ago

Describe the bug

The auto-complete example included in the template application is not functional.

To Reproduce

Generate a new template application with the following:

$ npx @stricli/create-app@latest my-app

Install the auto-completion command/script (for your runtime of choice), and try to run the CLI with autocomplete.

Expected behavior

Tab-to-complete works:

$ my-app [TAB][TAB]
nested         subdir

Additional context

I'm using deno:

$ deno --version
deno 2.0.0 (stable, release, aarch64-apple-darwin)
v8 12.9.202.13-rusty
typescript 5.6.2

When I looked at the source code, I noticed an issue with the generated bash-complete.ts file:

#!/usr/bin/env node
import { proposeCompletions } from "@stricli/core";
import { buildContext } from "../context";
import { app } from "../app";
const inputs = process.argv.slice(3);
if (process.env["COMP_LINE"]?.endsWith(" ")) {
  inputs.push("");
}
await proposeCompletions(app, inputs, buildContext(process));

I was able to get things working when I actually iterated over the completion set and wrote the results to stdout (separating each completion by a newline):

import { proposeCompletions } from "@stricli/core";
import process from "node:process";
import { cli } from "../cli/cli.ts";
import { buildContext } from "../context/context.ts";

const inputs = process.argv.slice(3);
if (process.env["COMP_LINE"]?.endsWith(" ")) {
  inputs.push("");
}

const completions = await proposeCompletions(
  cli,
  inputs,
  buildContext(process),
);
for (const completion of completions) {
  process.stdout.write(`${completion.completion}\n`);
}

Am I doing something wrong and/or overcomplicating anything here, or does the template just need to be fixed? Thanks!

molisani commented 2 days ago

Thanks for the report @amckinney. Yes it just looks like the template needs to be fixed to iterate over the completions and print them out.