Your First CLI App
Getting Started
tiny1 Example
With cmdr, a tiny CLI app can be written like this,
package main
import (
"context"
"os"
"github.com/hedzr/cmdr/v2"
"github.com/hedzr/cmdr/v2/cli"
)
func main() {
ctx := context.Background() // with cancel can be passed thru in your actions
app := prepareApp(ctx)
if err := app.Run(ctx); err != nil {
println("Application Error:", err)
os.Exit(app.SuggestRetCode())
}
}
func prepareApp(ctx context.Context, opts ...cli.Opt) (app cli.App) {
app = cmdr.New(opts...).
Info("tiny1-app", "0.3.1").
Author("The Example Authors") // .Description(``).Header(``).Footer(``)
app.Cmd("jump").
Description("jump command").
Examples(`jump example`). // {{.AppName}}, {{.AppVersion}}, {{.DadCommands}}, {{.Commands}}, ...
OnAction(func(ctx context.Context, cmd cli.Cmd, args []string) (err error) {
println("jump command:", cmd)
return
}).
Build()
return
}It's simple, neither Store, nor external loaders integrated with it.
Next Step
What is Next?
How is this guide?
Last updated on