Skip to content

Instantly share code, notes, and snippets.

@MakoTano
Last active May 18, 2019 12:32
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save MakoTano/624fe3fdea914b262e2c to your computer and use it in GitHub Desktop.
Save MakoTano/624fe3fdea914b262e2c to your computer and use it in GitHub Desktop.
github.com/codegangsta/cli を使ったコマンドラインツールのテンプレート
package main
import (
"fmt"
"os"
"github.com/codegangsta/cli"
)
func main() {
app := cli.NewApp()
app.Name = "sample_client"
app.Usage = "github.com/codegangsta/cli のテンプレートです"
app.Version = "0.0.1"
// グローバルオプション設定
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "dryrun, d", // 省略指定 => d
Usage: "グローバルオプション dryrunです。",
},
}
app.Commands = []cli.Command{
// コマンド設定
{
Name: "hello",
Aliases: []string{"h"},
Usage: "hello world を表示します",
Action: helloAction,
},
}
app.Before = func(c *cli.Context) error {
// 開始前の処理をここに書く
fmt.Println("開始")
return nil // error を返すと処理全体が終了
}
app.After = func(c *cli.Context) error {
// 終了時の処理をここに書く
fmt.Println("終了")
return nil
}
app.Run(os.Args)
}
func helloAction(c *cli.Context) {
// グローバルオプション
var isDry = c.GlobalBool("dryrun")
if isDry {
fmt.Println("this is dry-run")
}
// パラメータ
var paramFirst = ""
if len(c.Args()) > 0 {
paramFirst = c.Args().First() // c.Args()[0] と同じ意味
}
fmt.Printf("Hello world! %s\n", paramFirst)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment