1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
# kongplete
You kongplete me.
kongplete lets you generate shell completions for your command-line programs using
[github.com/alecthomas/kong](https://github.com/alecthomas/kong) and [github.com/posener/complete](https://github.com/posener/complete).
## Examples
```golang
// This example is adapted from the shell example in github.com/alecthomas/kong
package main
import (
"fmt"
"os"
"github.com/alecthomas/kong"
"github.com/posener/complete"
"github.com/willabides/kongplete"
)
var shellCli struct {
Rm struct {
User string `help:"Run as user." short:"u" default:"default"`
Force bool `help:"Force removal." short:"f"`
Recursive bool `help:"Recursively remove files." short:"r"`
Hidden string `help:"A hidden flag" hidden:""`
Paths []string `arg:"" help:"Paths to remove." type:"path" name:"path" predictor:"file"`
} `cmd:"" help:"Remove files."`
Ls struct {
Paths []string `arg:"" optional:"" help:"Paths to list." type:"path" predictor:"file"`
} `cmd:"" help:"List paths."`
Hidden struct{} `cmd:"" help:"A hidden command" hidden:""`
Debug bool `help:"Debug mode."`
InstallCompletions kongplete.InstallCompletions `cmd:"" help:"install shell completions"`
}
func main() {
// Create a kong parser as usual, but don't run Parse quite yet.
parser := kong.Must(&shellCli,
kong.Name("shell"),
kong.Description("A shell-like example app."),
kong.UsageOnError(),
)
// Run kongplete.Complete to handle completion requests
kongplete.Complete(parser,
kongplete.WithPredictor("file", complete.PredictFiles("*")),
)
// Proceed as normal after kongplete.Complete.
ctx, err := parser.Parse(os.Args[1:])
parser.FatalIfErrorf(err)
switch ctx.Command() {
case "rm <path>":
fmt.Println(shellCli.Rm.Paths, shellCli.Rm.Force, shellCli.Rm.Recursive)
case "ls", "hidden":
}
}
```
|