Skip to content

Commit fa863df

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 fa863df

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

pkg/cli/gptscript.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,23 @@ 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+
cmd.TraverseChildren = true
56+
57+
// Enable shell completion for the gptscript command.
58+
// Note: The gptscript command doesn't have any subcommands, but Cobra requires that at least one is defined before
59+
// it will generate the completion command automatically. To work around this, define a hidden no-op subcommand.
60+
cmd.AddCommand(&cobra.Command{Hidden: true})
61+
cmd.SetHelpCommand(&cobra.Command{Hidden: true})
62+
63+
// Override arg completion to prevent the hidden subcommands from masking default completion for positional args.
64+
// Note: This should be removed if the gptscript command supports subcommands in the future.
65+
cmd.ValidArgsFunction = func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
66+
return nil, cobra.ShellCompDirectiveDefault
67+
}
5368
}
5469

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

0 commit comments

Comments
 (0)