您现在的位置: 首页 > 网站导航收录 > 百科知识百科知识
(lnt是什么意思)-lolnt是什么意思
命令,参数,命令行(lnt是什么意思)-lolnt是什么意思
发布时间:2020-12-06加入收藏来源:互联网点击:
所谓启发式的探测,是指当解析到 red参数时,我们并不知道 red到底是子命令(或者子命令的前缀部分),还是子命令的参数,因此我们可以将其假定为子命令的前缀进行匹配,如果匹配不到,则将其当做子命令参数处理。
解析到 red时,用 echo red搜索预定义的子命令,若搜索不到,则将 red视为参数解析 times时,用 echo times搜索预定义的子命令,此时可搜索到 echo times子命令可以看到 red不需区分是 --color的标识参数,还是子命令的非标识参数,只要其匹配不到任何子命令,则可以确认,其一定是子命令的参数。
3 子命令任意书写顺序
子命令本质上就是一个字符串,我们上面的启发式解析已经实现将任意子命令字符串识别出来,前提是预先对这个字符串进行定义。也就是将这个字符串关联到某个函数。这样的设计使得父命令、子命令只是逻辑上的概念,而跟具体的代码实现毫无关联,我们需要做的就是调整映射而已。
维护映射关系
# 关联到 echoTimes 函数"echo times" = echoTimes# 调整子命令只是改一下这个映射而已"times echo" = echoTimes五 Cortana: 基于启发式命令行解析的实现为了实现上述思路,我开发了 Cortana这个项目。Cortana 引入 Btree 建立子命令与函数之间的映射关系,得益于其前缀搜索的能力,用户输入任意子命令前缀,程序都会自动列出所有可用的子命令。启发式命令行解析机制,可以在解析具体的标识或子命令参数前,先解析出子命令,从而搜索到子命令所映射的函数,在映射的函数中,去真正的解析子命令的参数,实现变量的绑定。另外,Cortana 充分利用了 Go 语言 Struct Tag 的特,简化了变量绑定的流程。
我们用 cortana 重新实现 cobra 代码的功能
package mainimport ( "fmt" "strings" "github.com/shafreeck/cortana")func print() { cortana.Title("Print anything to the screen") cortana.Description(`print is for printing anything back to the screen.For many years people have printed back to the screen.`) args := struct { Texts []string `cortana:"texts"` }{} cortana.Parse(&args) fmt.Println(strings.Join(args.Texts, " "))}func echo() { cortana.Title("Echo anything to the screen") cortana.Description(`echo is for echoing anything back. Echo works a lot like print, except it has a child command.`) args := struct { Texts []string `cortana:"texts"` }{} cortana.Parse(&args) fmt.Println(strings.Join(args.Texts, " "))}func echoTimes() { cortana.Title("Echo anything to the screen more times") cortana.Description(`echo things multiple times back to the user by providing a count and a string.`) args := struct { Times int `cortana:"--times, -t, 1, times to echo the input"` Texts []string `cortana:"texts"` }{} cortana.Parse(&args) for i := 0; i args.Times; i++ { fmt.Println(strings.Join(args.Texts, " ")) }}func main() { cortana.AddCommand("print", print, "print anything to the screen") cortana.AddCommand("echo", echo, "echo anything to the screen") cortana.AddCommand("echo times", echoTimes, "echo anything to the screen more times") cortana.Launch()}命令用法跟 cobra 完全一样,只是自动生成的帮助信息有一些区别
# 不加任何子命令,输出自动生成的帮助信息$ ./appAvailable commands:print print anything to the screenecho echo anything to the screenecho times echo anything to the screen more times# 默认启用 -h, --help 选项,开发者无需做任何事情$ ./app print -hPrint anything to the screenprint is for printing anything back to the screen.For many years people have printed back to the screen.Usage: print [texts...] -h, --help help for the command # echo 任意内容$ ./app echo hello world hello world # echo 任意次数$ ./app echo times hello world --times 3 hello world hello world hello world# --times 参数可以在任意位置$ ./app echo --times 3 times hello world hello world hello world hello world1 选项与默认值
args := struct { Times int `cortana:"--times, -t, 1, times to echo the input"` Texts []string `cortana:"texts"`}{}可以看到, echo times 命令有一个 --times 标识,另外,则是要回显的内容,内容本质上也是命令行参数,并且可能因为内容中有空格,而被分割为多个参数。
我们上面提到,标识本质上是将某个值绑定到某个变量,标识的名字,比如这里的 --times,跟变量 args.Times 关联,那么对于非标识的其他参数呢,这些参数是没有名字的,因此我们统一绑定到一个 Slice,也就是 args.Texts
Cortana 定义了属于自己的 Struct Tag,分别用来指定其长标识名、短标识名,默认值和这个选项的描述信息。其格式为: cortana:"long, short, default, description"
长标识名(long): --flagname, 任意标识都支持长标识名的格式,如果不写,则默认用字段名短标识名(short): -f,可以省略默认值(default):可以为任意跟字段类型匹配的值,如果省略,则默认为空值,如果为单个横线 "-",则标识用户必须提供一个值描述(description):这个选项的描述信息,用于生成帮助信息,描述中可以包含任意可打印字符(包括逗号和空格)为了便于记忆,cortana这个 Tag 名字也可以写为 lsdd,即上述四部分的英文首字母。
2 子命令与别名
AddCommond 可以添加任意子命令,其本质上是建立子命令与其处理函数的映射关系。
cortana.AddCommand("echo", echo, "echo anything to the screen")在这个例子里,print命令和 echo命令是相同的,我们其实可以通过别名的方式将两者关联
// 定义 print 为 echo 命令的别名cortana.Alias("print", "echo")执行 print 命令实际上执行的是 echo
$ ./app print -hEcho anything to the screenecho is for echoing anything back. Echo works a lot like print, except it has a child command.Available commands:echo times echo anything to the screen more timesUsage: echo [texts...] -h, --help help for the command别名的机制非常灵活,可以为任意命令和参数设置别名,比如我们期望实现 three这个子命令,打印任意字符串 3 次。可以直接通过别名的方式实现:
cortana.Alias("three", "echo times --times 3")# three 是 echo times --times 3 的别名$ ./app three hello world hello world hello world hello world3 help 标识和命令
Cortana 自动为任意命令生成帮助信息,这个行为也可以通过 cortana.DisableHelpFlag禁用,也可以通过 cortana.HelpFlag来设定自己喜欢的标识名。
cortana.Use(cortana.HelpFlag("--usage", "-u"))# 自定义 --usage 来打印帮助信息$ ./app echo --usageEcho anything to the screenecho is for echoing anything back. Echo works a lot like print, except it has a child command.Available commands:echo times echo anything to the screen more timesUsage: echo [texts...] -u, --usage help for the commandCortana 默认并没有提供 help子命令,但利用别名的机制,我们自己很容易实现 help命令。
cortana.Alias("help", "--help")// 通过别名,实现 help 命令,用于打印任意子命令的帮助信息$ ./app help echo timesEcho anything to the screen more timesecho things multiple times back to the user by providing a count and a string.Usage: echo times [options] [texts...] -t, --times times times to echo the input. (default=1) -h, --help help for the command下一篇:返回列表
相关链接 |
||
网友回复(共有 0 条回复) |