Compare commits
No commits in common. "main" and "v0.0.1" have entirely different histories.
3 changed files with 15 additions and 86 deletions
35
LICENSE
35
LICENSE
|
@ -1,35 +0,0 @@
|
||||||
3-Clause BSD NON-AI License
|
|
||||||
|
|
||||||
Copyright 2025 The foilhatguy.casa domain name owner.
|
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without modification,
|
|
||||||
are permitted provided that the following conditions are met:
|
|
||||||
|
|
||||||
1. Redistributions of source code must retain the above copyright notice, this
|
|
||||||
list of conditions and the following disclaimer.
|
|
||||||
|
|
||||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
||||||
this list of conditions and the following disclaimer in the documentation and/or
|
|
||||||
other materials provided with the distribution.
|
|
||||||
|
|
||||||
3. Neither the name of the copyright holder nor the names of its contributors
|
|
||||||
may be used to endorse or promote products derived from this software without
|
|
||||||
specific prior written permission.
|
|
||||||
|
|
||||||
4. The source code and the binary form, and any modifications made to them may
|
|
||||||
not be used for the purpose of training or improving machine learning algorithms,
|
|
||||||
including but not limited to artificial intelligence, natural language processing,
|
|
||||||
or data mining. This condition applies to any derivatives, modifications, or
|
|
||||||
updates based on the Software code. Any usage of the source code or the binary
|
|
||||||
form in an AI-training dataset is considered a breach of this License.
|
|
||||||
|
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND
|
|
||||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
||||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
||||||
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
||||||
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
||||||
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
||||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
||||||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
|
||||||
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
||||||
OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
31
README.md
31
README.md
|
@ -1,31 +0,0 @@
|
||||||
### 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,38 +1,33 @@
|
||||||
|
// 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() {
|
||||||
isQuiet := detectFlag("q", "--quiet")
|
outputFlagSet := pflag.NewFlagSet("output", pflag.ContinueOnError)
|
||||||
isVerbose := detectFlag("V", "--verbose")
|
outputFlagSet.ParseErrorsWhitelist.UnknownFlags = true
|
||||||
|
outputFlagSet.SetOutput(nil)
|
||||||
|
|
||||||
SetupOutputMode(isQuiet, isVerbose)
|
isVerbose := outputFlagSet.BoolP("verbose", "V", false, "If set adds additional details to output.")
|
||||||
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
func detectFlag(shortFlag string, fullFlag string) bool {
|
SetupOutputMode(*isQuiet, *isVerbose)
|
||||||
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