From 08e163b7a546c812648b796e940dd26a5073c8d3 Mon Sep 17 00:00:00 2001 From: Inhere Date: Tue, 4 Oct 2022 12:25:13 +0800 Subject: [PATCH] up: update some for flag parse and cmd init --- cmd.go | 2 +- gflag/args.go | 10 ++++++++-- gflag/gflag_test.go | 3 +-- gflag/parser.go | 10 ++++++---- show/README.md | 1 + 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/cmd.go b/cmd.go index 78376a1..1776a8b 100644 --- a/cmd.go +++ b/cmd.go @@ -263,7 +263,7 @@ func (c *Command) initialize() { } // init for cmd flags parser - c.InitParser(cName) + c.Flags.Init(cName) // format description if len(c.Desc) > 0 { diff --git a/gflag/args.go b/gflag/args.go index e566716..bb0157d 100644 --- a/gflag/args.go +++ b/gflag/args.go @@ -15,6 +15,9 @@ import ( * Arguments definition *************************************************************/ +// CliArgs alias of Arguments +type CliArgs = Arguments + // Arguments definition type Arguments struct { // name inherited from gcli.Command @@ -26,6 +29,8 @@ type Arguments struct { // {"arg1", "this is second argument", false, false}, // ] args []*Argument + // arg name max width + argWidth int // record min length for args // argsMinLen int // record argument names and defined positional relationships @@ -36,8 +41,6 @@ type Arguments struct { // "arg1": 1, // } argsIndexes map[string]int - // arg name max width - argWidth int // validate the args number is right validateNum bool // mark exists array argument @@ -246,6 +249,9 @@ func (ags *Arguments) BuildArgsHelp() string { * Argument definition *************************************************************/ +// CliArg alias of Argument +type CliArg = Argument + // Argument a command argument definition type Argument struct { *structs.Value diff --git a/gflag/gflag_test.go b/gflag/gflag_test.go index 3b03ff6..e363e19 100644 --- a/gflag/gflag_test.go +++ b/gflag/gflag_test.go @@ -494,14 +494,13 @@ func TestFlags_PrintHelpPanel(t *testing.T) { } func TestFlagMeta_Validate(t *testing.T) { - fm := gcli.FlagMeta{ + fm := gflag.CliOpt{ Name: "opt1", Required: true, Validator: func(val string) error { if len(val) < 5 { return errors.New("flag value min len is 5") } - return nil }, } diff --git a/gflag/parser.go b/gflag/parser.go index 66ce637..3f17a94 100644 --- a/gflag/parser.go +++ b/gflag/parser.go @@ -74,11 +74,13 @@ func New(nameWithDesc ...string) *Parser { return p } -// InitParser for parser -func (p *Parser) InitParser(name string) { - if p.out == nil { - p.out = os.Stdout +// Init for parser +func (p *Parser) Init(name string) { + if p.out != nil { + return } + + p.out = os.Stdout if p.cfg == nil { p.cfg = newDefaultFlagConfig() } diff --git a/show/README.md b/show/README.md index 9c59340..161dafa 100644 --- a/show/README.md +++ b/show/README.md @@ -19,3 +19,4 @@ contains `section, panel, padding, helpPanel, table, tree, title, list, multiLis - https://github.com/InVisionApp/tabular - https://github.com/gosuri/uitable - https://github.com/tomlazar/table +- https://github.com/nwidger/jsoncolor