-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (65 loc) · 1.28 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
70
71
72
73
package main
import (
"bufio"
"fmt"
"github.com/alecthomas/kong"
"os"
"strings"
)
type cmd struct {
opts struct {
Args []string `arg:"" optional:""`
Precision uint `short:"p" default:"2" help:"Precision."`
Five bool `short:"5" help:"Give me five."`
}
}
func (c *cmd) run() {
kong.Parse(&c.opts,
kong.Name("hsize"),
kong.Description("hsize 123 383764 <OR> echo 19129219219129119 | hsize"),
kong.UsageOnError(),
)
if c.opts.Five {
fmt.Print("ヘ( ^o^)ノ\(^_^ )")
return
}
if c.opts.Precision > 0 {
prec = int(c.opts.Precision)
}
if len(c.opts.Args) > 0 {
for _, arg := range c.opts.Args {
c.parse(arg)
}
} else {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
c.parse(scanner.Text())
}
if scanner.Err() != nil {
exitf("error reading stdin: %s", scanner.Err())
}
}
}
func (c *cmd) parse(raw string) {
size, err := NewSizeNum().From(strings.TrimSpace(raw))
if err != nil {
fmt.Printf("NaN(%s)\n", raw)
return
}
var label string
for _, label = range units {
if size.Gte(scale) {
size.Div1024()
} else {
break
}
}
fmt.Printf("%s%s\n", size, label)
}
func exitf(format string, a ...interface{}) {
fmt.Fprintln(os.Stderr, fmt.Sprintf(format, a...))
os.Exit(-2)
}
func main() {
new(cmd).run()
}