使用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 个 ...
随机推荐
- 【项目管理】 PMBOK 基础概念 (引论 PMBOK 笔记)
好紧张, 3月28考试, 全力学 PMP ~~ 一. 项目 1. 项目的定义 项目 : 项目是为创造 独特 的 产品, 服务 或 成果 而进行的 临时性 工作; -- 独特 : 独特性是项目的特征, ...
- 【一天一道LeetCode】#112. Path Sum
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 程序员需要有多懒 ?- cocos2d-x 数学函数、常用宏粗整理
原帖地址:http://www.cnblogs.com/buaashine/archive/2012/11/12/2765691.html 1.注意这是cocos2d-x中的函数,但大体上和cocos ...
- Libgdx 1.6.0发布,跨平台游戏开发框架
[1.6.0] -英文原文:http://www.badlogicgames.com/wordpress/?p=3682 -API更改:GlyphLayout xAdvances现在有了额外的开始入口 ...
- TCP的ACK确认系列 — 延迟确认
主要内容:TCP的延迟确认.延迟确认定时器的实现. 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd 延迟确认模式 发送方在发送数据包时,如果发送的数据包有 ...
- ConcurrentHashMap和HashTable的区别
hashtable是做了同步的,hashmap未考虑同步.所以hashmap在单线程情况下效率较高.hashtable在的多线程情况下,同步操作能保证程序执行的正确性. 但是hashtable每次同步 ...
- Rust语言之HelloWorld
Rust语言之HelloWorld 参考文档: http://doc.crates.io/guide.html 1 什么是Cargo 相当于maven/ant之于java, automake之于c, ...
- android数据保存之greendao
有时我们的数据属于保存到数据库,对于Android应用和IOS应用,我们一般都会使用SQLite这个嵌入式的数据库作为我们保存数据的工具.由于我们直接操作数据库比较麻烦,而且管理起来也非常的麻烦,以前 ...
- C语言颜色转换宏
C语言颜色转换宏 #define COLOR_BPP16_RGB555 /* Win RGB */ #define COLOR_RGB(r,g,b) ((COLORREF)(((BYTE)(r)|(( ...
- Learning ROS for Robotics Programming Second Edition学习笔记(二) indigo tools
中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS for Robotics Pr ...