2017-12-17 3 views
2

다음 코드를 사용하여 cli에서 전달 된 플래그에 따라 실행해야하는 명령을 만듭니다. 내가 go run main.go echo test명령과 함께 빈을 제공하는 방법

그것을 실행할 때

는 내가 작동

Print: test

를 얻을 코브라의 repo 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 어떤 명령을 사용 c는 (MZR echo과 같은) 내가 로컬로 그것을 실행할 때 수 있었다 go run main.go echo test

좋아하지만 사용하고 싶습니다다음과 같이 또는 MZR echo, 어떻게 할 수 있습니까? (또한 내 친구에게 인 go install 이후에 생성 된 휴지통의 파일을 제공하십시오.

). 동일한 명령 줄 도구를 사용하여 실행이 REPO처럼 당신이 예를 들어 코드입니다 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'를 입력하십시오. 그래도 작동하지 않으면 bin 디렉토리를'export PATH = $ PATH : users/go/bin' 명령으로 경로에 추가하고 다시 시도하십시오. –

+0

감사합니다 1 +, 나는 그것을 사용하는 창문을 다르게 포장해야하는 친구에게 줄 필요가 있습니까? –

관련 문제