package main
import (
"fmt""github.com/guonaihong/clop"
)
typeteststruct {
A []int`clop:"-a;greedy" usage:"test array"`Bint`clop:"-b" usage:"test int"`
}
funcmain() {
a:=&test{}
clop.Bind(a)
fmt.Printf("%#v\n", a)
}
/*运行./use_array -a 12 34 56 78 -b 100输出&main.test{A:[]int{12, 34, 56, 78}, B:100}*/
required flag
package main
import (
"github.com/guonaihong/clop"
)
typecurlstruct{
Urlstring`clop:"-u; --url" usage:"url" valid:"required"`
}
funcmain() {
c:=curl{}
clop.Bind(&c)
}
// ./required // error: -u; --url must have a value!// For more information try --help
package main
import (
"github.com/guonaihong/clop"
)
typeOncestruct {
Debugbool`clop:"-d; --debug; once" usage:"debug mode"`
}
funcmain() {
o:=Once{}
clop.Bind(&o)
}
/*./once -debug -debugerror: The argument '-d' was provided more than once, but cannot be used multiple timesFor more information try --help*/
quick write
快速写法,通过使用固定的 short, long tag 生成短,长选项。可以和cat例子直观比较下。命令行选项越多,越能节约时间,提升效率。
package main
import (
"fmt""github.com/guonaihong/clop"
)
typecatstruct {NumberNonblankbool`clop:"-c;long" usage:"number nonempty output lines, overrides"`ShowEndsbool`clop:"-E;long" usage:"display $ at end of each line"`Numberbool`clop:"-n;long" usage:"number all output lines"`SqueezeBlankbool`clop:"-s;long" usage:"suppress repeated empty output lines"`ShowTabbool`clop:"-T;long" usage:"display TAB characters as ^I"`ShowNonprintingbool`clop:"-v;long" usage:"use ^ and M- notation, except for LFD and TAB" `Files []string`clop:"args=files"`
}
funcmain() {
c:=cat{}
err:=clop.Bind(&c)
fmt.Printf("%#v, %s\n", c, err)
}
package main
import (
"fmt""github.com/guonaihong/clop"
)
typecatstruct {NumberNonblankbool`clop:"-c;--number-nonblank" usage:"number nonempty output lines, overrides"`ShowEndsbool`clop:"-E;--show-ends" usage:"display $ at end of each line"`Numberbool`clop:"-n;--number" usage:"number all output lines"`SqueezeBlankbool`clop:"-s;--squeeze-blank" usage:"suppress repeated empty output lines"`ShowTabbool`clop:"-T;--show-tabs" usage:"display TAB characters as ^I"`ShowNonprintingbool`clop:"-v;--show-nonprinting" usage:"use ^ and M- notation, except for LFD and TAB" `Files []string`clop:"args=files"`
}
funcmain() {
c:=cat{}
err:=clop.Bind(&c)
fmt.Printf("%#v, %s\n", c, err)
}
/*Usage: ./cat [Flags] <files> 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: <files>*/
clop v0.2.6,Go 纯结构体(struct)命令行解析器
clop
clop (Command Line Option Parse) 是一款基于 struct 的命令行解析器,麻雀虽小,五脏俱全 (从零实现)。
feature
env DEBUG=xx ./proccat a.txt b.txt,可以把a.txt, b.txt散装成员归归类,收集到你指定的结构体成员里proc -d或者长选项proc --debug不在话下ls -ltr是ls -l -t -r简写形式,方便实现普通 posix 标准命令subcommand) 支持,方便实现 git 风格子命令git add,简洁的子命令注册方式,只要会写结构体就行,3,4,5 到无穷尽子命令也支持,只要你喜欢,用上 clop 就可以实现default:"1",支持多种数据类型,让你省去类型转换的烦恼if x!= ""orif y!=0浪费青春的代码内容
Installation
Quick start
example
base type
int
float64
duration
string
array
similar to curl command
similar to join command
加上 greedy 属性,就支持数组贪婪写法。类似 join 命令。
required flag
set default value
可以使用 default tag 设置默认值,普通类型直接写,复合类型用 json 表示
Support environment variables
custom environment variable name
Quick writing of environment variables
使用 env tag 会根据结构体名,生成一个环境变量名,规则就是驼峰命令名,改成大写下划线
subcommand
Sub command implementation method 1
Sub command implementation method 2
使用 clop 实现子命令的第 2 种做法,子命令结构体只要实现
SubMain方法,该方法 clop 库会帮你自动调用。省去在 main 里面写一堆 if else 判断 (相对方法 1 来说), 特别是子命令特别多的情况,推荐用这种方法.Get command priority
Can only be set once
指定选项只能被设置一次,如果命令行选项,使用两次则会报错。
quick write
快速写法,通过使用固定的 short, long tag 生成短,长选项。可以和 cat 例子直观比较下。命令行选项越多,越能节约时间,提升效率。
Multi structure series
多结构体串联功能。多结构体统一组成一个命令行视图
如果命令行解析是要怼到多个 (>=2) 结构体里面,可以使用结构体串联功能,前面几个结构体使用
clop.Register()接口,最后一个结构体使用clop.Bind()函数.Advanced features
高级功能里面有一些 clop 包比较有特色的功能
Parsing flag code to generate clop code
让你爽翻天,如果你的 command 想迁移至 clop, 但是面对众多的 flag 代码,又不想花费太多时间在无谓的人肉 code 转换上,这时候你就需要 clop 命令,一行命令解决你的痛点.
1. 安装 clop 命令
2. 使用 clop 解析包含 flag 包的代码
就可以把 main.go 里面的 flag 库转成 clop 包的调用方式
main.go代码如下输出代码如下
Implementing linux command options
cat