Compare commits
7 commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
72db14ad2c | ||
![]() |
bd2c81aad1 | ||
![]() |
31cbc929d3 | ||
![]() |
7a21684147 | ||
![]() |
c0853ca918 | ||
![]() |
fa49d17486 | ||
![]() |
b4960cb421 |
3 changed files with 86 additions and 15 deletions
35
LICENSE
Normal file
35
LICENSE
Normal file
|
@ -0,0 +1,35 @@
|
|||
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
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
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/pflag"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var stdOutput io.Writer
|
||||
var verboseOutput io.Writer
|
||||
|
||||
func init() {
|
||||
outputFlagSet := pflag.NewFlagSet("output", pflag.ContinueOnError)
|
||||
outputFlagSet.ParseErrorsWhitelist.UnknownFlags = true
|
||||
outputFlagSet.SetOutput(nil)
|
||||
isQuiet := detectFlag("q", "--quiet")
|
||||
isVerbose := detectFlag("V", "--verbose")
|
||||
|
||||
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.")
|
||||
SetupOutputMode(isQuiet, isVerbose)
|
||||
}
|
||||
|
||||
err := outputFlagSet.Parse(os.Args)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
func detectFlag(shortFlag string, fullFlag string) bool {
|
||||
for _, arg := range os.Args {
|
||||
if containFlag(arg, shortFlag) || arg == fullFlag {
|
||||
return true
|
||||
}
|
||||
}
|
||||
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) {
|
||||
|
|
Loading…
Add table
Reference in a new issue