https://github.com/ccampbell/clapp
Command line app builder for go
https://github.com/ccampbell/clapp
command-line command-line-tool golang golang-library
Last synced: 7 months ago
JSON representation
Command line app builder for go
- Host: GitHub
- URL: https://github.com/ccampbell/clapp
- Owner: ccampbell
- Created: 2015-07-26T02:11:03.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2024-07-04T13:12:22.000Z (over 1 year ago)
- Last Synced: 2024-10-15T18:45:44.783Z (about 1 year ago)
- Topics: command-line, command-line-tool, golang, golang-library
- Language: Go
- Size: 18.6 KB
- Stars: 4
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Clapp
Clapp is a simple tool for writing command line apps using Go. Rather than
being based around flags and subcommands, it takes a similar approach
to writing a web application. You define a series of patterns and handler
functions for the patterns. The handler gets passed a `clapp.Context` which you can use to access the arguments and flags passed into the command.
To install run
```
go get github.com/ccampbell/clapp
```
Package documentation is available here: https://godoc.org/github.com/ccampbell/clapp
## Example
This is a sample application to demonstrate some of the functionality
```go
package main
import(
"github.com/ccampbell/clapp"
"os"
)
func HelloWorldHandler(c *clapp.Context) {
c.Print("Hello World!")
}
func InvalidHandler(c *clapp.Context) {
c.FailWithCode("This failed to run!", 123)
}
func SayNameHandler(c *clapp.Context) {
c.Print("Say %s", c.Arg("name"))
}
func RunTaskHandler(c *clapp.Context) {
c.Print("Command is %s", c.Arg("command"))
c.Print("Run task named: %s", c.Arg("name"))
// Flags are automatically available even if they are not defined
if c.Flag("debug") != "" {
c.Print("Now we are running in debug mode")
}
}
func main() {
app := clapp.New("hello")
app.Version = "1.0"
// You can overwrite the default title/version and usage using these
// variables.
// app.Intro = "Sweet Hello App - version " + app.Version
// app.Usage = "This is the usage"
app.HandleFunc("world", HelloWorldHandler, "Print hello world")
// You can use named variables
app.HandleFunc("say [name]", SayNameHandler, "Says a person's name")
// You can use regular expressions
app.HandleFunc("task command:^(add|remove|restart)$ [name]", RunTaskHandler, "Perform a task on a name (command: add, remove, restart)")
app.HandleFunc("invalid", InvalidHandler)
// It is not required to define flags but any that you define will
// show up in the main usage output.
//
// If you want to set a default value then you do have to call DefineFlag
// and pass the default value as the third parameter.
app.DefineFlag("--config", "Path to config file", "config.json")
app.DefineFlag("--verbose", "Show verbose output")
app.AddAlias("-v", "--verbose")
app.Run(os.Args)
}
```