|
1 | 1 | # serpent |
2 | 2 |
|
3 | | -`serpent` is a CLI configuration framework based on [cobra](https://github.com/spf13/cobra). |
| 3 | +[](https://pkg.go.dev/github.com/coder/serpent) |
4 | 4 |
|
| 5 | +`serpent` is a CLI configuration framework based on [cobra](https://github.com/spf13/cobra) and used by [coder/coder](https://github.com/coder/coder). |
| 6 | +It's designed for large-scale CLIs with dozens of commands and hundreds |
| 7 | +of options. If you're building a small, self-contained tool, go with |
| 8 | +cobra. |
| 9 | + |
| 10 | +## Basic Usage |
| 11 | + |
| 12 | +See `example/echo`: |
| 13 | + |
| 14 | +```go |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "os" |
| 19 | + "strings" |
| 20 | + |
| 21 | + "github.com/coder/serpent" |
| 22 | +) |
| 23 | + |
| 24 | +func main() { |
| 25 | + var upper bool |
| 26 | + cmd := serpent.Cmd{ |
| 27 | + Use: "echo <text>", |
| 28 | + Short: "Prints the given text to the console.", |
| 29 | + Options: serpent.OptionSet{ |
| 30 | + { |
| 31 | + Name: "upper", |
| 32 | + Value: serpent.BoolOf(&upper), |
| 33 | + Flag: "upper", |
| 34 | + Description: "Prints the text in upper case.", |
| 35 | + }, |
| 36 | + }, |
| 37 | + Handler: func(inv *serpent.Invocation) error { |
| 38 | + if len(inv.Args) == 0 { |
| 39 | + inv.Stderr.Write([]byte("error: missing text\n")) |
| 40 | + os.Exit(1) |
| 41 | + } |
| 42 | + |
| 43 | + text := inv.Args[0] |
| 44 | + if upper { |
| 45 | + text = strings.ToUpper(text) |
| 46 | + } |
| 47 | + |
| 48 | + inv.Stdout.Write([]byte(text)) |
| 49 | + return nil |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + err := cmd.Invoke().WithOS().Run() |
| 54 | + if err != nil { |
| 55 | + panic(err) |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +## Design |
| 61 | +This Design section assumes you have a good understanding of how `cobra` works. |
| 62 | + |
| 63 | +### Options |
| 64 | + |
| 65 | +Serpent is designed for high-configurability. To us, that means providing |
| 66 | +many ways to configure the same value (env, YAML, flags, etc.) and keeping |
| 67 | +the code clean and testable as you scale the number of options. |
| 68 | + |
| 69 | + |
| 70 | +## More coming... |
| 71 | +This README is a stub for now. We'll better explain the design and usage |
| 72 | +of `serpent` in the future. |
0 commit comments