@octen.ai/cli 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -0
- package/dist/cli.js +3 -0
- package/dist/commands/completion.d.ts +3 -0
- package/dist/commands/completion.js +125 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -169,6 +169,23 @@ Options: `--all` (both surfaces, all clients), `--mcp`, `--skills`, plus per-cli
|
|
|
169
169
|
|
|
170
170
|
---
|
|
171
171
|
|
|
172
|
+
## Shell completion
|
|
173
|
+
|
|
174
|
+
`octen completion <shell>` prints a completion script for `bash`, `zsh`, or `fish`. It completes subcommand names and each subcommand's flags.
|
|
175
|
+
|
|
176
|
+
```sh
|
|
177
|
+
# zsh — add to ~/.zshrc
|
|
178
|
+
eval "$(octen completion zsh)"
|
|
179
|
+
|
|
180
|
+
# bash — add to ~/.bashrc
|
|
181
|
+
eval "$(octen completion bash)"
|
|
182
|
+
|
|
183
|
+
# fish — write to the completions dir
|
|
184
|
+
octen completion fish > ~/.config/fish/completions/octen.fish
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
---
|
|
188
|
+
|
|
172
189
|
## Output
|
|
173
190
|
|
|
174
191
|
All commands print human-readable output when stdout is a TTY. When stdout is piped or `--json` is passed, commands emit raw JSON. Use `--pretty` to force human-readable output even when piped. `--no-color` disables ANSI colors.
|
package/dist/cli.js
CHANGED
|
@@ -13,6 +13,7 @@ import { registerVlEmbed } from "./commands/vlEmbed.js";
|
|
|
13
13
|
import { registerConfigureMcp } from "./commands/configureMcp.js";
|
|
14
14
|
import { registerConfigureSkills } from "./commands/configureSkills.js";
|
|
15
15
|
import { registerReset } from "./commands/reset.js";
|
|
16
|
+
import { registerCompletion } from "./commands/completion.js";
|
|
16
17
|
const pkg = JSON.parse(readFileSync(fileURLToPath(new URL("../package.json", import.meta.url)), "utf8"));
|
|
17
18
|
const program = new Command();
|
|
18
19
|
program
|
|
@@ -33,6 +34,8 @@ registerFetch(program);
|
|
|
33
34
|
registerChat(program);
|
|
34
35
|
registerEmbed(program);
|
|
35
36
|
registerVlEmbed(program);
|
|
37
|
+
// Register LAST so introspection sees every command and its flags.
|
|
38
|
+
registerCompletion(program);
|
|
36
39
|
program.parseAsync().catch((err) => {
|
|
37
40
|
process.stderr.write(pc.red(`error: ${err.message}\n`));
|
|
38
41
|
process.exit(exitCodeFor(err));
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { OctenValidationError } from "../api/errors.js";
|
|
2
|
+
const SUPPORTED_SHELLS = ["bash", "zsh", "fish"];
|
|
3
|
+
/** Collect long/short flag tokens (e.g. "--count", "-n") from a list of options. */
|
|
4
|
+
function flagsFor(options) {
|
|
5
|
+
const out = [];
|
|
6
|
+
for (const opt of options) {
|
|
7
|
+
if (opt.long)
|
|
8
|
+
out.push(opt.long);
|
|
9
|
+
if (opt.short)
|
|
10
|
+
out.push(opt.short);
|
|
11
|
+
}
|
|
12
|
+
return out;
|
|
13
|
+
}
|
|
14
|
+
/** Introspect the root program into a shell-agnostic completion model. */
|
|
15
|
+
function buildModel(program) {
|
|
16
|
+
const subcommands = program.commands.map((c) => c.name());
|
|
17
|
+
const globalFlags = flagsFor(program.options);
|
|
18
|
+
const subFlags = {};
|
|
19
|
+
for (const cmd of program.commands) {
|
|
20
|
+
subFlags[cmd.name()] = flagsFor(cmd.options);
|
|
21
|
+
}
|
|
22
|
+
return { subcommands, globalFlags, subFlags };
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* The shared bash `_octen` completion function, reused verbatim by the zsh
|
|
26
|
+
* script (via bashcompinit). Returns the full function plus `complete` line.
|
|
27
|
+
*/
|
|
28
|
+
function bashFunction(model) {
|
|
29
|
+
const subs = model.subcommands.join(" ");
|
|
30
|
+
const globals = model.globalFlags.join(" ");
|
|
31
|
+
// Build the case branches for per-subcommand flag completion.
|
|
32
|
+
const branches = model.subcommands
|
|
33
|
+
.map((sub) => {
|
|
34
|
+
const flags = [...(model.subFlags[sub] ?? []), ...model.globalFlags].join(" ");
|
|
35
|
+
return ` ${sub})\n opts="${flags}"\n ;;`;
|
|
36
|
+
})
|
|
37
|
+
.join("\n");
|
|
38
|
+
return `_octen() {
|
|
39
|
+
local cur sub i opts
|
|
40
|
+
cur="\${COMP_WORDS[COMP_CWORD]}"
|
|
41
|
+
sub=""
|
|
42
|
+
for (( i=1; i < COMP_CWORD; i++ )); do
|
|
43
|
+
case "\${COMP_WORDS[i]}" in
|
|
44
|
+
-*) ;;
|
|
45
|
+
*) sub="\${COMP_WORDS[i]}"; break ;;
|
|
46
|
+
esac
|
|
47
|
+
done
|
|
48
|
+
if [ -z "$sub" ]; then
|
|
49
|
+
opts="${subs} ${globals}"
|
|
50
|
+
else
|
|
51
|
+
case "$sub" in
|
|
52
|
+
${branches}
|
|
53
|
+
*)
|
|
54
|
+
opts="${globals}"
|
|
55
|
+
;;
|
|
56
|
+
esac
|
|
57
|
+
fi
|
|
58
|
+
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
|
|
59
|
+
return 0
|
|
60
|
+
}
|
|
61
|
+
complete -F _octen octen`;
|
|
62
|
+
}
|
|
63
|
+
function bashScript(model) {
|
|
64
|
+
return `#!/usr/bin/env bash\n# octen bash completion. Source via: eval "$(octen completion bash)"\n${bashFunction(model)}\n`;
|
|
65
|
+
}
|
|
66
|
+
function zshScript(model) {
|
|
67
|
+
// Reuse the bash function through zsh's bashcompinit shim. This is reliable
|
|
68
|
+
// and stays in sync with the bash implementation.
|
|
69
|
+
return `#compdef octen\n# octen zsh completion. Source via: eval "$(octen completion zsh)"\nif ! whence -w bashcompinit >/dev/null 2>&1; then\n autoload -Uz bashcompinit\nfi\nbashcompinit\n${bashFunction(model)}\n`;
|
|
70
|
+
}
|
|
71
|
+
/** Strip leading dashes for fish's `-l`/`-s` which want bare names. */
|
|
72
|
+
function fishFlagArgs(flags) {
|
|
73
|
+
return flags
|
|
74
|
+
.map((f) => {
|
|
75
|
+
if (f.startsWith("--"))
|
|
76
|
+
return `-l ${f.slice(2)}`;
|
|
77
|
+
if (f.startsWith("-"))
|
|
78
|
+
return `-s ${f.slice(1)}`;
|
|
79
|
+
return `-l ${f}`;
|
|
80
|
+
})
|
|
81
|
+
.join(" ");
|
|
82
|
+
}
|
|
83
|
+
function fishScript(model) {
|
|
84
|
+
const lines = [
|
|
85
|
+
"# octen fish completion.",
|
|
86
|
+
"# Install via: octen completion fish > ~/.config/fish/completions/octen.fish",
|
|
87
|
+
];
|
|
88
|
+
// Subcommands (only when no subcommand has been seen yet).
|
|
89
|
+
lines.push(`complete -c octen -f -n '__fish_use_subcommand' -a '${model.subcommands.join(" ")}'`);
|
|
90
|
+
// Global flags, always available.
|
|
91
|
+
for (const sub of model.subcommands) {
|
|
92
|
+
const flagArgs = fishFlagArgs(model.subFlags[sub] ?? []);
|
|
93
|
+
if (flagArgs) {
|
|
94
|
+
lines.push(`complete -c octen -n '__fish_seen_subcommand_from ${sub}' ${flagArgs}`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const globalArgs = fishFlagArgs(model.globalFlags);
|
|
98
|
+
if (globalArgs) {
|
|
99
|
+
lines.push(`complete -c octen ${globalArgs}`);
|
|
100
|
+
}
|
|
101
|
+
return lines.join("\n") + "\n";
|
|
102
|
+
}
|
|
103
|
+
export function buildCompletionScript(program, shell) {
|
|
104
|
+
const model = buildModel(program);
|
|
105
|
+
switch (shell) {
|
|
106
|
+
case "bash":
|
|
107
|
+
return bashScript(model);
|
|
108
|
+
case "zsh":
|
|
109
|
+
return zshScript(model);
|
|
110
|
+
case "fish":
|
|
111
|
+
return fishScript(model);
|
|
112
|
+
default:
|
|
113
|
+
throw new OctenValidationError(`unsupported shell: ${shell} (supported: ${SUPPORTED_SHELLS.join(", ")})`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
export function registerCompletion(program) {
|
|
117
|
+
program
|
|
118
|
+
.command("completion")
|
|
119
|
+
.argument("<shell>", "bash | zsh | fish")
|
|
120
|
+
.description("Output a shell completion script")
|
|
121
|
+
.action((shell) => {
|
|
122
|
+
const script = buildCompletionScript(program, shell);
|
|
123
|
+
process.stdout.write(script);
|
|
124
|
+
});
|
|
125
|
+
}
|