本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.6KB

  1. // +build !windows forceposix
  2. package flags
  3. import (
  4. "strings"
  5. )
  6. const (
  7. defaultShortOptDelimiter = '-'
  8. defaultLongOptDelimiter = "--"
  9. defaultNameArgDelimiter = '='
  10. )
  11. func argumentStartsOption(arg string) bool {
  12. return len(arg) > 0 && arg[0] == '-'
  13. }
  14. func argumentIsOption(arg string) bool {
  15. if len(arg) > 1 && arg[0] == '-' && arg[1] != '-' {
  16. return true
  17. }
  18. if len(arg) > 2 && arg[0] == '-' && arg[1] == '-' && arg[2] != '-' {
  19. return true
  20. }
  21. return false
  22. }
  23. // stripOptionPrefix returns the option without the prefix and whether or
  24. // not the option is a long option or not.
  25. func stripOptionPrefix(optname string) (prefix string, name string, islong bool) {
  26. if strings.HasPrefix(optname, "--") {
  27. return "--", optname[2:], true
  28. } else if strings.HasPrefix(optname, "-") {
  29. return "-", optname[1:], false
  30. }
  31. return "", optname, false
  32. }
  33. // splitOption attempts to split the passed option into a name and an argument.
  34. // When there is no argument specified, nil will be returned for it.
  35. func splitOption(prefix string, option string, islong bool) (string, string, *string) {
  36. pos := strings.Index(option, "=")
  37. if (islong && pos >= 0) || (!islong && pos == 1) {
  38. rest := option[pos+1:]
  39. return option[:pos], "=", &rest
  40. }
  41. return option, "", nil
  42. }
  43. // addHelpGroup adds a new group that contains default help parameters.
  44. func (c *Command) addHelpGroup(showHelp func() error) *Group {
  45. var help struct {
  46. ShowHelp func() error `short:"h" long:"help" description:"Show this help message"`
  47. }
  48. help.ShowHelp = showHelp
  49. ret, _ := c.AddGroup("Help Options", "", &help)
  50. ret.isBuiltinHelp = true
  51. return ret
  52. }
上海开阖软件有限公司 沪ICP备12045867号-1