Skip to content

Commit

Permalink
up: update some logic for alone run command
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 7, 2022
1 parent 4d91775 commit a5d0cc8
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 19 deletions.
3 changes: 1 addition & 2 deletions builtin/gen_auto_complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ func GenAutoComplete() *gcli.Command {
Func: doGen,
Name: "genac",
Aliases: []string{"gen-ac"},
// des
Desc: "generate auto complete scripts for current application",
Desc: "generate auto complete scripts for current application",
}

genOpts._selfName = c.Name
Expand Down
2 changes: 1 addition & 1 deletion cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ func (c *Command) parseOptions(args []string) (ss []string, err error) {
// args = moveArgumentsToEnd(args)
// Debugf("cmd: %s - option flags on after format: %v", c.Name, args)

Debugf("cmd: %s - will parse options by args: %v", c.Name, args)
Debugf("cmd: %s - will parse options from args: %v", c.Name, args)

// parse options, don't contains command name.
if err = c.Parse(args); err != nil {
Expand Down
8 changes: 4 additions & 4 deletions gcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,13 @@ func (g *GOptions) NoProgress() bool {

func (g *GOptions) bindingFlags(fs *Flags) {
// up: allow use int and string.
fs.VarOpt(&g.verbose, "verbose", "", "Set error reporting level(quiet 0 - 5 crazy)")
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", "", g.NoColor, "Disable color when outputting message")
fs.BoolOpt(&g.noProgress, "no-progress", "", g.noProgress, "Disable display progress message")
fs.BoolOpt(&g.noInteractive, "no-interactive", "", g.noInteractive, "Disable interactive confirmation operations")
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
40 changes: 28 additions & 12 deletions gflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ func (fs *Flags) Parse(args []string) (err error) {
return err
}

if gOpts.showHelp {
return flag.ErrHelp
}

// call flags validate
for name, meta := range fs.metas {
fItem := fs.fSet.Lookup(name)
Expand Down Expand Up @@ -262,7 +266,7 @@ var (
)

// FromStruct from struct tag binding options
func (fs *Flags) FromStruct(ptr interface{}) error {
func (fs *Flags) FromStruct(ptr any) error {
v := reflect.ValueOf(ptr)
if v.Kind() != reflect.Ptr {
return errNotPtrValue
Expand Down Expand Up @@ -623,7 +627,7 @@ func (fs *Flags) checkFlagInfo(meta *FlagMeta) string {
}

// check flag name
name := meta.goodName()
name := meta.initCheck()
if _, ok := fs.metas[name]; ok {
panicf("redefined option flag '%s'", name)
}
Expand Down Expand Up @@ -904,7 +908,7 @@ type FlagMeta struct {
// Name of flag and description
Name, Desc string
// default value for the flag option
DefVal interface{}
DefVal any
// wrapped the default value
defVal *structs.Value
// short names. eg: ["o", "a"]
Expand All @@ -921,7 +925,7 @@ type FlagMeta struct {
}

// newFlagMeta quick create an FlagMeta
func newFlagMeta(name, desc string, defVal interface{}, shortcut string) *FlagMeta {
func newFlagMeta(name, desc string, defVal any, shortcut string) *FlagMeta {
return &FlagMeta{
Name: name,
Desc: desc,
Expand All @@ -936,12 +940,7 @@ func (m *FlagMeta) Shorts2String(sep ...string) string {
if len(m.Shorts) == 0 {
return ""
}

char := ","
if len(sep) > 0 {
char = sep[0]
}
return strings.Join(m.Shorts, char)
return strings.Join(m.Shorts, sepStr(sep))
}

// HelpName for show help
Expand Down Expand Up @@ -974,8 +973,25 @@ func (m *FlagMeta) DValue() *stdutil.Value {
return m.defVal
}

func (m *FlagMeta) initCheck() {
m.goodName()
func (m *FlagMeta) initCheck() string {
if m.Desc != "" {
desc := strings.Trim(m.Desc, "; ")
if strings.ContainsRune(desc, ';') {
// format: desc;required
parts := strutil.SplitNTrimmed(desc, ";", 2)
if ln := len(parts); ln > 1 {
bl, err := strutil.Bool(parts[1])
if err == nil && bl {
desc = parts[0]
m.Required = true
}
}
}

m.Desc = desc
}

return m.goodName()
}

// good name of the flag
Expand Down
8 changes: 8 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/gookit/color"
"github.com/gookit/goutil/arrutil"
"github.com/gookit/goutil/common"
"github.com/gookit/goutil/stdutil"
"github.com/gookit/goutil/strutil"
)
Expand Down Expand Up @@ -99,6 +100,13 @@ func panicf(format string, v ...interface{}) {
panic(fmt.Sprintf("GCli: "+format, v...))
}

func sepStr(seps []string) string {
if len(seps) > 0 {
return seps[0]
}
return common.DefaultSep
}

// allowed keys on struct tag.
var flagTagKeys = arrutil.Strings{"name", "shorts", "desc", "default", "required"}

Expand Down

0 comments on commit a5d0cc8

Please sign in to comment.