string.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package pflag
  2. // -- string Value
  3. type stringValue string
  4. func newStringValue(val string, p *string) *stringValue {
  5. *p = val
  6. return (*stringValue)(p)
  7. }
  8. func (s *stringValue) Set(val string) error {
  9. *s = stringValue(val)
  10. return nil
  11. }
  12. func (s *stringValue) Type() string {
  13. return "string"
  14. }
  15. func (s *stringValue) String() string { return string(*s) }
  16. func stringConv(sval string) (interface{}, error) {
  17. return sval, nil
  18. }
  19. // GetString return the string value of a flag with the given name
  20. func (f *FlagSet) GetString(name string) (string, error) {
  21. val, err := f.getFlagType(name, "string", stringConv)
  22. if err != nil {
  23. return "", err
  24. }
  25. return val.(string), nil
  26. }
  27. // StringVar defines a string flag with specified name, default value, and usage string.
  28. // The argument p points to a string variable in which to store the value of the flag.
  29. func (f *FlagSet) StringVar(p *string, name string, value string, usage string) {
  30. f.VarP(newStringValue(value, p), name, "", usage)
  31. }
  32. // StringVarP is like StringVar, but accepts a shorthand letter that can be used after a single dash.
  33. func (f *FlagSet) StringVarP(p *string, name, shorthand string, value string, usage string) {
  34. f.VarP(newStringValue(value, p), name, shorthand, usage)
  35. }
  36. // StringVar defines a string flag with specified name, default value, and usage string.
  37. // The argument p points to a string variable in which to store the value of the flag.
  38. func StringVar(p *string, name string, value string, usage string) {
  39. CommandLine.VarP(newStringValue(value, p), name, "", usage)
  40. }
  41. // StringVarP is like StringVar, but accepts a shorthand letter that can be used after a single dash.
  42. func StringVarP(p *string, name, shorthand string, value string, usage string) {
  43. CommandLine.VarP(newStringValue(value, p), name, shorthand, usage)
  44. }
  45. // String defines a string flag with specified name, default value, and usage string.
  46. // The return value is the address of a string variable that stores the value of the flag.
  47. func (f *FlagSet) String(name string, value string, usage string) *string {
  48. p := new(string)
  49. f.StringVarP(p, name, "", value, usage)
  50. return p
  51. }
  52. // StringP is like String, but accepts a shorthand letter that can be used after a single dash.
  53. func (f *FlagSet) StringP(name, shorthand string, value string, usage string) *string {
  54. p := new(string)
  55. f.StringVarP(p, name, shorthand, value, usage)
  56. return p
  57. }
  58. // String defines a string flag with specified name, default value, and usage string.
  59. // The return value is the address of a string variable that stores the value of the flag.
  60. func String(name string, value string, usage string) *string {
  61. return CommandLine.StringP(name, "", value, usage)
  62. }
  63. // StringP is like String, but accepts a shorthand letter that can be used after a single dash.
  64. func StringP(name, shorthand string, value string, usage string) *string {
  65. return CommandLine.StringP(name, shorthand, value, usage)
  66. }