Skip to content

Commit

Permalink
Add example
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed Oct 28, 2021
1 parent 900399e commit f51abe1
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ TODO.

* Simple API.
* Easy to integrate.
* Builtin `help` and `version` commands.
* Clean and tested code.
* Dependency-free.

Expand All @@ -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

Expand Down
55 changes: 55 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit f51abe1

Please sign in to comment.