Compare commits

..

7 commits
v0.0.1 ... main

Author SHA1 Message Date
Foil-hat-guy
72db14ad2c
Fixed bug in os.Args iteration. 2025-04-21 00:25:22 +03:00
Foil-hat-guy
bd2c81aad1
Add some more logs. 2025-04-21 00:19:48 +03:00
Foil-hat-guy
31cbc929d3
Added extra log for debuging. 2025-04-21 00:05:36 +03:00
Foil-hat-guy
7a21684147
Get rid from pflag dependency. 2025-04-20 23:57:39 +03:00
Foil-hat-guy
c0853ca918
Fixed typo in README.md 2025-04-20 23:46:27 +03:00
Foil-hat-guy
fa49d17486
README.md file is added. 2025-04-20 23:44:39 +03:00
Foil-hat-guy
b4960cb421
Licence file is added. BSD-3 Non-AI. 2025-04-16 19:36:52 +03:00
3 changed files with 86 additions and 15 deletions

35
LICENSE Normal file
View 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
View 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.")
```

33
ui.go
View file

@ -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) {