Skip to content

Commit

Permalink
update #5 长短选项都支持'='号
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Mar 9, 2020
1 parent e748860 commit 2e9fddd
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 74 deletions.
78 changes: 67 additions & 11 deletions clop.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,55 @@ func (c *Clop) setOption(name string, option *Option, m map[string]*Option, long
return nil
}

func unknownOptionErrorShort(optionName string) error {
return fmt.Errorf(`error: Found argument '-%s' which wasn't expected, or isn't valid in this context`,
optionName)
}
func unknownOptionError(optionName string) error {
return fmt.Errorf(`error: Found argument '--%s' which wasn't expected, or isn't valid in this context`,
optionName)
}

// 解析长选项
func (c *Clop) parseLong(arg string, index *int) error {
var option *Option
value := ""
option, _ = c.shortAndLong[arg]
if option == nil || len(arg) == 1 {
return fmt.Errorf(`error: Found argument '--%s' which wasn't expected, or isn't valid in this context`,
arg)
if option == nil {
pos := strings.Index(arg, "=")
if pos == -1 {
return unknownOptionError(arg)
}

option, _ = c.shortAndLong[arg[:pos]]
if option == nil {
return unknownOptionError(arg)
}
value = arg[pos+1:]
}

if len(arg) == 1 {
return unknownOptionError(arg)
}

value := ""
switch option.pointer.Kind() {
//bool类型,不考虑false的情况
case reflect.Bool:
value = "true"
if value == "" {
value = "true"
}
default:
_, isBoolSlice := option.pointer.Interface().([]bool)
if isBoolSlice {
return setBase("true", option.pointer)
if value == "" {
value = "true"
}

return setBase(value, option.pointer)
}

if len(value) > 0 {
return setBase(value, option.pointer)
}

// 如果是长选项
Expand Down Expand Up @@ -227,6 +258,10 @@ func (c *Clop) parseShort(arg string, index *int) error {

var a rune
find := false
// -d -d是bool类型
// -vvv 是[]bool类型
// -d=false -d 是bool false是value
// -ffile -f是string类型,file是value
for shortIndex, a = range arg {
//只支持ascii
if a >= utf8.RuneSelf {
Expand All @@ -236,30 +271,51 @@ func (c *Clop) parseShort(arg string, index *int) error {
optionName := string(byte(a))
option, _ = c.shortAndLong[optionName]
if option == nil {
return fmt.Errorf("error: Found argument '-%s' which wasn't expected, or isn't valid in this context", optionName)
return unknownOptionErrorShort(optionName)
}

find = true
//value := "" //TODO
findEqual := false
value := arg
_, isBoolSlice := option.pointer.Interface().([]bool)
_, isBool := option.pointer.Interface().(bool)
if !(isBoolSlice || isBool) {
shortIndex++
}

if len(value[shortIndex:]) > 0 && len(value[shortIndex+1:]) > 0 {
if value[shortIndex:][0] == '=' {
findEqual = true
shortIndex++
}

if value[shortIndex+1:][0] == '=' {
findEqual = true
shortIndex += 2
}
}

getchar:
for value := arg; ; {

if len(value[shortIndex:]) > 0 {
if len(value[shortIndex:]) > 0 { // 如果没有值,要取args下个参数
val := value[shortIndex:]
if isBoolSlice || isBool {
val = "true"
}

if findEqual {
val = string(value[shortIndex:])
}

if err := setBase(val, option.pointer); err != nil {
return err
}

if findEqual {
return nil
}

if isBoolSlice || isBool { //比如-vvv这种情况
break getchar
}
Expand Down Expand Up @@ -291,8 +347,7 @@ func (c *Clop) parseShort(arg string, index *int) error {
return nil
}

return fmt.Errorf(`error: Found argument '-%s' which wasn't expected, or isn't valid in this context`,
arg)
return unknownOptionErrorShort(arg)
}

func (c *Clop) getOptionAndSet(arg string, index *int, numMinuses int) error {
Expand Down Expand Up @@ -428,6 +483,7 @@ func (c *Clop) parseSubcommandTag(clop string, usage string) (newClop *Clop, hav

name := opt[len("subcommand="):]
newClop := New(nil)
//newClop.exit = c.exit //继承exit属性
newClop.SetProcName(name)
newClop.root = c.getRoot()
c.subcommand[name] = &Subcommand{Clop: newClop, usage: usage}
Expand Down
Loading

0 comments on commit 2e9fddd

Please sign in to comment.