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...
to quickly create a Cobra application.`, Run: func(cmd*cobra.Command, args []string) {fmt.Println("unbuf called")//TODO: work your own magic hereobj := unbuf.NewPlayer("chen","song") obj.GoPlayer()fmt.Println("\n-- start running --\n") unbuf.Runner() }, } func init() {...
为了确保命令行上的子命令、位置参数和 Flags 能够被准确的解析,cobra 总是执行根命令的 ExecuteC() 方法,其实现为在 ExecuteC() 方法中找到根命令,然后执行根命令的 ExecuteC() 方法,其逻辑如下: //ExecuteC executes the command.func (c *Command) ExecuteC() (cmd *Command, err error) {//Regardless...
vueper4楼•4 个月前作者
to quickly create a Cobra application.`,// Uncomment the following line if your bare application// has an action associated with it:// Run: func(cmd *cobra.Command, args []string) { },}// Execute adds all child commands to the root command sets flags appropriately.// This is called ...
在golang中有很多方法来处理命令行参数,简单情况下可以不使用任何库,直接使用os.Args;但是golang标准库提供了flag包来处理命令行参数;还有第三方提供的处理命令行参数的库cobra、cli等。 2、os.Args 如果你只是简单的想要获取命令行参数,可以像下面的代码示例一样使用os.Args来获取命令行参数。
// Uncomment the following line if your bare application // has an action associated with it: Run: func(cmd *cobra.Command, args []string) { if len(name) == 0 { cmd.Help() return } simple.Show(name, age) }, } // Execute adds all child commands to the root command sets flags...
fmt.Println("sub command") }, } 在最外面的main.go里,只用写一句话。 package main import ( "{pathToYourApp}/cmd" ) func main() { cmd.Execute() } 用cobra的架构来搭建命令行工具会使架构更清晰。 viper https://github.com/spf13/viper ...
如果有人真的想要添加此功能,项目的作者很乐意合并它。指定应用程序允许的格式很容易。 问:为什么叫“Viper”? 答:Viper是Cobra项目的同伴。虽然两者都可以完全独立运作,但它们一起可以为您的应用程序做非常多的基础工作。 问:为什么叫“Cobra”? 答:还能为commander取一个更好的名字吗?
Command, args []string) { _ = cmd.Help() }, } func main() { stu := student{ Name: "zhangsanfeng", Age: 20100 } cobrautils.BindFlags(rootCmd, &stu) _ = rootCmd.Execute() fmt.Printf("%+v", stu) } 执行结果 代码语言:javascript 代码运行次数:0 运行 AI代码解释 go run . --...