从零实现一个命令行解析器

2020-02-17 12:33:22 +08:00
 guonaihong

出发点

分享下从零实现一个命令解析器的所有细节。

项目地址

https://github.com/guonaihong/clop

一个简单的例子(以 linux cat 命令举例)

package main

import (
	"fmt"
	"github.com/guonaihong/clop"
)

type cat struct {
	NumberNonblank bool `clop:"-c;--number-nonblank" 
	                     usage:"number nonempty output lines, overrides"`

	ShowEnds bool `clop:"-E;--show-ends" 
	               usage:"display $ at end of each line"`

	Number bool `clop:"-n;--number" 
	             usage:"number all output lines"`

	SqueezeBlank bool `clop:"-s;--squeeze-blank" 
	                   usage:"suppress repeated empty output lines"`

	ShowTab bool `clop:"-T;--show-tabs" 
	              usage:"display TAB characters as ^I"`

	ShowNonprinting bool `clop:"-v;--show-nonprinting" 
	                      usage:"use ^ and M- notation, except for LFD and TAB" `

	Files []string `clop:"args=files"`
}

func main() {

	c := cat{}
	err := clop.Bind(&c)

	fmt.Printf("%#v, %s\n", c, err)
}
2564 次点击
所在节点    Go 编程语言
5 条回复
guonaihong
2020-02-17 16:08:34 +08:00
看来我发消息,要有个强劲的大心脏。一不小心就没人看了,哈哈。
unicloud
2020-02-17 16:45:15 +08:00
确实有点尴尬啊~哈哈
guonaihong
2020-02-17 19:13:36 +08:00
@unicloud 这是在微笑在中掩示尴尬。。。哈哈
hijoker
2020-02-17 19:40:18 +08:00
加油,奥力给
guonaihong
2020-02-17 20:23:05 +08:00
@hijoker 恩,加油,奥力给是啥意思?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/645209

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX