Compare commits
6 commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
72db14ad2c | ||
![]() |
bd2c81aad1 | ||
![]() |
31cbc929d3 | ||
![]() |
7a21684147 | ||
![]() |
c0853ca918 | ||
![]() |
fa49d17486 |
2 changed files with 51 additions and 15 deletions
31
README.md
Normal file
31
README.md
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
### The "UI" package
|
||||||
|
|
||||||
|
This package provides --verbose and --quiet flag features to your cli.
|
||||||
|
It recognize both short and long flags. The package contains three functions:
|
||||||
|
- **PrintOut** to print regular messages unless --quiet flag is set.
|
||||||
|
- **PrintVerbose** to print messages only if --verbose flag is set, but --quiet
|
||||||
|
flag is not.
|
||||||
|
- **SetupOutputMode** to reconfigure output mode in the runtime.
|
||||||
|
|
||||||
|
-------------
|
||||||
|
|
||||||
|
### Usage
|
||||||
|
1. Download the package by **go get** command:
|
||||||
|
```shell
|
||||||
|
go get code.foilhatguy.casa/pub/ui
|
||||||
|
```
|
||||||
|
2. Import package to your golang code:
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"code.foilhatguy.casa/pub/ui"
|
||||||
|
)
|
||||||
|
```
|
||||||
|
There is no need for additional configuration. The package have its own **init**
|
||||||
|
function to detect --verbose and --quiet flags.
|
||||||
|
|
||||||
|
3. Use **PrintOut** and **PrintVerbose** functions to print
|
||||||
|
messages to the standard output:
|
||||||
|
```go
|
||||||
|
ui.PrintOut("Some regular message.")
|
||||||
|
ui.PrintVerbose("Some message printed only if --verbose flag is set.")
|
||||||
|
```
|
35
ui.go
35
ui.go
|
@ -1,33 +1,38 @@
|
||||||
// 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)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SetupOutputMode(*isQuiet, *isVerbose)
|
func detectFlag(shortFlag string, fullFlag string) bool {
|
||||||
|
for _, arg := range os.Args {
|
||||||
|
if containFlag(arg, shortFlag) || arg == fullFlag {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
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