golangで開発アプリのコマンドインタフェースを拡張するには ...
今回は、golangでコマンドインタフェースの定義する際の自分用メモです。
以下の外部アプリを組み込むと、簡単にコマンドインタフェースが拡張できるみたいです。github.com
OMGが定めた分散オブジェクト技術の仕様"CORBA"とは、何の関係ありません。
あしからず。
spf13/cobra環境構築
golangのインストールは、ここでは割愛します。
$ export GOPATH=/Users/ttsubo/gocode $ go get github.com/spf13/cobra $ cd gocode/src/github.com/spf13 $ ls -l total 0 drwxr-xr-x 17 ttsubo staff 578 8 17 17:03 cobra drwxr-xr-x 38 ttsubo staff 1292 8 17 17:03 pflag
サンプルアプリ作成
Githubのリポジトリに記載されたREADME.mdのサンプルアプリをそのまま動作させてみて、その挙動を確認します。
まずは、サンプルアプリを準備します。
package main import( "github.com/spf13/cobra" "fmt" "strings" ) func main() { var echoTimes int var cmdPrint = &cobra.Command{ Use: "print [string to print]", Short: "Print anything to the screen", Long: `print is for printing anything back to the screen. For many years people have printed back to the screen. `, Run: func(cmd *cobra.Command, args []string) { fmt.Println("Print: " + strings.Join(args, " ")) }, } var cmdEcho = &cobra.Command{ Use: "echo [string to echo]", Short: "Echo anything to the screen", Long: `echo is for echoing anything back. Echo works a lot like print, except it has a child command. `, Run: func(cmd *cobra.Command, args []string) { fmt.Println("Print: " + strings.Join(args, " ")) }, } var cmdTimes = &cobra.Command{ Use: "times [# times] [string to echo]", Short: "Echo anything to the screen more times", Long: `echo things multiple times back to the user by providing a count and a string.`, Run: func(cmd *cobra.Command, args []string) { for i:=0; i < echoTimes; i++ { fmt.Println("Echo: " + strings.Join(args, " ")) } }, } cmdTimes.Flags().IntVarP(&echoTimes, "times", "t", 1, "times to echo the input") var rootCmd = &cobra.Command{Use: "app"} rootCmd.AddCommand(cmdPrint, cmdEcho) cmdEcho.AddCommand(cmdTimes) rootCmd.Execute() }
そして、サンプルアプリをコンパイルします。
$ go build app.go
サンプルアプリを動作させてみる
以降、サンプルアプリを動作させてみます。コマンド結果と、実際のコードを比較してみると、コマンドライン拡張できる様子が理解できると思います。
(1) helpコマンドで起動してみる。
$ ./app help Usage: app [command] Available Commands: print Print anything to the screen echo Echo anything to the screen help Help about any command Flags: -h, --help[=false]: help for app Use "app [command] --help" for more information about a command.
(2) printコマンドを起動してみる。
まずは、オプション"-h"を付与して起動してみます。
$ ./app print -h
print is for printing anything back to the screen.
For many years people have printed back to the screen.
Usage:
app print [string to print] [flags]
Flags:
-h, --help[=false]: help for printつづいて、printコマンドで任意文字列"aaa"を出力してみます。
$ ./app print aaa Print: aaa
(2) echoコマンドを起動してみる。
まずは、オプション"-h"を付与して起動してみます。
$ ./app echo -h
echo is for echoing anything back.
Echo works a lot like print, except it has a child command.
Usage:
app echo [string to echo] [flags]
app echo [command]
Available Commands:
times Echo anything to the screen more times
Flags:
-h, --help[=false]: help for echo
Use "app echo [command] --help" for more information about a command.つづいて、echoコマンドで任意文字列"aaa"を出力してみます。
$ ./app echo aaa Print: aaa
(2) echo timesコマンドを起動してみる。
まずは、オプション"-h"を付与して起動してみます。
$ ./app echo times -h
echo things multiple times back to the user by providing
a count and a string.
Usage:
app echo times [# times] [string to echo] [flags]
Flags:
-h, --help[=false]: help for times
-t, --times=1: times to echo the input
つづいて、echo timesコマンドで任意文字列"aaa"を出力してみます。
$ ./app echo times -t 2 aaa Echo: aaa Echo: aaa
echoのサブコマンドとしてtimesが動作している様子がわかります。