Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

args.go 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright (c) 2016-2017 Daniel Oaks <daniel@danieloaks.net>
  2. // released under the MIT license
  3. package utils
  4. import (
  5. "errors"
  6. "strings"
  7. )
  8. var (
  9. ErrInvalidParams = errors.New("Invalid parameters")
  10. )
  11. // ArgsToStrings takes the arguments and splits them into a series of strings,
  12. // each argument separated by delim and each string bounded by maxLength.
  13. func ArgsToStrings(maxLength int, arguments []string, delim string) []string {
  14. var messages []string
  15. var buffer string
  16. for {
  17. if len(arguments) < 1 {
  18. break
  19. }
  20. if len(buffer) > 0 && maxLength < len(buffer)+len(delim)+len(arguments[0]) {
  21. messages = append(messages, buffer)
  22. buffer = ""
  23. continue
  24. }
  25. if len(buffer) > 1 {
  26. buffer += delim
  27. }
  28. buffer += arguments[0]
  29. arguments = arguments[1:]
  30. }
  31. if len(buffer) > 0 {
  32. messages = append(messages, buffer)
  33. }
  34. return messages
  35. }
  36. func StringToBool(str string) (result bool, err error) {
  37. switch strings.ToLower(str) {
  38. case "on", "true", "t", "yes", "y":
  39. result = true
  40. case "off", "false", "f", "no", "n":
  41. result = false
  42. default:
  43. err = ErrInvalidParams
  44. }
  45. return
  46. }