Skip to content
This repository has been archived by the owner on Feb 17, 2020. It is now read-only.

可以设置所有的类型后面 不需要跟值 #9

Closed
guonaihong opened this issue Jun 13, 2019 · 1 comment
Closed

可以设置所有的类型后面 不需要跟值 #9

guonaihong opened this issue Jun 13, 2019 · 1 comment

Comments

@guonaihong
Copy link
Owner

guonaihong commented Jun 13, 2019

经常有这么一个场景,如果设置-0 或者设置-null 命令行选项 得到 '\0'输出。否则使用'\n'输出。

现在要实现效果的做法

var openNull bool
delimiter := byte('\n')
 command.Opt("0, null", "end each output line with NUL, not newline").
                Flags(flag.Posix).
                Var(&openNull)
if openNull {
    delimiter = byte(0)
}

假如设置 byte类型 不需要后面跟值

delimiter := byte(0)
command.Opt("0, null", "end each output line with NUL, not newline").
                Flags(flag.Posix).
                MatchVar(&delimiter, '\n')

MatchVar的语意与DefaultVar是截然不同的
MatchVar函数如果发现注册的选项,则使用MatchVar函数第二个参数的值。
DefaultVar函数如果没有选项匹配,则使用DefaultVar函数第二个参数的值。

@guonaihong
Copy link
Owner Author

guonaihong commented Jun 22, 2019

done #11

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant