1package main
 2
 3import (
 4	"fmt"
 5	"math/rand"
 6	"os"
 7	"strconv"
 8	"strings"
 9	"time"
10)
11
12var (
13	length  = 20
14	version = ""
15)
16
17func main() {
18	if len(os.Args) > 1 && os.Args[1] == "-h" {
19		printHelp()
20		os.Exit(0)
21	} else if len(os.Args) > 3 {
22		fmt.Print("Error: too many arguments...\n\n")
23		printHelp()
24		os.Exit(1)
25	} else if len(os.Args) == 2 {
26		// Convert the string argument to an int
27		length, err := strconv.Atoi(os.Args[1])
28		if err != nil {
29			fmt.Println("Error: length must be an integer")
30			os.Exit(1)
31		}
32		fmt.Println(generate(length))
33		os.Exit(0)
34	} else if len(os.Args) == 3 {
35		length, err := strconv.Atoi(os.Args[1])
36		if err != nil {
37			fmt.Println("Error: length must be an integer")
38			os.Exit(1)
39		}
40		count, err := strconv.Atoi(os.Args[2])
41		if err != nil {
42			fmt.Println("Error: count must be an integer")
43			os.Exit(1)
44		}
45		for i := 0; i < count; i++ {
46			fmt.Println(generate(length))
47		}
48		os.Exit(0)
49	} else if len(os.Args) == 1 {
50		fmt.Println(generate(length))
51		os.Exit(0)
52	}
53}
54
55func generate(length int) string {
56	const newBase60Chars = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ_abcdefghijkmnopqrstuvwxyz"
57
58	rng := rand.New(rand.NewSource(time.Now().UnixNano()))
59
60	var sb strings.Builder
61	sb.Grow(length)
62
63	for i := 0; i < length; i++ {
64		sb.WriteByte(newBase60Chars[rng.Intn(len(newBase60Chars))])
65	}
66
67	return sb.String()
68}
69
70func printHelp() {
71	fmt.Println(`Usage: eow [-h] <length> <count>
72
73-h prints this help message
74
75Arguments are positional, so length AND count
76may be omitted OR count may be omitted. If
77specifying count, length must also be
78specified. When ommitted, length defaults to
7920 and count defaults to 1.`)
80}