lorem/main.go
2024-08-25 22:35:36 +02:00

67 lines
1.3 KiB
Go

package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/go-loremipsum/loremipsum"
)
func randomWords(generator *loremipsum.LoremIpsum, count int) string {
values := []string{}
for i := 0; i < count; i++ {
values = append(values, generator.Word())
}
return strings.Join(values, " ")
}
func removeFirstItem(value, separator, newSeparator string) string {
datas := strings.Split(value, separator)
return strings.Join(datas[1:], newSeparator)
}
func usage(cmd string) string {
return `Usage of ` + cmd + `:
-h Show help
-p int
Nomber of paragraphs
-s int
Nomber of sentences
-w int
Nomber of words
`
}
func main() {
var value string
wordFlag := flag.Int("w", 0, "Nomber of words")
paragraphFlag := flag.Int("p", 0, "Nomber of paragraphs")
sentenceFlag := flag.Int("s", 0, "Nomber of sentences")
helpFlag := flag.Bool("h", false, "Show help")
flag.Parse()
generator := loremipsum.New()
if *helpFlag {
fmt.Print(usage(os.Args[0]))
os.Exit(0)
}
if *wordFlag > 0 {
value = randomWords(generator, *wordFlag)
} else if *paragraphFlag > 0 {
value = removeFirstItem(generator.Paragraphs(*paragraphFlag+1), "\n", "\n\n")
} else if *sentenceFlag > 0 {
value = removeFirstItem(generator.Sentences(*sentenceFlag+1), ". ", ". ")
} else {
fmt.Fprint(os.Stderr, usage(os.Args[0]))
os.Exit(1)
}
fmt.Println(value)
}