Go命令行库Cobra的核心文件root.go
因为docker及Kubernetes都在用cobra库,所以记录一下。
自定义的地方,高红标出。
root.go
/* Copyright © 2019 NAME HERE <EMAIL ADDRESS> Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package cmd import ( "fmt" "os" "github.com/spf13/cobra" "CobraDemo/imp" homedir "github.com/mitchellh/go-homedir" "github.com/spf13/viper" ) var cfgFile string var name string var age int // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "CobraDemo", Short: "A brief description of your application", Long: `A longer description that spans multiple lines and likely contains examples and usage of using your application. For example: Cobra is a CLI library for Go that empowers applications. This application is a tool to generate the needed files 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) { if len(name) == 0 { cmd.Help() return } imp.Show(name, age) }, } // Execute adds all child commands to the root command and sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { if err := rootCmd.Execute(); err != nil { fmt.Println(err) os.Exit(1) } } func init() { cobra.OnInitialize(initConfig) // Here you will define your flags and configuration settings. // Cobra supports persistent flags, which, if defined here, // will be global for your application. rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.CobraDemo.yaml)") // Cobra also supports local flags, which will only run // when this action is called directly. //rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") rootCmd.Flags().StringVarP(&name, "name", "n", "", "Person's name") rootCmd.Flags().IntVarP(&age, "age", "a", 0, "Person's age") } // initConfig reads in config file and ENV variables if set. func initConfig() { if cfgFile != "" { // Use config file from the flag. viper.SetConfigFile(cfgFile) } else { // Find home directory. home, err := homedir.Dir() if err != nil { fmt.Println(err) os.Exit(1) } // Search config in home directory with name ".CobraDemo" (without extension). viper.AddConfigPath(home) viper.SetConfigName(".CobraDemo") } viper.AutomaticEnv() // read in environment variables that match // If a config file is found, read it in. if err := viper.ReadInConfig(); err == nil { fmt.Println("Using config file:", viper.ConfigFileUsed()) } }
Go命令行库Cobra的核心文件root.go的更多相关文章
- golang命令行库cobra的使用
简介 Cobra既是一个用来创建强大的现代CLI命令行的golang库,也是一个生成程序应用和命令行文件的程序.下面是Cobra使用的一个演示: Cobra提供的功能 简易的子命令行模式,如 app ...
- golang命令行库cobra使用
github地址:https://github.com/spf13/cobra Cobra功能 简单子命令cli 如 kubectl verion kubectl get 自动识别-h,--h ...
- Google 开源的 Python 命令行库:fire 实现 git 命令
作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...
- 如何使用命令行编译和运行java文件
相信大家现在一般都在使用IDE环境来开发运行java文件,但我觉得可以在命令行里面简单运行java文件,技多不压身. 接下来我来说一下编译和运行java文件: 第一步,首先下一个入门程序(注意:一定要 ...
- 命令行编译多个java文件
如何使用命令行编译多个java文件 文件结构: method 1 cd javaproject javac -sourcepath javapath -d classpath javapath/*.j ...
- Linux (rz、sz命令行)与本地电脑 命令行上传、下载文件
Linux 与本地电脑直接交互, 命令行上传.下载文件. 一.lrzsz命令行安装: 1.rpm安装:(链接: http://pan.baidu.com/s/1cBuTm2 密码: vijf) rpm ...
- java命令行导出、导入sql文件
@IocBean public class SqlCommandModel{ //用户名 @Inject("java:$conf.get('jdbc.username')") pr ...
- python如何通过windows命令行运行一个python程序文件?
python如何通过windows命令行运行一个python程序文件? cmd 进入到py文件对应目录下或者直接在上面的文件地址栏输入cmd,敲入回车 定位到对应的目录下 输入python xxx.p ...
- Google 开源的 Python 命令行库:深入 fire(一)
作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...
随机推荐
- 10g+: Transportable Tablespaces Across Different Platforms (Doc ID 243304.1)
10g+: Transportable Tablespaces Across Different Platforms (Doc ID 243304.1) APPLIES TO: Oracle Data ...
- sso单点登录系统的压力测试
环境:vmware centos7.4 2cpu 2核心 工具:ab压力测试工具 测试对象:sso单点登录系统 电脑:win10 4核 项目环境:flask+uwsgi+nginx(uwsgi 2进程 ...
- 前端JSON请求转换Date问题
目的:记录使用SpringMVC中前端JSON数据中的日期转换成Date数据类型时区产生的问题 记录下遇到过的问题 在使用SpringMVC框架中,使用@RequestBody注解将前端的json数据 ...
- LINUX中查看、添加、删除PATH以及永久添加PATH
1.查看PATH变量 echo $PATH 会出现: 路径1:路径2:路径3 2.添加PATH变量 export PATH=$PATH:新添加的路径 用此方法添加的PATH,在关闭或者重启LINUX后 ...
- 验证登录的前世今生:session、cookie
参考地址:彻底理解cookie,session,token 使用JSON Web Token设计单点登录系统 1.很久很久以前,Web 基本上就是文档的浏览而已, 既然是浏览,作为服务器, 不需要记录 ...
- 算法设计与分析 1.2 不一样的fibonacci数列
★题目描述 fibonacci 数列的递推公式是F(n) = F(n-1) + F(n-2)(n >= 2 且 n 为整数). 将这个递推式改为F(n) = aF(n-1) + bF(n-2)( ...
- day83_11_1 阿里配python使用。
一.环境准备. 1.首先需要在支付包中注册开发者模式,并注册沙箱,模拟支付过程. https://openhome.alipay.com/platform/appDaily.htm?tab=info ...
- 解决:Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?
简单粗暴法 删除锁 $ sudo rm /var/cache/apt/archives/lock $ sudo rm /var/lib/dpkg/lock 如果还不行,重启虚拟机 $ reboot
- Windows下同时安装python2和python3如何兼容版本
引言:因学习需要把python2和python3都安装了,为了避免使用过程中混淆版本在网上找了一些解决方案,亲测可用.方法如下: 分别下载并安装Python2.x和Python3.x. 配置环境变量. ...
- vscode源码分析【五】事件分发机制
第一篇: vscode源码分析[一]从源码运行vscode 第二篇:vscode源码分析[二]程序的启动逻辑,第一个窗口是如何创建的 第三篇:vscode源码分析[三]程序的启动逻辑,性能问题的追踪 ...