歡迎您光臨本站 註冊首頁

clop v0.0.2 版本發布,golang 實現的命令行解析庫

←手機掃碼閱讀     admin @ 2020-03-30 , reply:0

changlog

  • #32 fix 使用slice變數,默認進入貪婪模式

地址

https://github.com/guonaihong/clop

https://gitee.com/guonaihong/clop

clop

clop 是一款基於struct的命令行解析器,麻雀雖小,五臟俱全。(從零實現)

feature

  • 支持環境變數綁定 env DEBUG=xx ./proc

  • 支持參數搜集 cat a.txt b.txt,可以把a.txt, b.txt散裝成員歸歸類,收集到你指定的結構體成員里

  • 支持短選項proc -d 或者長選項proc --debug不在話下

  • posix風格命令行支持,支持命令組合ls -ltrls -l -t -r簡寫形式,方便實現普通posix 標準命令

  • 子命令支持,方便實現git風格子命令git add ,簡潔的子命令註冊方式,只要會寫結構提就行,3,4,5到無窮盡子命令也支持,只要你喜歡,用上clop就可以實現

  • 默認值支持default:"1",支持多種數據類型,讓你省去類型轉換的煩惱

  • 貼心的重複命令報錯

  • 嚴格的短選項,長選項報錯。避免二義性選項誕生

  • 效驗模式支持,不需要寫一堆的if x!= "" or if y!=0浪費青春的代碼

內容

Installation

go get github.com/guonaihong/clop

Quick start

package mainimport (	"fmt""github.com/guonaihong/clop")type Hello struct {	File string `clop:"-f; --file" usage:"file"`}func main() {

	h := Hello{}
	clop.Bind(&h)
	fmt.Printf("%#v\n", h)
}// ./one -f test// main.Hello{File:"test"}// ./one --file test// main.Hello{File:"test"}

example

required flag

 package mainimport ("fmt""github.com/guonaihong/clop")type curl struct {Url string `clop:"-u; --url" usage:"url" valid:"required"`}func main() {

    c := curl{}
    clop.Bind(&c)
}

set default value

可以使用default tag設置默認值,普通類型直接寫,複合類型用json表示

package mainimport ("fmt""github.com/guonaihong/clop")type defaultExample struct {Int          int       `default:"1"`Float64      float64   `default:"3.64"`Float32      float32   `default:"3.32"`SliceString  []string  `default:"[\"one\", \"two\"]"`SliceInt     []int     `default:"[1,2,3,4,5]"`SliceFloat64 []float64 `default:"[1.1,2.2,3.3,4.4,5.5]"`}func main() {
    de := defaultExample{}
    clop.Bind(&de)
    fmt.Printf("%v\n", de) 
}// run//         ./use_def// output://         {1 3.64 3.32 [one two] [1 2 3 4 5] [1.1 2.2 3.3 4.4 5.5]}

Support environment variables

// file name use_env.gopackage mainimport (	"fmt""github.com/guonaihong/clop")type env struct {	OmpNumThread string `clop:"env=omp_num_thread" usage:"omp num thread"`Path         string `clop:"env=XPATH" usage:"xpath"`Max          int    `clop:"env=MAX" usage:"max thread"`}func main() {
	e := env{}
	clop.Bind(&e)
	fmt.Printf("%#v\n", e)
}// run// env XPATH=`pwd` omp_num_thread=3 MAX=4 ./use_env // output// main.env{OmpNumThread:"3", Path:"/home/guo", Max:4}

subcommand

package mainimport (	"fmt""github.com/guonaihong/clop")type add struct {	All      bool     `clop:"-A; --all" usage:"add changes from all tracked and untracked files"`Force    bool     `clop:"-f; --force" usage:"allow adding otherwise ignored files"`Pathspec []string `clop:"args=pathspec"`}type mv struct {	Force bool `clop:"-f; --force" usage:"allow adding otherwise ignored files"`}type git struct {	Add add `clop:"subcommand=add" usage:"Add file contents to the index"`Mv  mv  `clop:"subcommand=mv" usage:"Move or rename a file, a directory, or a symlink"`}func main() {
	g := git{}
	clop.Bind(&g)
	fmt.Printf("git:%#v\n", g)
	fmt.Printf("git:set mv(%t) or set add(%t)\n", clop.IsSetSubcommand("mv"), clop.IsSetSubcommand("add"))
}// run:// ./git add -f// output:// git:main.git{Add:main.add{All:false, Force:true, Pathspec:[]string(nil)}, Mv:main.mv{Force:false}}// git:set mv(false) or set add(true)

Implementing linux command options

cat

package mainimport (	"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)
}/*Usage:    ./cat [Flags]Flags:    -E,--show-ends           display $ at end of each line     -T,--show-tabs           display TAB characters as ^I     -c,--number-nonblank     number nonempty output lines, overrides     -n,--number              number all output lines     -s,--squeeze-blank       suppress repeated empty output lines     -v,--show-nonprinting    use ^ and M- notation, except for LFD and TAB Args:    */

[admin ]

來源:OsChina
連結:https://www.oschina.net/news/114480/clop-0-0-2-released
clop v0.0.2 版本發布,golang 實現的命令行解析庫已經有235次圍觀

http://coctec.com/news/all/show-post-228661.html