Skip to content

Instantly share code, notes, and snippets.

@memememomo
Created November 11, 2014 12:53
Show Gist options
  • Save memememomo/bb080fd5d16081ca4fba to your computer and use it in GitHub Desktop.
Save memememomo/bb080fd5d16081ca4fba to your computer and use it in GitHub Desktop.
cliパッケージでサブコマンドを作る ref: http://qiita.com/uchiko/items/5e5cda98ecb510671e56
package main
import (
"github.com/mitchellh/cli"
"log"
"os"
)
/** foo サブコマンド用の実装 **/
type Foo struct{}
func (f *Foo) Help() string {
return "app foo"
}
func (f *Foo) Run(args []string) int {
log.Println("Foo!")
return 0
}
func (f *Foo) Synopsis() string {
return "Print \"Foo!\""
}
/** bar サブコマンド用の実装 **/
type Bar struct{}
func (b *Bar) Help() string {
return "app bar"
}
func (b *Bar) Run(args []string) int {
log.Println("Bar!")
return 0
}
func (b *Bar) Synopsis() string {
return "Print \"Bar!\""
}
func main() {
// コマンドの名前とバージョンを指定
c := cli.NewCLI("app", "1.0.0")
// サブコマンドの引数を指定
c.Args = os.Args[1:]
// サブコマンド文字列 と コマンド実装の対応付け
c.Commands = map[string]cli.CommandFactory{
"foo": func() (cli.Command, error) {
return &Foo{}, nil
},
"bar": func() (cli.Command, error) {
return &Bar{}, nil
},
}
// コマンド実行
exitStatus, err := c.Run()
if err != nil {
log.Println(err)
}
os.Exit(exitStatus)
}
$ ./app --help foo
app foo
$ ./app --help bar
app bar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment