Skip to content

I think the option flags usage description is wrong on StringArrayVar and StringSliceVar #10427

Closed

Description

@jemygraw

func addValueOptionsFlags(f *pflag.FlagSet, v *values.Options) {

func addValueOptionsFlags(f *pflag.FlagSet, v *values.Options) {
	f.StringSliceVarP(&v.ValueFiles, "values", "f", []string{}, "specify values in a YAML file or a URL (can specify multiple)")
	f.StringArrayVar(&v.Values, "set", []string{}, "set values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
	f.StringArrayVar(&v.StringValues, "set-string", []string{}, "set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)")
	f.StringArrayVar(&v.FileValues, "set-file", []string{}, "set values from respective files specified via the command line (can specify multiple or separate values with commas: key1=path1,key2=path2)")
}
package main

import (
   "encoding/json"
   "fmt"
   "github.com/spf13/cobra"
)

func GetJson(obj interface{}) string {
   data, _ := json.Marshal(&obj)
   return string(data)
}

var names []string
var hobbys []string

func main() {
   cmd := cobra.Command{Run: func(cmd *cobra.Command, args []string) {
      fmt.Println("names", GetJson(names))
      fmt.Println("hobbys", GetJson(hobbys))
   }}

   cmd.PersistentFlags().StringArrayVar(&names, "names", nil, "names")
   cmd.PersistentFlags().StringSliceVar(&hobbys, "hobbys", nil, "")
   cmd.Execute()
}

Test Cases:

./viper --names jemy,dolphin --hobbys walk,books
names ["jemy,dolphin"]
hobbys ["walk","books"]

./viper --names jemy --names dolphin --hobbys walk --hobbys books
names ["jemy","dolphin"]
hobbys ["walk","books"]

From what we can see, StringArray does not support separate values with commas, you must use multiple flags to get multiple values. On the contrary, StringSlice support both multiple flags and separate values with commas.

To support my views, you can also check the comment of StringArray.

// StringArrayVar defines a string flag with specified name, default value, and usage string.
// The argument p points to a []string variable in which to store the values of the multiple flags.
// The value of each argument will not try to be separated by comma. Use a StringSlice for that.
func (f *FlagSet) StringArrayVar(p *[]string, name string, value []string, usage string) {
   f.VarP(newStringArrayValue(value, p), name, "", usage)
}

Activity

  1. mattfarina commented on Dec 3, 2021

    @mattfarina
    Collaborator

    This is a good question and great observation. What's happening is not obvious.

    The variables assigned by StringArrayVar here are parsed by the strvals package. This package knows how to parse and handle the comma separation.

  2. jemygraw commented on Dec 6, 2021

    @jemygraw
    Author

    @mattfarina Thanks for your help and comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions