mirror of
https://github.com/spf13/cobra
synced 2025-05-07 13:57:21 +00:00
add helpers: 'er', 'Er' and 'WrStringAndCheck', use 'io.StringWriter'
This commit is contained in:
parent
44c8ae4518
commit
7b62ee10c2
1 changed files with 16 additions and 0 deletions
16
cobra.go
16
cobra.go
|
@ -19,6 +19,7 @@ package cobra
|
|||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -205,3 +206,18 @@ func stringInSlice(a string, list []string) bool {
|
|||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func er(msg interface{}) {
|
||||
if msg != nil {
|
||||
fmt.Println("Error:", msg)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// Er prints the msg and exits with error code 1, unless the msg is nil
|
||||
func Er(msg interface{}) { er(msg) }
|
||||
|
||||
func WrStringAndCheck(b io.StringWriter, s string) {
|
||||
_, err := b.WriteString(s)
|
||||
er(err)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue