Get rid from pflag dependency.
This commit is contained in:
parent
c0853ca918
commit
7a21684147
1 changed files with 18 additions and 15 deletions
31
ui.go
31
ui.go
|
@ -1,33 +1,36 @@
|
||||||
// This package provides --quiet and --verbose flags for cli and corresponding output print functions PrintOut and PrintVerbose.
|
|
||||||
// Another line
|
|
||||||
package ui
|
package ui
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"github.com/spf13/pflag"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var stdOutput io.Writer
|
var stdOutput io.Writer
|
||||||
var verboseOutput io.Writer
|
var verboseOutput io.Writer
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
outputFlagSet := pflag.NewFlagSet("output", pflag.ContinueOnError)
|
isQuiet := detectFlag("q", "--quiet")
|
||||||
outputFlagSet.ParseErrorsWhitelist.UnknownFlags = true
|
isVerbose := detectFlag("V", "--verbose")
|
||||||
outputFlagSet.SetOutput(nil)
|
|
||||||
|
|
||||||
isVerbose := outputFlagSet.BoolP("verbose", "V", false, "If set adds additional details to output.")
|
SetupOutputMode(isQuiet, isVerbose)
|
||||||
isQuiet := outputFlagSet.BoolP("quiet", "q", false, "If set makes no output at all.")
|
}
|
||||||
|
|
||||||
err := outputFlagSet.Parse(os.Args)
|
func detectFlag(shortFlag string, fullFlag string) bool {
|
||||||
if err != nil {
|
for _, arg := range os.Args {
|
||||||
fmt.Println(err)
|
return containFlag(arg, shortFlag) || arg == fullFlag
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
SetupOutputMode(*isQuiet, *isVerbose)
|
func containFlag(arg string, shortFlag string) bool {
|
||||||
|
if len(arg)>1 {
|
||||||
|
if arg[0] == "-"[0] && arg[1] != "-"[0] {
|
||||||
|
return strings.Contains(arg, shortFlag)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetupOutputMode(isQuiet bool, isVerbose bool) {
|
func SetupOutputMode(isQuiet bool, isVerbose bool) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue