在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子命令,它有articlestweet两个命令:

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的命令行应用的更多相关文章

  1. CLI:使用Go开发命令行应用

      原文地址 CLI或者"command line interface"是用户在命令行下交互的程序.由于通过将程序编译到一个静态文件中来减少依赖,一次Go特别适合开发CLI程序.如 ...

  2. php cli模式学习(PHP命令行模式)

    http://www.jb51.net/article/37796.htm php_cli模式简介  php-cli是php Command Line Interface的简称,如同它名字的意思,就是 ...

  3. php在cli模式下取得命令行中的参数的方法-getopt命令行可传递数组-简单自定义方法取命令行参数

    在cli模式下执行PHP时,自动给脚本文件传递了一个变量$argv,其值即是一个命令中所有值组成的数组(以空格区分),在PHP程序中接收参数有3种方法1.直接使用argv变量数组. 2.使用$_SER ...

  4. Azure CLI 2.0-Azure新命令行工具介绍

    Azure CLI 2.0 是 Azure 的新命令行体验,用于管理 Azure 资源. 可以将其安装在 macOS.Linux 和 Windows 上,然后从命令行运行它. Azure CLI 2. ...

  5. 4 个用于构建优秀的命令行用户界面的 Python 库

    作者: Amjith Ramanujam 译者: LCTT Lv Feng 在这个分为两篇的关于具有绝佳命令行界面的终端程序的系列文章的第二篇教程中,我们将讨论 Prompt.Toolkit.Clic ...

  6. 构建一个Flowable命令行应用

    官网链接 [(https://flowable.com/open-source/docs/bpmn/ch02-GettingStarted/#building-a-command-line-appli ...

  7. C++/cli中swtich处理命令行接收到的关键字

    QQ群友中有人提出这样一个问题: Swtich接受的是整形或枚举类型, 关键字多数是一个字符, 将string转换成char就应该可以, 所以我试着写了一下代码, 直接提取string的第一个字符, ...

  8. Apache Commons CLI命令行启动

    今天又看了下Hangout的源码,一般来说一个开源项目有好几种启动方式--比如可以从命令行启动,也可以从web端启动.今天就看看如何设计命令行启动... Apache Commons CLI Apac ...

  9. golang常用库:cli命令行/应用程序生成工具-cobra使用

    golang常用库:cli命令行/应用程序生成工具-cobra使用 一.Cobra 介绍 我前面有一篇文章介绍了配置文件解析库 Viper 的使用,这篇介绍 Cobra 的使用,你猜的没错,这 2 个 ...

随机推荐

  1. 安卓banner图片轮播

    之前写过一篇关于首页图片广告轮播的demo:http://blog.csdn.net/baiyuliang2013/article/details/45740091,不过图片轮播的指示器(小白点)处操 ...

  2. 如何在Cocos2D 1.0 中掩饰一个精灵(五)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 掩饰和CCRenderTexture CCRenderTextu ...

  3. XML解析之JAXP案例详解

    根据一个CRUD的案例,对JAXP解析xml技术,进行详细的解释: 首先,已知一个xml文件中的数据如下: <?xml version="1.0" encoding=&quo ...

  4. JavaScript进阶(十)Array 数组详解

    JS array 数组详解 数组的声明方法 arrayObj = new Array(); 的数组 ,并且第一位是5 数组的运算(传地址) var t2=new Array(); t2[0]=1; t ...

  5. PKU 3468 A Simple Problem with Integers

    题目大意: 有N,M两个数 Q 表示查询, 后面两个数a b,表示查询从a 到b计算它们的和 C 表示增加   后面三个数a,b,c 表示从a开始,一直到b,每个数都增加c 除了查询要进行输出,增加不 ...

  6. Git版本控制:Git查阅、撤销文件修改和撤销文件追踪

    http://blog.csdn.net/pipisorry/article/details/47867097 查看文件的修改历史 git log --pretty=oneline 文件名 # 显示修 ...

  7. map.js的编写(js编写一个对象的方式)

    // 定义map function Map() { this.container = {}; } // 将key-value放入map中 Map.prototype.put = function(ke ...

  8. ceres-solver库使用示例

    上一篇博客大致说明了下ceres-solver库的编译,然后形成了一个二次开发的库,下面就是用这个二次开发库来写一个简单(其实不太简单)的DEMO来演示ceres-solver库的强大.我们以求解一个 ...

  9. 【网站建设】Linux上安装MySQL - 12条命令搞定MySql

    从零开始安装mysql数据库 : 按照该顺序执行 :  a. 查看是否安装有mysql:yum list installed mysql*, 如果有先卸载掉, 然后在进行安装; b. 安装mysql客 ...

  10. 关于C++“加、减机制”的整理

    今天上C++的课,杨老师提到C++继承是“加机制”的,而没有像人类进化一样采取的是“减机制”,这样会导致代码的膨胀和冗余.回来后,特地查阅了一下资料,发现这方面的文章很少. 下边的资料摘自网上及杨老师 ...