使用Cli构建Go的命令行应用
在Go里面应用中flag这一标准库,提供了很多我们在写命令行时需要的interface,然而如果你需要更强大更好的结构,可以试一下cli.go这个库。
利用cli.go来写命令行应用
定义命令和子命令
下面是一个例子:
package main import (
"fmt"
"os" "github.com/codegangsta/cli"
) func main() {
app := cli.NewApp()
app.Name = "jasperapp"
app.Usage = "sample command-line app by jasper"
app.Author = "Carter"
app.Email = "jasper@xxx.com"
app.Commands = []cli.Command{
{
Name: "read",
ShortName: "r",
Usage: "read something",
Subcommands: []cli.Command{
{
Name: "articles",
Usage: "read articles",
Action: readArticles,
},
{
Name: "tweets",
Usage: "read Tweets",
Action: readTweets,
},
},
},
}
app.Run(os.Args)
} func readArticles(ctx *cli.Context) {
fmt.Println("Go to http://www.opscoder.info to read articles!")
} func readTweets(ctx *cli.Context) {
fmt.Println("Go to http://www.opscoder.info to read our tweets!")
}
如果你不带参数去运行,你将会得到下面的帮助信息:
NAME:
jasperapp - sample command-line app by jasper USAGE:
jasperapp [global options] command [command options] [arguments...] VERSION:
0.0.0 AUTHOR:
Carter - <jasper@xxx.com> COMMANDS:
read, r read something
help, h Shows a list of commands or help for one command GLOBAL OPTIONS:
--help, -h show help
--version, -v print the version
我们定义一个read子命令,它有articles和tweet两个命令:
jasperapp read articles
输出为:
Go to http://www.opscoder.info to read articles!
或是运行:
jasperapp read tweets
输出为:
Go to http://www.opscoder.info to read our tweets!
使用context
每个动作都有一个context,在下面的例子中函数readArticles和readTweets接收一个参数,我们可以这样使用:
func readArticles(ctx *cli.Context) {
fmt.Printf("The first argument was: %s", ctx.Args().First())
}
现在运行jasperapp read articles please将会输出:
The first argument was: please
使用flags
为了展示flags的用法,我们稍微修改一下tweets子命令:
{
Name: "tweets",
Usage: "read Tweets",
Flags: []cli.Flag{
cli.StringFlag{
Name: "account",
Value: "TheJasper",
Usage: "name of Twitter account",
},
},
Action: readTwitter,
},
其中readTweets函数这么写:
func readTwitter(ctx *cli.Context) {
fmt.Printf("Go to http://www.opscoder.info/%s to read tweets!", ctx.String("account"))
}
现在让我们编译结果并查看下tweets子命令的帮助信息:
jasperapp read tweets --help
输出如下:
NAME:
tweets - read Tweets USAGE:
command tweets [command options] [arguments...] OPTIONS:
--account 'TheJasper' name of Twitter account
现在让我们试试:
$ jasperapp read tweets
Go to http://www.opscoder.info/TheJasper to read tweets! $ jasperapp read tweets --account codegangsta
Go to http://www.opscoder.info/codegangsta to read tweets!
总结
Cli.go还有很多自定义的东西,具体可以参考Godoc的文档。
使用Cli构建Go的命令行应用的更多相关文章
- CLI:使用Go开发命令行应用
原文地址 CLI或者"command line interface"是用户在命令行下交互的程序.由于通过将程序编译到一个静态文件中来减少依赖,一次Go特别适合开发CLI程序.如 ...
- php cli模式学习(PHP命令行模式)
http://www.jb51.net/article/37796.htm php_cli模式简介 php-cli是php Command Line Interface的简称,如同它名字的意思,就是 ...
- php在cli模式下取得命令行中的参数的方法-getopt命令行可传递数组-简单自定义方法取命令行参数
在cli模式下执行PHP时,自动给脚本文件传递了一个变量$argv,其值即是一个命令中所有值组成的数组(以空格区分),在PHP程序中接收参数有3种方法1.直接使用argv变量数组. 2.使用$_SER ...
- Azure CLI 2.0-Azure新命令行工具介绍
Azure CLI 2.0 是 Azure 的新命令行体验,用于管理 Azure 资源. 可以将其安装在 macOS.Linux 和 Windows 上,然后从命令行运行它. Azure CLI 2. ...
- 4 个用于构建优秀的命令行用户界面的 Python 库
作者: Amjith Ramanujam 译者: LCTT Lv Feng 在这个分为两篇的关于具有绝佳命令行界面的终端程序的系列文章的第二篇教程中,我们将讨论 Prompt.Toolkit.Clic ...
- 构建一个Flowable命令行应用
官网链接 [(https://flowable.com/open-source/docs/bpmn/ch02-GettingStarted/#building-a-command-line-appli ...
- C++/cli中swtich处理命令行接收到的关键字
QQ群友中有人提出这样一个问题: Swtich接受的是整形或枚举类型, 关键字多数是一个字符, 将string转换成char就应该可以, 所以我试着写了一下代码, 直接提取string的第一个字符, ...
- Apache Commons CLI命令行启动
今天又看了下Hangout的源码,一般来说一个开源项目有好几种启动方式--比如可以从命令行启动,也可以从web端启动.今天就看看如何设计命令行启动... Apache Commons CLI Apac ...
- golang常用库:cli命令行/应用程序生成工具-cobra使用
golang常用库:cli命令行/应用程序生成工具-cobra使用 一.Cobra 介绍 我前面有一篇文章介绍了配置文件解析库 Viper 的使用,这篇介绍 Cobra 的使用,你猜的没错,这 2 个 ...
随机推荐
- 高性能nosql ledisdb设计与实现 (2):replication
ledisdb现在已经支持replication机制,为ledisdb的高可用做出了保障. 使用 假设master的ip为10.20.187.100,端口6380,slave的ip为10.20.187 ...
- android 网络通信框架volly
1. 什么是Volley 在这之前,我们在程序中需要和网络通信的时候,大体使用的东西莫过于AsyncTaskLoader,HttpURLConnection,AsyncTask,HTTPClient( ...
- C语言中的内存分配
对于一个C语言程序而言,内存空间主要由以下几个部分组成: 1)程序代码区:用来存储程序的二进制代码 2)全局区/静态存储区 3)BSS段:用来存储未初始化的全局变量和静态变量. 4)栈区:存储局部变量 ...
- [C++学习历程]基础部分 C++中的类型和声明
前面搭起了C++的VS环境,可以在VS中编写C++代码了,也运行了最简单的一个程序Helloworld.那么我们该怎么才能写出功能强大的程序,怎样才能随心所欲的应用呢,那就需要重新回头来,从C++基础 ...
- pig代码格式上小注意
1,%default file test.txt 中不要用引号,'' 和""都不行.'file'不会被识别 2,pig判断相等,用==,不是一个=.. 3,pig中只用单引号,不用 ...
- Spring BeanFacoty doCreateBean方法分析
上一篇,我们分析到了doCreateBean,现在继续: 先看看时序图 protected Object doCreateBean(final String beanName, final RootB ...
- Git版本控制:Git分支处理
http://blog.csdn.net/pipisorry/article/details/46958699分支的意义创建分支可以避免提交代码后对主分支的影响,同时也使你有了相对独立的开发环境. 假 ...
- Leetcode_141_Linked List Cycle
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42833739 Given a linked list, d ...
- STL - miltimap(可重映射)
#include <iostream> #include <map> #include <string> using namespace std; //Multim ...
- 关于使用Xcode自带的单元测试UnitTest的介绍
什么是单元测试? 单元测试就是为你的方法专门多写一个测试函数.以保证你的方法在不停的修改开发中.保持正确.如果出错,第一时间让你知道,这样从最小单位开始监控来保证软件的质量. 什么时候用到单元测试: ...