bb/tool/yes/yes.go

59 lines
867 B
Go
Raw Permalink Normal View History

2020-03-14 01:44:50 +03:00
/* Yes program implementation. */
package yes
2023-12-04 13:49:01 +03:00
import (
2020-03-14 01:44:50 +03:00
"os"
"fmt"
"strings"
2024-05-15 21:07:35 +03:00
"surdeus.su/util/bb/input"
"surdeus.su/core/cli/mtool"
2020-03-14 01:44:50 +03:00
)
2023-12-04 13:49:01 +03:00
var (
2020-05-03 03:59:39 +03:00
nArg int
)
2020-03-14 01:44:50 +03:00
2023-12-04 13:49:01 +03:00
func yes(s string) {
if nArg < 0 {
for {
2020-05-03 03:59:39 +03:00
fmt.Print(s)
}
} else {
2023-12-04 13:49:01 +03:00
for i := 0; i < nArg; i += 1 {
2020-05-03 03:59:39 +03:00
fmt.Print(s)
}
2020-03-14 01:44:50 +03:00
}
}
2023-03-24 16:54:51 +03:00
func Run(flagSet *mtool.Flags) {
2023-12-04 13:49:01 +03:00
var (
2020-03-14 01:44:50 +03:00
stdinFlag bool
2023-12-04 13:49:01 +03:00
nFlag bool
s string
2020-03-14 01:44:50 +03:00
)
flagSet.BoolVar(&stdinFlag, "s", false, "Read string from stdin.")
flagSet.BoolVar(&nFlag, "n", false, "Do not add net line character.")
2020-05-03 03:59:39 +03:00
flagSet.IntVar(&nArg, "N", -1, "Repeat input N times. Negative value means infinite cycle.")
2023-12-04 13:49:01 +03:00
2023-03-24 16:54:51 +03:00
flagSet.Parse()
args := flagSet.Args()
2020-03-14 01:44:50 +03:00
if stdinFlag {
in, _ := input.ReadAllRaw(os.Stdin)
s = string(in)
2020-03-14 01:44:50 +03:00
} else {
2023-12-04 13:49:01 +03:00
if len(args) > 0 {
2020-03-14 01:44:50 +03:00
s = strings.Join(args, " ")
} else {
s = "y"
}
}
if !nFlag {
s += "\n"
}
yes(s)
}