diff --git a/README.md b/README.md index 91ae9be..84a79ed 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ TODO. * Simple API. * Easy to integrate. +* Builtin `help` and `version` commands. * Clean and tested code. * Dependency-free. @@ -28,7 +29,42 @@ go get github.com/cristalhq/acmd ## Example -TODO +``` +cmds := []acmd.Command{ + { + Name: "now", + Description: "prints current time", + Do: func(ctx context.Context, args []string) error { + fmt.Printf("now: %s\n", now.Format("15:04:05")) + return nil + }, + }, + { + Name: "status", + Description: "prints status of the system", + Do: func(ctx context.Context, args []string) error { + // do something with ctx :) + return nil + }, + }, +} + +// all the acmd.Config fields are optional +r := acmd.RunnerOf(cmds, acmd.Config{ + AppName: "acmd-example", + AppDescription: "Example of acmd package", + Version: "the best v0.x.y", + Context: nil, + Args: []string{"now"}, + Usage: nil, +}) + +if err := r.Run(); err != nil { + panic(err) +} +``` + +Also see examples: [examples_test.go](https://github.com/cristalhq/acmd/blob/main/example_test.go). ## Documentation diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000..2d0e64c --- /dev/null +++ b/example_test.go @@ -0,0 +1,55 @@ +package acmd_test + +import ( + "context" + "fmt" + "net/http" + "time" + + "github.com/cristalhq/acmd" +) + +func ExampleRunner() { + const format = "15:04:05" + now, _ := time.Parse("15:04:05", "10:20:30") + + cmds := []acmd.Command{ + { + Name: "now", + Description: "prints current time", + Do: func(ctx context.Context, args []string) error { + fmt.Printf("now: %s\n", now.Format(format)) + return nil + }, + }, + { + Name: "status", + Description: "prints status of the system", + Do: func(ctx context.Context, args []string) error { + req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "https://www.githubstatus.com/", http.NoBody) + resp, err := http.DefaultClient.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + fmt.Print() + return nil + }, + }, + } + + r := acmd.RunnerOf(cmds, acmd.Config{ + AppName: "acmd-example", + AppDescription: "Example of acmd package", + Version: "the best v0.x.y", + Context: nil, + Args: []string{"now"}, + Usage: nil, + }) + + if err := r.Run(); err != nil { + panic(err) + } + + // Output: now: 10:20:30 +}