2 Star 0 Fork 0

MJ PC Lab/goNixArgParser

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
option.go 3.25 KB
一键复制 编辑 原始数据 按行查看 历史
小小猫猫 提交于 2022-07-03 19:40 . feat(option): make option hideable
package goNixArgParser
import (
"io"
"strings"
)
func (opt *Option) isDelimiter(r rune) bool {
for _, delimiter := range opt.Delimiters {
if r == delimiter {
return true
}
}
return false
}
func (opt *Option) splitValues(input string) []string {
values := strings.FieldsFunc(input, opt.isDelimiter)
values = opt.filterValues(values)
return values
}
func (opt *Option) filterValues(values []string) []string {
if opt.UniqueValues {
uniqueValues := make([]string, 0, len(values))
uniqueValues = appendUnique(uniqueValues, values...)
return uniqueValues
}
return values
}
func NewFlagOption(key, flag, envVar, summary string) Option {
return Option{
Key: key,
Flags: []*Flag{NewSimpleFlag(flag)},
EnvVars: stringToSlice(envVar),
Summary: summary,
}
}
func NewFlagsOption(key string, flags []string, envVar, summary string) Option {
return Option{
Key: key,
Flags: NewSimpleFlags(flags),
EnvVars: stringToSlice(envVar),
Summary: summary,
}
}
func NewFlagValueOption(key, flag, envVar, defaultValue, summary string) Option {
return Option{
Key: key,
Flags: []*Flag{NewSimpleFlag(flag)},
AcceptValue: true,
OverridePrev: true,
EnvVars: stringToSlice(envVar),
DefaultValues: stringToSlice(defaultValue),
Summary: summary,
}
}
func NewFlagValuesOption(key, flag, envVar string, defaultValues []string, summary string) Option {
return Option{
Key: key,
Flags: []*Flag{NewSimpleFlag(flag)},
AcceptValue: true,
MultiValues: true,
UniqueValues: true,
EnvVars: stringToSlice(envVar),
DefaultValues: defaultValues,
Summary: summary,
}
}
func NewFlagsValueOption(key string, flags []string, envVar, defaultValue, summary string) Option {
return Option{
Key: key,
Flags: NewSimpleFlags(flags),
AcceptValue: true,
OverridePrev: true,
EnvVars: stringToSlice(envVar),
DefaultValues: stringToSlice(defaultValue),
Summary: summary,
}
}
func NewFlagsValuesOption(key string, flags []string, envVar string, defaultValues []string, summary string) Option {
return Option{
Key: key,
Flags: NewSimpleFlags(flags),
AcceptValue: true,
MultiValues: true,
UniqueValues: true,
EnvVars: stringToSlice(envVar),
DefaultValues: defaultValues,
Summary: summary,
}
}
func (opt *Option) OutputHelp(w io.Writer) {
if opt.Hidden {
return
}
newline := []byte{'\n'}
for i, flag := range opt.Flags {
if i > 0 {
w.Write([]byte{'|'})
}
io.WriteString(w, flag.Name)
}
if opt.AcceptValue {
io.WriteString(w, " <value>")
if opt.MultiValues {
io.WriteString(w, " ...")
}
}
w.Write(newline)
if len(opt.EnvVars) > 0 {
io.WriteString(w, "EnvVar: ")
for i, envVar := range opt.EnvVars {
if i > 0 {
io.WriteString(w, ", ")
}
io.WriteString(w, envVar)
}
w.Write(newline)
}
if len(opt.DefaultValues) > 0 {
io.WriteString(w, "Default: ")
for i, d := range opt.DefaultValues {
if i > 0 {
io.WriteString(w, ", ")
}
io.WriteString(w, d)
}
w.Write(newline)
}
if len(opt.Summary) > 0 {
io.WriteString(w, opt.Summary)
w.Write(newline)
}
if len(opt.Description) > 0 {
io.WriteString(w, opt.Description)
w.Write(newline)
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mjpclab/goNixArgParser.git
git@gitee.com:mjpclab/goNixArgParser.git
mjpclab
goNixArgParser
goNixArgParser
master

搜索帮助