Skip to content

Commit

Permalink
up: update some for flag parse and cmd init
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 4, 2022
1 parent 4c7ed4b commit 08e163b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 8 additions & 2 deletions gflag/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import (
* Arguments definition
*************************************************************/

// CliArgs alias of Arguments
type CliArgs = Arguments

// Arguments definition
type Arguments struct {
// name inherited from gcli.Command
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions gflag/gflag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand Down
10 changes: 6 additions & 4 deletions gflag/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
1 change: 1 addition & 0 deletions show/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 08e163b

Please sign in to comment.