yes.go 867 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* Yes program implementation. */
  2. package yes
  3. import (
  4. "os"
  5. "fmt"
  6. "strings"
  7. "surdeus.su/util/bb/input"
  8. "surdeus.su/core/cli/mtool"
  9. )
  10. var (
  11. nArg int
  12. )
  13. func yes(s string) {
  14. if nArg < 0 {
  15. for {
  16. fmt.Print(s)
  17. }
  18. } else {
  19. for i := 0; i < nArg; i += 1 {
  20. fmt.Print(s)
  21. }
  22. }
  23. }
  24. func Run(flagSet *mtool.Flags) {
  25. var (
  26. stdinFlag bool
  27. nFlag bool
  28. s string
  29. )
  30. flagSet.BoolVar(&stdinFlag, "s", false, "Read string from stdin.")
  31. flagSet.BoolVar(&nFlag, "n", false, "Do not add net line character.")
  32. flagSet.IntVar(&nArg, "N", -1, "Repeat input N times. Negative value means infinite cycle.")
  33. flagSet.Parse()
  34. args := flagSet.Args()
  35. if stdinFlag {
  36. in, _ := input.ReadAllRaw(os.Stdin)
  37. s = string(in)
  38. } else {
  39. if len(args) > 0 {
  40. s = strings.Join(args, " ")
  41. } else {
  42. s = "y"
  43. }
  44. }
  45. if !nFlag {
  46. s += "\n"
  47. }
  48. yes(s)
  49. }