Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Jul 8, 2023
0 parents commit 0a0f34a
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 rsteube

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# carapace-spec-kingpin

[Spec](https://github.com/rsteube/carapace-spec) generation for [alecthomas/kingpin](https://github.com/alecthomas/kingpin)
18 changes: 18 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package spec

type Command struct {
Name string `yaml:"name"`
Aliases []string `yaml:"aliases,omitempty"`
Description string `yaml:"description,omitempty"`
Group string `yaml:"group,omitempty"`
Flags map[string]string `yaml:"flags,omitempty"`
PersistentFlags map[string]string `yaml:"persistentflags,omitempty"`
Completion struct {
Flag map[string][]string `yaml:"flag,omitempty"`
Positional [][]string `yaml:"positional,omitempty"`
PositionalAny []string `yaml:"positionalany,omitempty"`
Dash [][]string `yaml:"dash,omitempty"`
DashAny []string `yaml:"dashany,omitempty"`
} `yaml:"completion,omitempty"`
Commands []Command `yaml:"commands,omitempty"`
}
14 changes: 14 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module github.com/rsteube/carapace-spec-kingpin

go 1.19

require (
github.com/alecthomas/kingpin/v2 v2.3.2
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect
github.com/stretchr/testify v1.8.4 // indirect
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
)
19 changes: 19 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
github.com/alecthomas/kingpin/v2 v2.3.2 h1:H0aULhgmSzN8xQ3nX1uxtdlTHYoPLu5AhHxWrKI6ocU=
github.com/alecthomas/kingpin/v2 v2.3.2/go.mod h1:0gyi0zQnjuFk8xrkNKamJoyUo382HRL7ATRpFZCw6tE=
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 h1:s6gZFSlWYmbqAuRjVTiNNhvNRfY2Wxp9nhfyel4rklc=
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc=
github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
81 changes: 81 additions & 0 deletions spec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package spec

import (
"fmt"

"github.com/alecthomas/kingpin/v2"
"gopkg.in/yaml.v3"
)

func Register(app *kingpin.Application) {
cmd := app.GetCommand("_carapace")
if cmd == nil {
cmd = app.Command("_carapace", "")
cmd.Hidden()
}

specCmd := cmd.Command("spec", "")
specCmd.Action(func(pc *kingpin.ParseContext) error {
fmt.Println(Scrape(app))
return nil
})
}

func Scrape(app *kingpin.Application) string {
cmd := command(&kingpin.CmdModel{
Name: app.Name,
Help: app.Help,
FlagGroupModel: app.Model().FlagGroupModel,
CmdGroupModel: app.Model().CmdGroupModel,
})
m, err := yaml.Marshal(cmd)
if err != nil {
panic(err.Error())
}
return string(m)
}

func command(c *kingpin.CmdModel) Command {
cmd := Command{
Name: c.Name,
Aliases: c.Aliases,
Description: c.Help,
Flags: make(map[string]string),
Commands: make([]Command, 0),
}
cmd.Completion.Flag = make(map[string][]string)

// TODO groups
// if group := node.Group; group != nil {
// cmd.Group = group.Key
// }

for _, flag := range c.Flags {
formatted := ""

if flag.Short != 0 {
formatted += fmt.Sprintf("-%v, ", string(flag.Short))
}
formatted += fmt.Sprintf("--%v", flag.Name)

switch {
case flag.IsBoolFlag():
//case optionalArgument: // TODO
// formatted += "?"
default:
formatted += "="
}

// if flag.IsCounter() || flag.IsCumulative() { // TODO
// formatted += "*"
// }
cmd.Flags[formatted] = flag.Help
}

for _, subcmd := range c.Commands {
if !subcmd.Hidden {
cmd.Commands = append(cmd.Commands, command(subcmd))
}
}
return cmd
}

0 comments on commit 0a0f34a

Please sign in to comment.