string.go 2.9 KB

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