Skip to content

Commit 49c0bd7

Browse files
committed
spec: documentation
1 parent 7d529b7 commit 49c0bd7

File tree

5 files changed

+116
-25
lines changed

5 files changed

+116
-25
lines changed

internal/spec/command.go

-20
This file was deleted.

internal/spec/command/command.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package command
2+
3+
type Command struct {
4+
Name string `yaml:"name" json:"name" jsonschema_description:"Name of the command"`
5+
Aliases []string `yaml:"aliases,omitempty" json:"aliases,omitempty" jsonschema_description:"Aliases of the command"`
6+
Description string `yaml:"description,omitempty" json:"description,omitempty" jsonschema_description:"Description of the command"`
7+
Group string `yaml:"group,omitempty" json:"group,omitempty" jsonschema_description:"Group of the command"`
8+
Hidden bool `yaml:"hidden,omitempty" json:"hidden,omitempty" jsonschema_description:"Hidden state of the command"`
9+
Parsing Parsing `yaml:"parsing,omitempty" json:"parsing,omitempty" jsonschema_description:"Flag parsing mode of the command" jsonschema:"enum=interspersed,enum=non-interspersed,enum=disabled"`
10+
11+
Flags map[string]string `yaml:"flags,omitempty" json:"flags,omitempty" jsonschema_description:"Flags of the command with their description"`
12+
PersistentFlags map[string]string `yaml:"persistentflags,omitempty" json:"persistentflags,omitempty" jsonschema_description:"Persistent flags of the command with their description"`
13+
ExclusiveFlags [][]string `yaml:"exclusiveflags,omitempty" json:"exclusiveflags,omitempty" jsonschema_description:"Flags that are mutually exclusive"`
14+
Run string `yaml:"run,omitempty" json:"run,omitempty" jsonschema_description:"Command or script to execute in runnable mode"`
15+
Completion struct {
16+
Flag map[string][]string `yaml:"flag,omitempty" json:"flag,omitempty" jsonschema_description:"Flag completion"`
17+
Positional [][]string `yaml:"positional,omitempty" json:"positional,omitempty" jsonschema_description:"Positional completion"`
18+
PositionalAny []string `yaml:"positionalany,omitempty" json:"positionalany,omitempty" jsonschema_description:"Positional completion for every other position"`
19+
Dash [][]string `yaml:"dash,omitempty" json:"dash,omitempty" jsonschema_description:"Dash completion"`
20+
DashAny []string `yaml:"dashany,omitempty" json:"dashany,omitempty" jsonschema_description:"Dash completion of every other position"`
21+
} `yaml:"completion,omitempty" json:"completion,omitempty" jsonschema_description:"Completion definition"`
22+
Commands []Command `yaml:"commands,omitempty" json:"commands,omitempty" jsonschema_description:"Subcommands of the command"`
23+
24+
Documentation struct {
25+
Command string `yaml:"command,omitempty" json:"command,omitempty" jsonschema_description:"Documentation of the command"`
26+
Flag map[string]string `yaml:"flag,omitempty" json:"flag,omitempty" jsonschema_description:"Documentation of flags"`
27+
Positional []string `yaml:"positional,omitempty" json:"positional,omitempty" jsonschema_description:"Documentation of positional arguments"`
28+
PositionalAny string `yaml:"positionalany,omitempty" json:"positionalany,omitempty" jsonschema_description:"Documentation of other positional arguments"`
29+
Dash []string `yaml:"dash,omitempty" json:"dash,omitempty" jsonschema_description:"Documentation of dash arguments"`
30+
DashAny string `yaml:"dashany,omitempty" json:"dashany,omitempty" jsonschema_description:"Documentation of other dash arguments"`
31+
} `yaml:"documentation,omitempty" json:"documentation,omitempty" jsonschema_description:"Documentation"`
32+
Examples map[string]string `yaml:"examples,omitempty" json:"examples,omitempty" jsonschema_description:"Examples"`
33+
}
34+
35+
func (c *Command) AddFlag(f Flag) {
36+
switch {
37+
case f.Persistent:
38+
if c.PersistentFlags == nil {
39+
c.PersistentFlags = make(map[string]string)
40+
}
41+
c.PersistentFlags[f.format()] = f.Usage
42+
43+
default:
44+
if c.Flags == nil {
45+
c.Flags = make(map[string]string)
46+
}
47+
c.Flags[f.format()] = f.Usage
48+
}
49+
}

internal/spec/command/flag.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package command
2+
3+
type Flag struct {
4+
Longhand string
5+
Shorthand string
6+
Usage string
7+
Repeatable bool
8+
Optarg bool
9+
Value bool
10+
Hidden bool
11+
Required bool
12+
Persistent bool
13+
}
14+
15+
func (f Flag) format() string {
16+
var s string
17+
18+
if f.Shorthand != "" {
19+
s += f.Shorthand
20+
if f.Longhand != "" {
21+
s += ", "
22+
}
23+
}
24+
25+
if f.Longhand != "" {
26+
s += f.Longhand
27+
}
28+
29+
switch {
30+
case f.Optarg:
31+
s += "?"
32+
case f.Value:
33+
s += "="
34+
}
35+
36+
if f.Repeatable {
37+
s += "*"
38+
}
39+
40+
if f.Required {
41+
s += "!"
42+
}
43+
44+
if f.Hidden {
45+
s += "&"
46+
}
47+
48+
return s
49+
}

internal/spec/command/parsing.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package command
2+
3+
type Parsing string
4+
5+
const (
6+
DEFAULT Parsing = "" // INTERSPERSED but allows implicit changes
7+
INTERSPERSED Parsing = "interspersed" // mixed flags and positional arguments
8+
NON_INTERSPERSED Parsing = "non-interspersed" // flag parsing stopped after first positional argument
9+
DISABLED Parsing = "disabled" // flag parsing disabled
10+
)

internal/spec/spec.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,28 @@ package spec
33

44
import (
55
"github.com/rsteube/carapace/internal/pflagfork"
6+
"github.com/rsteube/carapace/internal/spec/command"
67
"github.com/spf13/cobra"
78
"github.com/spf13/pflag"
89
"gopkg.in/yaml.v3"
910
)
1011

1112
// Spec generates the spec file.
1213
func Spec(cmd *cobra.Command) string {
13-
m, _ := yaml.Marshal(command(cmd))
14+
m, _ := yaml.Marshal(genCommand(cmd))
1415
return "# yaml-language-server: $schema=https://carapace.sh/schemas/command.json\n" + string(m)
1516
}
1617

17-
func command(cmd *cobra.Command) Command {
18-
c := Command{
18+
func genCommand(cmd *cobra.Command) command.Command {
19+
c := command.Command{
1920
Name: cmd.Use,
2021
Description: cmd.Short,
2122
Aliases: cmd.Aliases,
2223
Group: cmd.GroupID,
2324
Hidden: cmd.Hidden,
2425
Flags: make(map[string]string),
2526
PersistentFlags: make(map[string]string),
26-
Commands: make([]Command, 0),
27+
Commands: make([]command.Command, 0),
2728
}
2829

2930
// TODO mutually exclusive flags
@@ -46,9 +47,11 @@ func command(cmd *cobra.Command) Command {
4647

4748
for _, subcmd := range cmd.Commands() {
4849
if subcmd.Name() != "_carapace" && subcmd.Deprecated == "" {
49-
c.Commands = append(c.Commands, command(subcmd))
50+
c.Commands = append(c.Commands, genCommand(subcmd))
5051
}
5152
}
5253

54+
c.Documentation.Command = cmd.Long
55+
5356
return c
5457
}

0 commit comments

Comments
 (0)