optstyle_windows.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // +build !forceposix
  2. package flags
  3. import (
  4. "strings"
  5. )
  6. // Windows uses a front slash for both short and long options. Also it uses
  7. // a colon for name/argument delimter.
  8. const (
  9. defaultShortOptDelimiter = '/'
  10. defaultLongOptDelimiter = "/"
  11. defaultNameArgDelimiter = ':'
  12. )
  13. func argumentStartsOption(arg string) bool {
  14. return len(arg) > 0 && (arg[0] == '-' || arg[0] == '/')
  15. }
  16. func argumentIsOption(arg string) bool {
  17. // Windows-style options allow front slash for the option
  18. // delimiter.
  19. if len(arg) > 1 && arg[0] == '/' {
  20. return true
  21. }
  22. if len(arg) > 1 && arg[0] == '-' && arg[1] != '-' {
  23. return true
  24. }
  25. if len(arg) > 2 && arg[0] == '-' && arg[1] == '-' && arg[2] != '-' {
  26. return true
  27. }
  28. return false
  29. }
  30. // stripOptionPrefix returns the option without the prefix and whether or
  31. // not the option is a long option or not.
  32. func stripOptionPrefix(optname string) (prefix string, name string, islong bool) {
  33. // Determine if the argument is a long option or not. Windows
  34. // typically supports both long and short options with a single
  35. // front slash as the option delimiter, so handle this situation
  36. // nicely.
  37. possplit := 0
  38. if strings.HasPrefix(optname, "--") {
  39. possplit = 2
  40. islong = true
  41. } else if strings.HasPrefix(optname, "-") {
  42. possplit = 1
  43. islong = false
  44. } else if strings.HasPrefix(optname, "/") {
  45. possplit = 1
  46. islong = len(optname) > 2
  47. }
  48. return optname[:possplit], optname[possplit:], islong
  49. }
  50. // splitOption attempts to split the passed option into a name and an argument.
  51. // When there is no argument specified, nil will be returned for it.
  52. func splitOption(prefix string, option string, islong bool) (string, string, *string) {
  53. if len(option) == 0 {
  54. return option, "", nil
  55. }
  56. // Windows typically uses a colon for the option name and argument
  57. // delimiter while POSIX typically uses an equals. Support both styles,
  58. // but don't allow the two to be mixed. That is to say /foo:bar and
  59. // --foo=bar are acceptable, but /foo=bar and --foo:bar are not.
  60. var pos int
  61. var sp string
  62. if prefix == "/" {
  63. sp = ":"
  64. pos = strings.Index(option, sp)
  65. } else if len(prefix) > 0 {
  66. sp = "="
  67. pos = strings.Index(option, sp)
  68. }
  69. if (islong && pos >= 0) || (!islong && pos == 1) {
  70. rest := option[pos+1:]
  71. return option[:pos], sp, &rest
  72. }
  73. return option, "", nil
  74. }
  75. // addHelpGroup adds a new group that contains default help parameters.
  76. func (c *Command) addHelpGroup(showHelp func() error) *Group {
  77. // Windows CLI applications typically use /? for help, so make both
  78. // that available as well as the POSIX style h and help.
  79. var help struct {
  80. ShowHelpWindows func() error `short:"?" description:"Show this help message"`
  81. ShowHelpPosix func() error `short:"h" long:"help" description:"Show this help message"`
  82. }
  83. help.ShowHelpWindows = showHelp
  84. help.ShowHelpPosix = showHelp
  85. ret, _ := c.AddGroup("Help Options", "", &help)
  86. ret.isBuiltinHelp = true
  87. return ret
  88. }