go cobra

https://github.com/spf13/cobra

https://github.com/spf13/cobra/blob/master/bash_completions.md

go get github.com/spf13/cobra/cobra

cobra init cobratest

main.go 目录下

cobra add serve
cobra add config
cobra add create -p 'configCmd'

Persistent Flags

一个标志可以是“持久的”,这意味着这个标志将被分配的命令以及该命令下的每个命令都可用。 对于全局标志,在根上分配一个标志作为持久标志。

Local Flags

一个标志也可以在本地分配,只适用于该特定的命令。

Local Flag on Parent Commands

默认情况下,Cobra只解析目标命令的本地标志,父命令的任何本地标志都被忽略。 通过启用Command.TraverseChildren Cobra将在执行目标命令之前分析每个命令上的本地标志。

command := cobra.Command{
  Use: "print [OPTIONS] [COMMANDS]",
  TraverseChildren: true,
}

https://github.com/spf13/viper#working-with-flags

原文地址:https://www.cnblogs.com/mhc-fly/p/7800552.html