A minimalist framework for beautiful CLI applications written in Go.
go get github.com/jordanbrauer/console
- Clone (or fork) the repository
- Checkout a new branch from
trunk
- ???
- PROFIT!!!
Happy hacking!
Commands are defined as variables using a struct that's properties are act as a
form of configuration. At a minimum, your command should have a Name
and Run
property defined.
var greet = &console.Command{
Name: "greet",
Description: "fubar snafu",
Run: func(fubar *Command) ExitCode {
println("hello, world!")
return 0
}}
var noop = &console.Command{
Name: "noop",
Description: "nada, zip, ziltch, nothing",
Run: func() ExitCode {
return 0
}}
Commands are recursive. That is to say that they can have commands of their own.
To do this, simply populate the Commands
property with one or more Command
pointers, instead of defining a Run
function.
var parent = &console.Command{
Name: "foo",
Description: "i am the parent",
Commands: []*Command{child},
}
var child = &console.Command{
Name: "bar",
Description: "i am the child",
Run: func() ExitCode {
return 0
}}
which would be used like so
go run main.go foo bar
Once you have some commands, you can create a new CLI app, give it a version, and register one or more commands to be executed by the user.
cli := console.New("your version")
cli.Register(noop, greet)
cli.Splash(func () string {
// logo, author(s), version, licensing, whatever you want...
})
The easiest part is running the app. It's a good idea to pass the execution
result to os.Exit
so that the user's operating system can see the exit code
returned by the executed command.
os.Exit(cli.Run())