-
Notifications
You must be signed in to change notification settings - Fork 0
/
capit.go
68 lines (56 loc) · 1.22 KB
/
capit.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
// capitalise from stdin to stdout
// SvM 29-DEC-2020 - 27-JUN-2022
//
// nicked from https://gobyexample.com/line-filters
// and from https://pkg.go.dev/golang.org/x/text@v0.3.6/cases
package main
import (
"bufio"
"flag"
"fmt"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"log"
"os"
"path"
)
func b2i(b bool) int8 {
if b {
return 1
}
return 0
}
func main() {
moi := path.Base(os.Args[0])
lg := log.New(os.Stderr, moi+": ", 0)
var u, l, c bool
var lng string
flag.BoolVar(&u, "u", false, "convert to uppercase")
flag.BoolVar(&l, "l", false, "convert to lowercase")
flag.BoolVar(&c, "c", false, "capitalise (default)")
flag.StringVar(&lng, "L", "en", "select language")
flag.Parse()
lngtag := language.Make(lng)
if x := b2i(u) + b2i(l) + b2i(c); x == 0 {
c = true
} else if x != 1 {
lg.Printf("use one and only one of -u, -l or -c\n")
os.Exit(1)
}
var cs cases.Caser
if u {
cs = cases.Upper(lngtag)
} else if l {
cs = cases.Lower(lngtag)
} else if c {
cs = cases.Title(lngtag)
}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
fmt.Println(cs.String(scanner.Text()))
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
}