Skip to content

Commit a48fa6f

Browse files
committed
Using stdlib flag instead of third-party dependencies
1 parent a2d2176 commit a48fa6f

File tree

3 files changed

+22
-30
lines changed

3 files changed

+22
-30
lines changed

cmd/sulid/main.go

+22-26
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package main
22

33
import (
44
cryptorand "crypto/rand"
5+
"flag"
56
"fmt"
67
mathrand "math/rand"
78
"os"
89
"strings"
910
"time"
1011

11-
getopt "github.com/pborman/getopt/v2"
1212
"github.com/xray-bit/sulid"
1313
)
1414

@@ -17,30 +17,26 @@ const (
1717
rfc3339ms = "2006-01-02T15:04:05.000Z07:00"
1818
)
1919

20-
func main() {
21-
// Completely obnoxious.
22-
getopt.HelpColumn = 50
23-
getopt.DisplayWidth = 140
20+
var (
21+
formatFlag string
22+
localFlag bool
23+
quickFlag bool
24+
zeroFlag bool
25+
)
2426

25-
fs := getopt.New()
26-
var (
27-
format = fs.StringLong("format", 'f', "default", "when parsing, show times in this format: default, rfc3339, unix, ms", "<format>")
28-
local = fs.BoolLong("local", 'l', "when parsing, show local time instead of UTC")
29-
quick = fs.BoolLong("quick", 'q', "when generating, use non-crypto-grade entropy")
30-
zero = fs.BoolLong("zero", 'z', "when generating, fix entropy to all-zeroes")
31-
help = fs.BoolLong("help", 'h', "print this help text")
32-
)
33-
if err := fs.Getopt(os.Args, nil); err != nil {
34-
fmt.Fprintf(os.Stderr, "%v\n", err)
35-
os.Exit(1)
36-
}
37-
if *help {
38-
fs.PrintUsage(os.Stderr)
39-
os.Exit(0)
40-
}
27+
func init() {
28+
flag.StringVar(&formatFlag, "f", "default", "when parsing, show times in this format: default, rfc3339, unix, ms")
29+
flag.BoolVar(&localFlag, "l", false, "when parsing, show local time instead of UTC")
30+
flag.BoolVar(&quickFlag, "q", false, "when generating, use non-crypto-grade entropy")
31+
flag.BoolVar(&zeroFlag, "z", false, "when generating, fix entropy to all-zeroes")
32+
}
33+
34+
func main() {
35+
flag.Parse()
36+
args := flag.Args()
4137

4238
var formatFunc func(time.Time) string
43-
switch strings.ToLower(*format) {
39+
switch strings.ToLower(formatFlag) {
4440
case "default":
4541
formatFunc = func(t time.Time) string { return t.Format(defaultms) }
4642
case "rfc3339":
@@ -50,15 +46,15 @@ func main() {
5046
case "ms":
5147
formatFunc = func(t time.Time) string { return fmt.Sprint(t.UnixNano() / 1e6) }
5248
default:
53-
fmt.Fprintf(os.Stderr, "invalid --format %s\n", *format)
49+
fmt.Fprintf(os.Stderr, "invalid -f %s\n", formatFlag)
5450
os.Exit(1)
5551
}
5652

57-
switch args := fs.Args(); len(args) {
53+
switch len(args) {
5854
case 0:
59-
generate(*quick, *zero)
55+
generate(quickFlag, zeroFlag)
6056
default:
61-
parse(args[0], *local, formatFunc)
57+
parse(args[0], localFlag, formatFunc)
6258
}
6359
}
6460

go.mod

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
module github.com/xray-bit/sulid
22

33
go 1.23
4-
5-
require github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30 h1:BHT1/DKsYDGkUgQ2jmMaozVcdk+sVfz0+1ZJq4zkWgw=
2-
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=

0 commit comments

Comments
 (0)