如何为 go bin 提供命令
How to provide go bin with commands
我使用以下代码创建命令,该命令应该 运行 根据一些标志
从 cli 传递过来。
我使用 cobra 存储库
https://github.com/spf13/cobra
当我 运行 它与 go run main.go echo test
我明白了
Print: test
有效。
现在我运行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]
我 c 不使用任何命令(比如 MZR echo
),当我 运行 在本地使用 go run main.go echo test
但我想像 一样使用它 MZR -h
或 MZR echo
,
我该怎么做?
(并且还给我的朋友提供了 go install
之后创建的 bin 中的文件 - 即 Unix executable - 3.8 MB
)
例如就像这个使用相同命令行工具的 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()
}
可执行文件的名称取自目录名称。将目录 newApp
重命名为 MZR
。通过此更改,go install
命令将创建一个名为 MZR
的可执行文件。如果可执行文件在您的路径上,那么您可以使用 MZR -h
或 MZR echo
、
从命令行 运行 它
我使用以下代码创建命令,该命令应该 运行 根据一些标志 从 cli 传递过来。
我使用 cobra 存储库 https://github.com/spf13/cobra
当我 运行 它与 go run main.go echo test
我明白了
Print: test
有效。
现在我运行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]
我 c 不使用任何命令(比如 MZR echo
),当我 运行 在本地使用 go run main.go echo test
但我想像 一样使用它 MZR -h
或 MZR echo
,
我该怎么做?
(并且还给我的朋友提供了 go install
之后创建的 bin 中的文件 - 即 Unix executable - 3.8 MB
)
例如就像这个使用相同命令行工具的 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()
}
可执行文件的名称取自目录名称。将目录 newApp
重命名为 MZR
。通过此更改,go install
命令将创建一个名为 MZR
的可执行文件。如果可执行文件在您的路径上,那么您可以使用 MZR -h
或 MZR echo
、