From 2ee8fdb53c0be628bcace3f47ea7d05ab3d1b5c3 Mon Sep 17 00:00:00 2001 From: rsteube Date: Mon, 10 Jul 2023 15:24:35 +0200 Subject: [PATCH] fix scrape --- command.go | 32 ++++++++++++++++---------------- scrape.go | 16 +++++++++++++--- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/command.go b/command.go index 247da9b..5413867 100644 --- a/command.go +++ b/command.go @@ -6,23 +6,23 @@ import ( ) type Command struct { - Name string `json:"name" jsonschema_description:"Name of the command"` - Aliases []string `json:"aliases,omitempty" jsonschema_description:"Aliases of the command"` - Description string `json:"description,omitempty" jsonschema_description:"Description of the command"` - Group string `json:"group,omitempty" jsonschema_description:"Group of the command"` - Hidden bool `json:"hidden,omitempty" jsonschema_description:"Hidden state of the command"` - Flags map[string]string `json:"flags,omitempty" jsonschema_description:"Flags of the command with their description"` - PersistentFlags map[string]string `json:"persistentflags,omitempty" jsonschema_description:"Persistent flags of the command with their description"` - ExclusiveFlags [][]string `json:"exclusiveflags,omitempty" jsonschema_description:"Flags that are mutually exclusive"` - Run run `json:"run,omitempty" jsonschema_description:"Command or script to execute in runnable mode"` + Name string `yaml:"name" jsonschema_description:"Name of the command"` + Aliases []string `yaml:"aliases,omitempty" jsonschema_description:"Aliases of the command"` + Description string `yaml:"description,omitempty" jsonschema_description:"Description of the command"` + Group string `yaml:"group,omitempty" jsonschema_description:"Group of the command"` + Hidden bool `yaml:"hidden,omitempty" jsonschema_description:"Hidden state of the command"` + Flags map[string]string `yaml:"flags,omitempty" jsonschema_description:"Flags of the command with their description"` + PersistentFlags map[string]string `yaml:"persistentflags,omitempty" jsonschema_description:"Persistent flags of the command with their description"` + ExclusiveFlags [][]string `yaml:"exclusiveflags,omitempty" jsonschema_description:"Flags that are mutually exclusive"` + Run run `yaml:"run,omitempty" jsonschema_description:"Command or script to execute in runnable mode"` Completion struct { - Flag map[string]action `json:"flag,omitempty" jsonschema_description:"Flag completion"` - Positional []action `json:"positional,omitempty" jsonschema_description:"Positional completion"` - PositionalAny action `json:"positionalany,omitempty" jsonschema_description:"Positional completion for every other position"` - Dash []action `json:"dash,omitempty" jsonschema_description:"Dash completion"` - DashAny action `json:"dashany,omitempty" jsonschema_description:"Dash completion of every other position"` - } `json:"completion,omitempty" jsonschema_description:"Completion definition"` - Commands []Command `json:"commands,omitempty" jsonschema_description:"Subcommands of the command"` + Flag map[string]action `yaml:"flag,omitempty" jsonschema_description:"Flag completion"` + Positional []action `yaml:"positional,omitempty" jsonschema_description:"Positional completion"` + PositionalAny action `yaml:"positionalany,omitempty" jsonschema_description:"Positional completion for every other position"` + Dash []action `yaml:"dash,omitempty" jsonschema_description:"Dash completion"` + DashAny action `yaml:"dashany,omitempty" jsonschema_description:"Dash completion of every other position"` + } `yaml:"completion,omitempty" jsonschema_description:"Completion definition"` + Commands []Command `yaml:"commands,omitempty" jsonschema_description:"Subcommands of the command"` } func (c Command) ToCobra() *cobra.Command { diff --git a/scrape.go b/scrape.go index ede20fe..c4044c7 100644 --- a/scrape.go +++ b/scrape.go @@ -46,9 +46,10 @@ func (s scrapeXXX) formatCommand() string { Short: "%v", GroupID: "%v", Aliases: []string{"%v"}, + Hidden: %v, Run: func(cmd *cobra.Command, args []string) {}, } -`, cmdVarName(s.cmd), strings.SplitN(s.cmd.Use, "\n", 2)[0], s.cmd.Short, s.cmd.GroupID, strings.Join(s.cmd.Aliases, `", "`)) +`, cmdVarName(s.cmd), strings.SplitN(s.cmd.Use, "\n", 2)[0], s.cmd.Short, s.cmd.GroupID, strings.Join(s.cmd.Aliases, `", "`), s.cmd.Hidden) if s.cmd.GroupID == "" { re := regexp.MustCompile("(?m)\n\tGroupID:.*$") @@ -61,6 +62,12 @@ func (s scrapeXXX) formatCommand() string { snippet = re.ReplaceAllString(snippet, "") } + // if !s.cmd.Hidden { + // re := regexp.MustCompile("(?m)\n\tHidden:.*$") + // snippet = re.ReplaceAllString(snippet, "") + + // } + return snippet } @@ -120,6 +127,10 @@ func scrape(cmd *cobra.Command, tmpDir string) { if f.Value.Type() != "bool" && f.NoOptDefVal != "" { fmt.Fprintf(out, ` %vCmd.Flag("%v").NoOptDefVal = "%v"`+"\n", cmdVarName(cmd), f.Name, f.NoOptDefVal) } + + if f.Hidden { + fmt.Fprintf(out, ` %vCmd.Flag("%v").Hidden = true`+"\n", cmdVarName(cmd), f.Name) + } }) if cmd.HasParent() { @@ -144,7 +155,7 @@ func scrape(cmd *cobra.Command, tmpDir string) { os.WriteFile(filename, formatted, 0644) for _, subcmd := range cmd.Commands() { - if !subcmd.Hidden && subcmd.Deprecated == "" { + if subcmd.Deprecated == "" && subcmd.Name() != "_carapace" { scrape(subcmd, tmpDir) } } @@ -248,7 +259,6 @@ func flagValue(f *pflag.Flag) string { return fmt.Sprintf(`[]%v{%v}`, strings.TrimSuffix(strings.TrimSuffix(f.Value.Type(), "Slice"), "Array"), f.Value.String()[1:len(f.Value.String())-1]) } - println(f.Value.String()) switch f.Value.Type() { case "string": return fmt.Sprintf(`"%v"`, f.Value.String())