Skip to content

Commit

Permalink
up: update the option shorts validate and save logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 6, 2022
1 parent 623e065 commit 1f64ca6
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 151 deletions.
4 changes: 2 additions & 2 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type core struct {
HelpVars
// global options flag set
gFlags *Flags
// GOptsBinder you can custom binding global options
// GOptsBinder you can be custom binding global options
GOptsBinder func(gf *Flags)
}

Expand Down Expand Up @@ -186,7 +186,6 @@ func (h *Hooks) Fire(event string, data ...interface{}) (stop bool) {
if handler, ok := h.hooks[event]; ok {
return handler(data...)
}

return false
}

Expand Down Expand Up @@ -302,6 +301,7 @@ func (c *cmdLine) hasHelpKeywords() bool {
*************************************************************/

// HelpVarFormat allow var replace on render help info.
//
// Default support:
// "{$binName}" "{$cmd}" "{$fullCmd}" "{$workDir}"
const HelpVarFormat = "{$%s}"
Expand Down
4 changes: 1 addition & 3 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -803,9 +803,7 @@ func (c *Command) Errorf(format string, v ...interface{}) error {
}

// NewErr format message and add error to the command
func (c *Command) NewErr(msg string) error {
return errors.New(msg)
}
func (c *Command) NewErr(msg string) error { return errors.New(msg) }

// NewErrf format message and add error to the command
func (c *Command) NewErrf(format string, v ...interface{}) error {
Expand Down
11 changes: 5 additions & 6 deletions gargs.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package gcli

import (
"fmt"
"strconv"
"strings"

"github.com/gookit/goutil/errorx"
"github.com/gookit/goutil/strutil"
)

Expand Down Expand Up @@ -62,7 +62,7 @@ func (ags *Arguments) ParseArgs(args []string) (err error) {
num = i + 1
if num > inNum { // not enough args
if arg.Required {
return fmt.Errorf("must set value for the argument: %s(position#%d)", arg.ShowName, arg.index)
return errorx.Rawf("must set value for the argument: %s(position#%d)", arg.ShowName, arg.index)
}
break
}
Expand All @@ -81,7 +81,7 @@ func (ags *Arguments) ParseArgs(args []string) (err error) {
}

if ags.validateNum && inNum > num {
return fmt.Errorf("entered too many arguments: %v", args[num:])
return errorx.Rawf("entered too many arguments: %v", args[num:])
}
return
}
Expand Down Expand Up @@ -296,11 +296,10 @@ func (a *Argument) HelpName() string {
if a.Arrayed {
return a.ShowName + "..."
}

return a.ShowName
}

// With an func for config the argument
// With a func for config the argument
func (a *Argument) With(fn func(arg *Argument)) *Argument {
if fn != nil {
fn(a)
Expand All @@ -309,7 +308,7 @@ func (a *Argument) With(fn func(arg *Argument)) *Argument {
return a
}

// WithValidator set an value validator of the argument
// WithValidator set a value validator of the argument
func (a *Argument) WithValidator(fn func(interface{}) (interface{}, error)) *Argument {
a.Validator = fn
return a
Expand Down
44 changes: 11 additions & 33 deletions gcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,59 +111,37 @@ func Version() string {
}

// CommitID of the gcli
func CommitID() string {
return commitID
}
func CommitID() string { return commitID }

// Verbose returns verbose level
func Verbose() VerbLevel {
return gOpts.Verbose()
}
func Verbose() VerbLevel { return gOpts.Verbose() }

// SetCrazyMode level
func SetCrazyMode() {
gOpts.SetVerbose(VerbCrazy)
}
func SetCrazyMode() { gOpts.SetVerbose(VerbCrazy) }

// SetDebugMode level
func SetDebugMode() {
gOpts.SetVerbose(VerbDebug)
}
func SetDebugMode() { gOpts.SetVerbose(VerbDebug) }

// SetQuietMode level
func SetQuietMode() {
gOpts.SetVerbose(VerbQuiet)
}
func SetQuietMode() { gOpts.SetVerbose(VerbQuiet) }

// SetVerbose level
func SetVerbose(verbose VerbLevel) {
gOpts.SetVerbose(verbose)
}
func SetVerbose(verbose VerbLevel) { gOpts.SetVerbose(verbose) }

// ResetVerbose level
func ResetVerbose() {
gOpts.SetVerbose(DefaultVerb)
}
func ResetVerbose() { gOpts.SetVerbose(DefaultVerb) }

// StrictMode get is strict mode
func StrictMode() bool {
return gOpts.strictMode
}
func StrictMode() bool { return gOpts.strictMode }

// SetStrictMode for parse flags
func SetStrictMode(strict bool) {
gOpts.SetStrictMode(strict)
}
func SetStrictMode(strict bool) { gOpts.SetStrictMode(strict) }

// IsGteVerbose get is strict mode
func IsGteVerbose(verb VerbLevel) bool {
return gOpts.verbose >= verb
}
func IsGteVerbose(verb VerbLevel) bool { return gOpts.verbose >= verb }

// IsDebugMode get is debug mode
func IsDebugMode() bool {
return gOpts.verbose >= VerbDebug
}
func IsDebugMode() bool { return gOpts.verbose >= VerbDebug }

// Commander interface
type Commander interface {
Expand Down

0 comments on commit 1f64ca6

Please sign in to comment.