Skip to content

Commit

Permalink
doc
Browse files Browse the repository at this point in the history
  • Loading branch information
koron committed Mar 14, 2024
1 parent 242dd44 commit efa0d11
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,93 @@ Install or update:
```console
$ go install github.com/koron-go/subcmd@latest
```

The basic usage consists of just 3 steps.

1. Define commands with function.

```go
package item

import "github.com/koron-go/subcmd"

var List = subcmd.DefineCommand("list", "list items", func(ctx context.Context, args []string) error {
// TODO: list your items (A)
return nil
})

var Add = subcmd.DefineCommand("add", "add a new item", func(ctx context.Context, args []string) error {
// TODO: add a your item (B)
return nil
})
```

2. Define command sets by bundling the defined commands.

```go
package item
var CommandSet = subcmd.DefineSet("item", List, Add)
```

Similarly, assume that multiple commands (List/Add/Delete) have been defined for user package.

```go
package user
var CommandSet = subcmd.DefineSet("user", List, Add, Delete)
```

3. Call function `subcmd.Run()` with the defined set from `main()`.

```go
package main
import (
"log"
"os"
"github.com/koron-go/subcmd"
"{some/pacakge/path}/item"
"{some/package/path}/user"
)
var rootSet = subcmd.DefineRootSet(
item.CommandSet,
user.CommandSet,
)
func main() {
if err := subcmd.Run(rootSet, os.Args[1:]...); err != nil {
log.Fatal(err)
}
}
```

This will enable you to use subcommands such as the following:

```console
# This call (A)
$ myprog item list
# This call (B)
$ myprog item add
# List subcommands in "item" command set
$ myprog item --list
# Same about "user" command set.
$ myprog user list
$ myprog user add
$ myprog user delete
$ myprog user --help
```

## Concept

* Want to write each command as a function.
* Want to associate command name and description with a function.
* Want to make commands and command sets reusable, and re-assemblable.
* Don't want to use types that are not in the standard library in the function signature.
* Don't want to declare a `struct` or define a type instead of a function.

0 comments on commit efa0d11

Please sign in to comment.