Skip to content

Commit

Permalink
Merge pull request #171 from rsteube/fix-scrape
Browse files Browse the repository at this point in the history
fix scrape
  • Loading branch information
rsteube authored Jul 10, 2023
2 parents cbe595a + 2ee8fdb commit bd70fec
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
32 changes: 16 additions & 16 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 13 additions & 3 deletions scrape.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:.*$")
Expand All @@ -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
}

Expand Down Expand Up @@ -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() {
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit bd70fec

Please sign in to comment.