diff --git a/builtin/gen_auto_complete.go b/builtin/gen_auto_complete.go index 8888a3d..96b052d 100644 --- a/builtin/gen_auto_complete.go +++ b/builtin/gen_auto_complete.go @@ -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 diff --git a/cmd.go b/cmd.go index f6ddcd9..779844a 100644 --- a/cmd.go +++ b/cmd.go @@ -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 { diff --git a/gcli.go b/gcli.go index 6e73919..9c9c0fe 100644 --- a/gcli.go +++ b/gcli.go @@ -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") } /************************************************************************* diff --git a/gflag.go b/gflag.go index 8314748..aa8a5a2 100644 --- a/gflag.go +++ b/gflag.go @@ -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) @@ -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 @@ -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) } @@ -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"] @@ -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, @@ -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 @@ -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 diff --git a/util.go b/util.go index c2ecb91..129291f 100644 --- a/util.go +++ b/util.go @@ -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" ) @@ -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"}