diff --git a/go.mod b/go.mod index afb085dd..69849f66 100644 --- a/go.mod +++ b/go.mod @@ -22,6 +22,7 @@ require ( github.com/samber/lo v1.38.1 github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.8.0 + github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.8.4 github.com/tidwall/gjson v1.17.1 golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc @@ -68,7 +69,6 @@ require ( github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rivo/uniseg v0.1.0 // indirect - github.com/spf13/pflag v1.0.5 // indirect github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect github.com/therootcompany/xz v1.0.1 // indirect github.com/tidwall/match v1.1.1 // indirect diff --git a/pkg/cli/gptscript.go b/pkg/cli/gptscript.go index 637ecddf..18d757a0 100644 --- a/pkg/cli/gptscript.go +++ b/pkg/cli/gptscript.go @@ -25,6 +25,7 @@ import ( "github.com/gptscript-ai/gptscript/pkg/types" "github.com/gptscript-ai/gptscript/pkg/version" "github.com/spf13/cobra" + "github.com/spf13/pflag" "golang.org/x/term" ) @@ -58,9 +59,44 @@ type GPTScript struct { func New() *cobra.Command { root := &GPTScript{} - return cmd.Command(root, &Eval{ + command := cmd.Command(root, &Eval{ gptscript: root, }, &Credential{root: root}) + + // Hide all the global flags for the credential subcommand. + for _, child := range command.Commands() { + if strings.HasPrefix(child.Name(), "credential") { + command.PersistentFlags().VisitAll(func(f *pflag.Flag) { + newFlag := pflag.Flag{ + Name: f.Name, + Usage: f.Usage, + } + + if f.Name != "credential-context" { // We want to keep credential-context + child.Flags().AddFlag(&newFlag) + child.Flags().Lookup(newFlag.Name).Hidden = true + } + }) + + for _, grandchild := range child.Commands() { + command.PersistentFlags().VisitAll(func(f *pflag.Flag) { + newFlag := pflag.Flag{ + Name: f.Name, + Usage: f.Usage, + } + + if f.Name != "credential-context" { + grandchild.Flags().AddFlag(&newFlag) + grandchild.Flags().Lookup(newFlag.Name).Hidden = true + } + }) + } + + break + } + } + + return command } func (r *GPTScript) NewRunContext(cmd *cobra.Command) context.Context {