Skip to content

Commit c5a9b50

Browse files
committed
enhance: enable shell completions
Enable basic shell completions for `bash`, `zsh`, `fish`, and `powershell` by exposing the `completion` command automatically generated by Cobra. The command is hidden, so it doesn't show up in the help text or completions. ```zsh $ source <(gptscript completion zsh) $ gptscript --[tab] -- completions -- --assemble -- Assemble tool to a single artifact, saved to --output ($GPTSCRIPT_ASSEMBLE) --cache -- Disable caching ($GPTSCRIPT_CACHE) --cache-dir -- Directory to store cache (default: $XDG_CACHE_HOME/gptscript) ($GPTSCRIPT_CACHE_DIR) --debug -- Enable debug logging ($GPTSCRIPT_DEBUG) --dump-state -- Dump the internal execution state to a file ($GPTSCRIPT_DUMP_STATE) --help -- help for gptscript --input -- Read input from a file ("-" for stdin) ($GPTSCRIPT_INPUT) --list-models -- List the models available and exit ($GPTSCRIPT_LIST_MODELS) --list-tools -- List built-in tools and exit ($GPTSCRIPT_LIST_TOOLS) --listen-address -- Server listen address ($GPTSCRIPT_LISTEN_ADDRESS) --openai-api-key -- OpenAI API KEY ($OPENAI_API_KEY) --openai-api-type -- OpenAI API Type (valid: OPEN_AI, AZURE, AZURE_AD) ($OPENAI_API_TYPE) --openai-api-version -- OpenAI API Version (for Azure) ($OPENAI_API_VERSION) --openai-base-url -- OpenAI base URL ($OPENAI_BASE_URL) --openai-org-id -- OpenAI organization ID ($OPENAI_ORG_ID) --output -- Save output to a file, or - for stdout ($GPTSCRIPT_OUTPUT) --quiet -- No output logging ($GPTSCRIPT_QUIET) --server -- Start server ($GPTSCRIPT_SERVER) --sub-tool -- Use tool of this name, not the first tool in file ($GPTSCRIPT_SUB_TOOL) --version -- version for gptscript ``` This implementation produces file path completions for ALL positional and flag args. Support for customizing completions for individual arguments will require new features in `github.com/acorn-io/cmd` and should be addressed in a followup. Signed-off-by: Nick Hale <[email protected]>
1 parent e28e7b7 commit c5a9b50

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

pkg/cli/gptscript.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,22 @@ func New() *cobra.Command {
4848
}
4949

5050
func (r *GPTScript) Customize(cmd *cobra.Command) {
51-
cmd.Use = version.ProgramName + " [flags] PROGRAM_FILE [INPUT...]"
5251
cmd.Flags().SetInterspersed(false)
52+
cmd.Use = version.ProgramName + " [flags] PROGRAM_FILE [INPUT...]"
53+
cmd.Version = version.Get().String()
54+
cmd.CompletionOptions.HiddenDefaultCmd = true
55+
56+
// Enable shell completion for the gptscript command.
57+
// Note: The gptscript command doesn't have any subcommands, but Cobra requires that at least one is defined before
58+
// it will generate the completion command automatically. To work around this, define a hidden no-op subcommand.
59+
cmd.AddCommand(&cobra.Command{Hidden: true})
60+
cmd.SetHelpCommand(&cobra.Command{Hidden: true})
61+
62+
// Override arg completion to prevent the hidden subcommands from masking default completion for positional args.
63+
// Note: This should be removed if the gptscript command supports subcommands in the future.
64+
cmd.ValidArgsFunction = func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
65+
return nil, cobra.ShellCompDirectiveDefault
66+
}
5367
}
5468

5569
func (r *GPTScript) listTools(ctx context.Context) error {

0 commit comments

Comments
 (0)