Is there a way to define a default value for a flag (VarP) in the cobra?
func (f *FlagSet) VarPF(value Value, name, shorthand, usage string) *Flag {
// Remember the default value as a string; it won't change.
flag := &Flag{
Name: name,
Shorthand: shorthand,
Usage: usage,
Value: value,
DefValue: value.String(),
}
f.AddFlag(flag)
return flag
}
I could not find a default value in the function definition.
There are several ways — it's unclear what's been tried from the question.
FlagSethelper methods. For example,FlagSet.StringVarPFlagSet.AddFlagwhich takes aFlag. This type has a field for default value.FlagSet.Varand related methods, by implementing a custom type with that interface. If you go this route, you're responsible for defining what a "default value" is. TheValueinterface provides a method toSeta value and output what the current value is. If you want a default value, return it when nothing has been set in your custom type.