-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactor the gflag parse to sub-pkg /gflag
- Loading branch information
Showing
10 changed files
with
186 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package helper | ||
package gflag | ||
|
||
import ( | ||
"fmt" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package gflag | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/gookit/gcli/v3/helper" | ||
"github.com/gookit/goutil/arrutil" | ||
"github.com/gookit/goutil/comdef" | ||
"github.com/gookit/goutil/strutil" | ||
) | ||
|
||
func sepStr(seps []string) string { | ||
if len(seps) > 0 { | ||
return seps[0] | ||
} | ||
return comdef.DefaultSep | ||
} | ||
|
||
// allowed keys on struct tag. | ||
var flagTagKeys = arrutil.Strings{"name", "shorts", "desc", "default", "required"} | ||
|
||
// parse tag named k-v value. item split by ';' | ||
// | ||
// eg: "name=int0;shorts=i;required=true;desc=int option message" | ||
// | ||
// supported field name: | ||
// | ||
// name | ||
// desc | ||
// shorts | ||
// required | ||
// default | ||
func parseNamedRule(name, rule string) (mp map[string]string) { | ||
ss := strutil.Split(rule, ";") | ||
if len(ss) == 0 { | ||
return | ||
} | ||
|
||
mp = make(map[string]string, len(flagTagKeys)) | ||
for _, s := range ss { | ||
if strings.ContainsRune(s, '=') == false { | ||
helper.Panicf("parse tag error on field '%s': item must match `KEY=VAL`", name) | ||
} | ||
|
||
kvNodes := strings.SplitN(s, "=", 2) | ||
key, val := kvNodes[0], strings.TrimSpace(kvNodes[1]) | ||
if !flagTagKeys.Has(key) { | ||
helper.Panicf("parse tag error on field '%s': invalid key name '%s'", name, key) | ||
} | ||
|
||
mp[key] = val | ||
} | ||
return | ||
} | ||
|
||
// ParseSimpleRule struct tag value use simple rule. each item split by ';' | ||
// | ||
// - format: "desc;required;default;shorts" | ||
// | ||
// eg: | ||
// | ||
// "int option message;required;i" | ||
// "int option message;;a,b" | ||
// "int option message;;a,b;23" | ||
// | ||
// returns field name: | ||
// | ||
// name | ||
// desc | ||
// shorts | ||
// required | ||
// default | ||
func ParseSimpleRule(name, rule string) (mp map[string]string) { | ||
ss := strutil.SplitNTrimmed(rule, ";", 4) | ||
ln := len(ss) | ||
if ln == 0 { | ||
return | ||
} | ||
|
||
mp = make(map[string]string, ln) | ||
mp["desc"] = ss[0] | ||
if ln == 1 { | ||
return | ||
} | ||
|
||
required := ss[1] | ||
if required == "required" { | ||
required = "true" | ||
} | ||
|
||
mp["required"] = required | ||
|
||
// has shorts and default | ||
if ln > 3 { | ||
mp["default"], mp["shorts"] = ss[2], ss[3] | ||
} else if ln > 2 { | ||
mp["default"] = ss[2] | ||
} | ||
return | ||
} |
Oops, something went wrong.