Skip to content

Instantly share code, notes, and snippets.

@mitsuse
Created February 19, 2015 01:12
Show Gist options
  • Save mitsuse/a45c99c7e405ed60e5ce to your computer and use it in GitHub Desktop.
Save mitsuse/a45c99c7e405ed60e5ce to your computer and use it in GitHub Desktop.
An example of command-line tool implemented in Golang with codegangsta/cli.
package main
import (
"fmt"
"os"
"github.com/codegangsta/cli"
)
func main() {
app := initApp()
app.Run(os.Args)
}
func initApp() *cli.App {
app := cli.NewApp()
app.Name = "greet"
app.Version = "0.0.1"
app.Usage = "Just greet you."
app.Author = "Tomoya Kose (mitsuse)"
app.Email = "tomoya@mitsuse.jp"
app.Commands = []cli.Command{
newPrintCommand(),
// Add more sub-commands ...
}
return app
}
func newPrintCommand() cli.Command {
command := cli.Command{
Name: "print",
ShortName: "p",
Usage: "Print the greeting",
Action: actionPrint,
Flags: []cli.Flag{
cli.StringFlag{
Name: "greeting,g",
Value: "Hello",
Usage: "The greeting to be shown",
},
cli.StringFlag{
Name: "name,n",
Value: "world",
Usage: "The name of person/something to be greeted",
},
// Add more command-line options for "print" ...
},
}
return command
}
func actionPrint(ctx *cli.Context) {
greeting := ctx.String("greeting")
name := ctx.String("name")
fmt.Printf("%s, %s.\n", greeting, name)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment