Initial commit.
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"gitlab.wow.st/gmp/rand"
|
||||
)
|
||||
|
||||
func usage() {
|
||||
fmt.Println("rand [-d | -l | -s] [num]")
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
func main() {
|
||||
args := os.Args[1:]
|
||||
var f func() (byte,error)
|
||||
var n int
|
||||
var err error
|
||||
|
||||
func() {
|
||||
if len(args) == 0 {
|
||||
f = rand.Char
|
||||
n = 20
|
||||
return
|
||||
}
|
||||
if len(args) >= 1 {
|
||||
switch args[0] {
|
||||
case "-d","--digits":
|
||||
f = rand.Digit
|
||||
case "-l","--letters":
|
||||
f = rand.Letter
|
||||
case "-s","--symbols":
|
||||
f = rand.Symbol
|
||||
default:
|
||||
f = rand.Char
|
||||
n, err = strconv.Atoi(args[0])
|
||||
if err != nil {
|
||||
usage()
|
||||
}
|
||||
}
|
||||
}
|
||||
switch len(args) {
|
||||
case 1:
|
||||
if n == 0 {
|
||||
n = 20
|
||||
}
|
||||
case 2:
|
||||
if n != 0 {
|
||||
usage()
|
||||
}
|
||||
n, err = strconv.Atoi(args[1])
|
||||
if err != nil {
|
||||
usage()
|
||||
}
|
||||
default:
|
||||
usage()
|
||||
}
|
||||
}()
|
||||
|
||||
s, err := rand.Slice(f,n)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
os.Stdout.Write(s)
|
||||
os.Stdout.Write([]byte("\n"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user