https://github.com/beevik/cmd
Command tree parser in go
https://github.com/beevik/cmd
Last synced: about 1 year ago
JSON representation
Command tree parser in go
- Host: GitHub
- URL: https://github.com/beevik/cmd
- Owner: beevik
- License: bsd-2-clause
- Created: 2018-03-24T03:29:06.000Z (about 8 years ago)
- Default Branch: main
- Last Pushed: 2024-07-06T20:08:23.000Z (almost 2 years ago)
- Last Synced: 2025-04-12T06:52:46.893Z (about 1 year ago)
- Language: Go
- Homepage:
- Size: 34.2 KB
- Stars: 2
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://godoc.org/github.com/beevik/cmd)
cmd
===
The `cmd` package is a lightweight, hierarchical command processor. It's
handy when you have a number of text commands you wish to organize into
a hierarchy.
For example, suppose you have written an application that uses the following
command hierarchy:
* file
* open
* close
* read
* write
* status
* quit
With each of these commands you have associated a callback function that is
called with user-supplied arguments whenever the command is matched.
Now consider what would happen if the application user types the following
command into the application:
```
file open foo.txt rw
```
This command string would be fed into a command tree's `Lookup` function,
which would return the callback associated with the `file/open` command
as well as a slice of string arguments `[]string{"foo.txt", "rw"}`.
The `cmd` package supports shortest unambiguous prefix matches, so the
following command would return the same results:
```
f o foo.txt rw
```
### Code examples
This code shows how the command tree used in the example above might be
created:
```go
tree := cmd.NewTree("root")
file := cmd.NewTree("file")
tree.AddCommand(cmd.Command{Name: "file", Subtree: file})
tree.AddCommand(cmd.Command{Name: "status", Brief: "Show status", Data: (*app).onStatus})
tree.AddCommand(cmd.Command{Name: "quit", Brief: "Quit application", Data: (*app).onQuit})
file.AddCommand(cmd.Command{Name: "open", Brief: "Open file", Data: (*app).onOpen})
file.AddCommand(cmd.Command{Name: "close", Brief: "Close file", Data: (*app).onClose})
file.AddCommand(cmd.Command{Name: "read", Brief: "Read file", Data: (*app).onRead})
file.AddCommand(cmd.Command{Name: "write", Brief: "Write file", Data: (*app).onWrite})
```
And here is how you might query the command tree:
```go
func (a *app) processCommand(s string) error {
sel, err := tree.Lookup(s)
switch {
case err == cmd.ErrAmbiguous:
fmt.Printf("Command '%s' is ambiguous.\n", s)
return err
case err == cmd.ErrNotFound:
fmt.Printf("Command '%s' not found.\n", s)
return err
default:
handler := sel.Command.Param.(func(a *app, args []string) error)
return handler(a, sel.Args)
}
}
```