Skip to content

Commit

Permalink
up: allow to disable global options by GOpts().SetDisable(). issues#42
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 13, 2022
1 parent 19fb234 commit b59c90b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
12 changes: 8 additions & 4 deletions _examples/cliapp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ import (
"github.com/gookit/gcli/v3"
"github.com/gookit/gcli/v3/_examples/cmd"
"github.com/gookit/gcli/v3/builtin"

// "github.com/gookit/gcli/v3/builtin/filewatcher"
// "github.com/gookit/gcli/v3/builtin/reverseproxy"
)

// local run:
// go run ./_examples/cliapp
// go build ./_examples/cliapp && ./cliapp
//
// go run ./_examples/cliapp
// go build ./_examples/cliapp && ./cliapp
//
// run on windows(cmd, powerShell):
// go build ./_examples/cliapp && ./cliapp
//
// go build ./_examples/cliapp && ./cliapp
func main() {
app := gcli.NewApp(func(app *gcli.App) {
app.Version = "3.0.0"
Expand All @@ -36,6 +37,9 @@ func main() {
/_/ /_/`
})

// disable global options
// gcli.GOpts().SetDisable()

var customGOpt string
app.GOptsBinder = func(gf *gcli.Flags) {
// gcli.Logf(gcli.VerbInfo, "custom add and global option flag")
Expand Down
23 changes: 18 additions & 5 deletions gcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
// Contains: cli app, flags parse, interact, progress, data show tools.
//
// Source code and other details for the project are available at GitHub:
// https://github.com/gookit/gcli
//
// https://github.com/gookit/gcli
//
// Usage please refer examples and see README
package gcli
Expand All @@ -26,6 +27,8 @@ const (
CommandSep = ":"
// HelpCommand name
HelpCommand = "help"
// VerbEnvName for set gcli debug level
VerbEnvName = "GCLI_VERBOSE"
)

// constants for error level (quiet 0 - 5 crazy)
Expand Down Expand Up @@ -89,7 +92,7 @@ var (
// init
func init() {
// set verbose from ENV var.
envVerb := os.Getenv("GCLI_VERBOSE")
envVerb := os.Getenv(VerbEnvName)
if envVerb != "" {
_ = gOpts.verbose.Set(envVerb)
}
Expand Down Expand Up @@ -155,6 +158,7 @@ type Commander interface {

// GOptions global flag options
type GOptions struct {
Disable bool
NoColor bool
verbose VerbLevel // message report level
showVer bool
Expand All @@ -179,7 +183,7 @@ type GOptions struct {

func newDefaultGOptions() *GOptions {
return &GOptions{
strictMode: true,
strictMode: false,
// init error level.
verbose: DefaultVerb,
}
Expand Down Expand Up @@ -210,12 +214,21 @@ func (g *GOptions) NoProgress() bool {
return g.noProgress
}

// SetDisable global options
func (g *GOptions) SetDisable() {
g.Disable = true
}

func (g *GOptions) bindingFlags(fs *Flags) {
fs.BoolOpt(&g.showHelp, "help", "h", false, "Display the help information")

if g.Disable {
return
}

// up: allow use int and string.
fs.VarOpt(&g.verbose, "verbose", "", "Set logs reporting level(quiet 0 - 5 crazy)")

fs.BoolOpt(&g.inShell, "ishell", "", false, "Run in an interactive shell environment(`TODO`)")
fs.BoolOpt(&g.showHelp, "help", "h", false, "Display the help information")
fs.BoolOpt(&g.NoColor, "no-color", "nc", g.NoColor, "Disable color when outputting message")
fs.BoolOpt(&g.noProgress, "no-progress", "np", g.noProgress, "Disable display progress message")
fs.BoolOpt(&g.noInteractive, "no-interactive", "ni", g.noInteractive, "Disable interactive confirmation operation")
Expand Down

0 comments on commit b59c90b

Please sign in to comment.