11import fs from 'node:fs/promises' ;
2+ import { generateCompletionScript , SUPPORTED_COMPLETION_SHELLS } from './completion.mjs' ;
23import { resolveLogPath } from './config.mjs' ;
34import {
45 buildConfigPathsReport ,
@@ -106,6 +107,7 @@ Usage:
106107 coder-studio doctor [--json]
107108 coder-studio config <subcommand>
108109 coder-studio auth <subcommand>
110+ coder-studio completion <bash|zsh|fish>
109111 coder-studio --version
110112
111113Global Flags:
@@ -121,12 +123,14 @@ Exit Codes:
121123
122124Examples:
123125 coder-studio help start
126+ coder-studio help completion
124127 coder-studio start
125128 coder-studio config show --json
126129 coder-studio config root set /srv/coder-studio/workspaces
127130 coder-studio auth ip list
131+ eval "$(coder-studio completion bash)"
128132
129- Run \`coder-studio config --help\` or \`coder-studio auth --help\` for detailed usage.
133+ Run \`coder-studio config --help\`, \`coder-studio auth --help\`, or \`coder-studio help completion \` for detailed usage.
130134` ) ;
131135}
132136
@@ -291,6 +295,22 @@ Examples:
291295` ) ;
292296}
293297
298+ function printCompletionHelp ( ) {
299+ console . log ( `coder-studio completion
300+
301+ Usage:
302+ coder-studio completion <bash|zsh|fish>
303+
304+ Description:
305+ Print a shell completion script to stdout. Evaluate or source the output in your shell profile.
306+
307+ Examples:
308+ eval "$(coder-studio completion bash)"
309+ source <(coder-studio completion zsh)
310+ coder-studio completion fish | source
311+ ` ) ;
312+ }
313+
294314function printHelpTopic ( topic ) {
295315 switch ( topic ) {
296316 case undefined :
@@ -326,6 +346,9 @@ function printHelpTopic(topic) {
326346 case 'auth' :
327347 printAuthHelp ( ) ;
328348 return EXIT_SUCCESS ;
349+ case 'completion' :
350+ printCompletionHelp ( ) ;
351+ return EXIT_SUCCESS ;
329352 default :
330353 throw usageError ( `unsupported help topic: ${ topic } ` , 'main' ) ;
331354 }
@@ -884,6 +907,8 @@ function printCliError(error, flags) {
884907 console . error ( 'hint: run `coder-studio config --help`' ) ;
885908 } else if ( normalized . helpTopic === 'auth' ) {
886909 console . error ( 'hint: run `coder-studio auth --help`' ) ;
910+ } else if ( normalized . helpTopic === 'completion' ) {
911+ console . error ( 'hint: run `coder-studio help completion`' ) ;
887912 } else if ( normalized . helpTopic === 'main' ) {
888913 console . error ( 'hint: run `coder-studio help`' ) ;
889914 }
@@ -907,6 +932,27 @@ export async function runCli(argv = process.argv.slice(2)) {
907932 return printHelpTopic ( command ) ;
908933 }
909934
935+ if ( command === 'completion' ) {
936+ if ( flags . json ) {
937+ throw usageError ( 'completion does not support --json' , 'completion' ) ;
938+ }
939+
940+ const [ shell , ...rest ] = positionals ;
941+ if ( ! shell ) {
942+ printCompletionHelp ( ) ;
943+ return EXIT_SUCCESS ;
944+ }
945+ if ( rest . length > 0 ) {
946+ throw usageError ( 'completion accepts exactly one <shell> argument' , 'completion' ) ;
947+ }
948+ if ( ! SUPPORTED_COMPLETION_SHELLS . includes ( shell ) ) {
949+ throw usageError ( `unsupported completion shell: ${ shell } ` , 'completion' ) ;
950+ }
951+
952+ process . stdout . write ( generateCompletionScript ( shell ) ) ;
953+ return EXIT_SUCCESS ;
954+ }
955+
910956 if ( command === 'config' ) {
911957 const context = await resolveCommandContext ( flags ) ;
912958 return await handleConfigCommand ( positionals , flags , context ) ;
0 commit comments