mirror of
https://github.com/spf13/cobra
synced 2025-05-07 22:07:23 +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 (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -205,3 +206,18 @@ func stringInSlice(a string, list []string) bool {
|
||||||
}
|
}
|
||||||
return false
|
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