-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.go
55 lines (46 loc) · 956 Bytes
/
utility.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
package main
import (
"fmt"
"os"
"strconv"
"strings"
)
const (
// characters that are used for trimming while parsing the input list of
// triangle sides
TrimCharachters = ", ;"
)
const (
UsageOutput = `go-triangle is the utility to determine a type of the triangle
Usage: go-triangle [options] <a_side> <b_side> <c_side>
Options:
-v parameter to verbose low level error messages
Example: go-triangle 10 20 15
`
)
const (
PrefixErrorMsg = `ERROR`
)
func usage() {
fmt.Printf(UsageOutput)
}
func exitWithError(msg string) {
printErr(msg)
usage()
os.Exit(2)
}
func printErr(msg string) {
fmt.Printf("%s: %s\n", PrefixErrorMsg, msg)
}
func parseArgs(args []string) ([]float64, error) {
var sides []float64
for i := range args {
sideStr := strings.Trim(args[i], TrimCharachters)
sideFloat, err := strconv.ParseFloat(sideStr, 64)
if err != nil {
return nil, err
}
sides = append(sides, sideFloat)
}
return sides, nil
}