2017-12-17 9 views
2

次のコードを使用して、がcliから渡されたいくつかのフラグに従って実行されるコマンドを作成します。コマンドでゴーbinを提供する方法

私はgo run main.go echo test

でそれを実行したときに、私はどの作品

Print: test

を取得 https://github.com/spf13/cobra

コブラレポを使用しています。

今私はgo installを実行するには、 binディレクトリを開き、ファイルnewApp(私のアプリのこの私の名前)をクリックしてください

、それは

Usage: 
    MZR [command] 

Available Commands: 
    echo  Echo anything to the screen 
    help  Help about any command 
    print  Print anything to the screen 

Flags: 
    -h, --help help for MZR 

Use "MZR [command] --help" for more information about a command. 


[Process completed] 

を印刷し、私はANNOT任意のコマンドを使用するCMZR echoのような)私はそれをローカルで実行するとできましたgo run main.go echo test

しかし、私はそれを使用したい またはMZR echo、 どうすればいいのですか? (また、go installの後に作成されたビンからのファイルを私の友人に与える - Unix executable - 3.8 MB

同じコマンドラインツールを使用し、それを実行するには、このレポのようなあなたはこれが例のコードであるhoarder --server https://github.com/nanopack/hoarder

使う実行ファイルの名前が取られ

package main 

import (
    "fmt" 
    "strings" 

    "github.com/spf13/cobra" 
) 

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.`, 
     Args: cobra.MinimumNArgs(1), 
     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.`, 
     Args: cobra.MinimumNArgs(1), 
     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.`, 
     Args: cobra.MinimumNArgs(1), 
     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: "MZR"} 
    rootCmd.AddCommand(cmdPrint, cmdEcho) 
    cmdEcho.AddCommand(cmdTimes) 
    rootCmd.Execute() 
} 

答えて

4

(それをより簡単にするため)ディレクトリ名から削除します。ディレクトリnewAppの名前をMZRに変更します。この変更により、go installコマンドは、MZRという名前の実行可能ファイルを作成します。実行ファイルがあなたのパス上にある場合は、コマンドラインからMZR -hまたはMZR echo

+0

を使用して実行することができます。あなたが言ったようにして、 'MZR'ファイル(Unix実行可能ファイル)を' users/i044442/go/bin' dir(私はMacを使います)。どのようにそれを実行する必要がありますか?私がダブルクリックすると、これまでと同じようにすべてがプリントされます。あなたは '実行ファイルがあなたのパス上にある場合'どのようにそれを検証すべきですか?ビンにあれば十分ではありませんか? –

+0

'main.go echo test'を実行した端末で' MZR echo'と入力してください。それでも問題が解決しない場合は、 'export PATH = $ PATH:users/go/bin'コマンドでパスにbinディレクトリを追加し、もう一度やり直してください。 –

+0

ありがとう1 +、私はそれを別の方法でパックする必要があります私の友人にそれを与える必要がある場合は? –

関連する問題