I have built some web servers with Go, but have not used Go for one of its most popular use cases: CLI tools. I decided to build a CLI tool to learn more about Go and CLIs in general. Instead of just building a CLI application, I decided to build a library that can be used to build CLI applications. This library is called Crane. It is heavily inspired by the already popular Cobra library.
I don't know that I would recommend using this library over Cobra or using it in production. I built this library to learn more about Go and CLIs. It has a finite set of features that are useful, but it is not as robust as Cobra.
- Command
- Args
- Nested Commands
- Flags
- Help
- Bash Completion
- Go
git clone https://github.com/mir-mirsodikov/crane
make build
go get github.com/mir-mirsodikov/crane
import "github.com/mir-mirsodikov/crane"
rootCmd := &crane.Command{
Name: "crane",
Short: "The crane testing application.",
Long: "A sample created testing application for the crane package.",
Handler: func(cmd *crane.Command, args []string) {
fmt.Println("Hello from the Root Command!")
},
}
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
}
versionCmd := &crane.Command{
Name: "version",
Aliases: []string{"v"},
Short: "Show version of the app",
Handler: func(cmd *crane.Command, args []string) {
fmt.Println("Version 0.1")
},
}
rootCmd.AddCommand(versionCmd)
helloCmd := &crane.Command{
Name: "hello [name]",
Aliases: []string{"h"},
NumOfArgs: 1,
Short: "Say hello to the world",
Handler: func(cmd *crane.Command, args []string) {
fmt.Println("Hello,", args[0]+"!")
},
}
package main
import (
"fmt"
"github.com/mir-mirsodikov/crane"
)
func main() {
rootCmd := &crane.Command{
Name: "crane",
Short: "The crane testing application.",
Long: "A sample created testing application for the crane package.",
Handler: func(cmd *crane.Command, args []string) {
fmt.Println("Hello, World from the Root Command!")
},
}
versionCmd := &crane.Command{
Name: "version",
Aliases: []string{"v"},
Short: "Show version of the app",
Handler: func(cmd *crane.Command, args []string) {
fmt.Println("Version 0.1")
},
}
helloCmd := &crane.Command{
Name: "hello [name]",
Aliases: []string{"h"},
NumOfArgs: 1,
Short: "Say hello to the world",
Handler: func(cmd *crane.Command, args []string) {
fmt.Println("Hello,", args[0]+"!")
},
}
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(helloCmd)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
}
}
This project is licensed under the MIT License - see the LICENSE file for details