-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
69 lines (56 loc) · 1.09 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"os"
"path/filepath"
"strconv"
"github.com/arifmahmudrana/rand-str/rand"
)
const (
strSet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-~!@#$%^&*()+={}[]\\|;:'\",<.>/?"
size = 12
)
func isHelp(args []string) bool {
if len(args) > 0 && (args[0] == "-h" || args[0] == "--help") {
return true
}
return false
}
func getHelp(p string) string {
return "usage: " + p + " <string_len> <string_set>\n"
}
func parseArgs(args []string) (siz int, str string, err error) {
str = strSet
siz = size
if len(args) > 0 {
if args[0] != "" {
i, err := strconv.Atoi(args[0])
if err != nil {
return siz, str, err
}
siz = i
}
if len(args) > 1 && args[1] != "" {
str = args[1]
}
}
return siz, str, nil
}
func main() {
base := filepath.Base(os.Args[0])
args := os.Args[1:]
if isHelp(args) {
print(getHelp(base))
os.Exit(0)
}
siz, str, err := parseArgs(args)
if err != nil {
print(getHelp(base))
os.Exit(1)
}
r, err := rand.RandomString(siz, str)
if err != nil {
print("error: " + err.Error())
os.Exit(1)
}
print(r)
}