Various fixes to CLI parsing

This commit is contained in:
Kovid Goyal
2022-09-23 17:17:32 +05:30
parent 79cfc1e70a
commit 04022ed363
3 changed files with 46 additions and 35 deletions

View File

@@ -117,15 +117,18 @@ func option_from_spec(spec OptionSpec) (*Option, error) {
ans.Depth = spec.Depth
if spec.Choices != "" {
parts := strings.Split(spec.Choices, ",")
ans.Choices = make(map[string]bool, len(parts))
ans.OptionType = StringOption
for i, x := range parts {
x = strings.TrimSpace(x)
ans.Choices[x] = true
if i == 0 && ans.Default == "" {
ans.Default = x
if len(parts) == 1 {
parts = strings.Split(spec.Choices, " ")
} else {
for i, x := range parts {
parts[i] = strings.TrimSpace(x)
}
}
ans.Choices = parts
ans.OptionType = StringOption
if ans.Default == "" {
ans.Default = parts[0]
}
} else {
switch spec.Type {
case "choice", "choices":
@@ -151,7 +154,7 @@ func option_from_spec(spec OptionSpec) (*Option, error) {
case "list":
ans.IsList = true
fallthrough
case "str", "string":
case "str", "string", "":
ans.OptionType = StringOption
default:
return nil, fmt.Errorf("Unknown option type: %s", spec.Type)