1package examples
2
3import (
4 "fmt"
5 "os"
6 "strings"
7)
8
9// CheckArgs should be used to ensure the right command line arguments are
10// passed before executing an example.
11func CheckArgs(arg ...string) {
12 if len(os.Args) < len(arg)+1 {
13 Warning("Usage: %s %s", os.Args[0], strings.Join(arg, " "))
14 os.Exit(1)
15 }
16}
17
18// CheckIfError should be used to naively panics if an error is not nil.
19func CheckIfError(err error) {
20 if err == nil {
21 return
22 }
23
24 fmt.Printf("\x1b[31;1m%s\x1b[0m\n", fmt.Sprintf("error: %s", err))
25 os.Exit(1)
26}
27
28// Info should be used to describe the example commands that are about to run.
29func Info(format string, args ...interface{}) {
30 fmt.Printf("\x1b[34;1m%s\x1b[0m\n", fmt.Sprintf(format, args...))
31}
32
33// Warning should be used to display a warning
34func Warning(format string, args ...interface{}) {
35 fmt.Printf("\x1b[36;1m%s\x1b[0m\n", fmt.Sprintf(format, args...))
36}