Skip to content

Commit 41c9c9d

Browse files
authored
Merge pull request #18 from njhale/enhance-basic-completions
enhance: enable shell completions
2 parents e28e7b7 + fa863df commit 41c9c9d

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)