Skip to content

Commit

Permalink
allow []string for argument.default as well as string
Browse files Browse the repository at this point in the history
  • Loading branch information
unRob committed Dec 21, 2021
1 parent 80a4c4c commit fc7c008
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
33 changes: 29 additions & 4 deletions internal/arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,19 @@ func (args *Arguments) Parse(supplied []string) {
argumentProvided := idx < len(supplied)

if !argumentProvided {
if arg.Default != "" {
arg.provided = &[]string{arg.Default}
if arg.Default != nil {
if arg.Variadic {
defaultSlice := []string{}
for _, valI := range arg.Default.([]interface{}) {
defaultSlice = append(defaultSlice, valI.(string))
}
arg.provided = &defaultSlice
} else {
defaultString := arg.Default.(string)
if defaultString != "" {
arg.provided = &[]string{defaultString}
}
}
}
continue
}
Expand Down Expand Up @@ -136,7 +147,7 @@ type Argument struct {
// Description is what this argument is for.
Description string `yaml:"description" validate:"required"`
// Default is the default value for this argument if none is provided.
Default string `yaml:"default" validate:"excluded_with=Required"`
Default interface{} `yaml:"default" validate:"excluded_with=Required"`
// Variadic makes an argument a list of all values from this one on.
Variadic bool `yaml:"variadic"`
// Required raises an error if an argument is not provided.
Expand All @@ -159,7 +170,7 @@ func (arg *Argument) IsKnown() bool {
}

func (arg *Argument) ToString(asShell bool) string {
value := arg.Default
var value string
if arg.IsKnown() {
if arg.Variadic {
if asShell {
Expand All @@ -175,6 +186,20 @@ func (arg *Argument) ToString(asShell bool) string {
vals := *arg.provided
value = vals[0]
}
} else {
if arg.Default != nil {
if arg.Variadic {
values := []string{}
for _, va := range arg.Default.([]interface{}) {
values = append(values, shellescape.Quote(va.(string)))
}
value = fmt.Sprintf("( %s )", strings.Join(values, " "))
} else {
value = arg.Default.(string)
}
} else {
value = ""
}
}

if !arg.Variadic && asShell {
Expand Down
4 changes: 3 additions & 1 deletion internal/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ func (opt *Option) ToString(asShell bool) string {
stringValue = strconv.FormatBool(value.(bool))
}
case "string":
stringValue = value.(string)
if value != nil {
stringValue = value.(string)
}
}

if asShell {
Expand Down

0 comments on commit fc7c008

Please sign in to comment.