Skip to content

Commit

Permalink
refactor: refactoring the command flags help render logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 6, 2022
1 parent 5295721 commit ce659b1
Show file tree
Hide file tree
Showing 6 changed files with 323 additions and 389 deletions.
2 changes: 1 addition & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func New(fns ...func(app *App)) *App {
}

// NewApp create new app instance.
//
// Usage:
// NewApp()
// // Or with a config func
Expand Down Expand Up @@ -101,7 +102,6 @@ func NewApp(fns ...func(app *App)) *App {
Hooks: &Hooks{},
gFlags: NewFlags("app.GOptions").WithConfigFn(func(opt *FlagsConfig) {
opt.WithoutType = true
opt.NameDescOL = true
opt.Alignment = AlignLeft
}),
}
Expand Down
23 changes: 5 additions & 18 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,9 @@ func (c *Command) Match(names []string) *Command {
// ensure is initialized
c.initialize()

ln := len(names)
if ln == 0 { // return self.
if len(names) == 0 { // return self.
return c
}

return c.commandBase.Match(names)
}

Expand All @@ -251,7 +249,6 @@ func (c *Command) initialize() {

// check command name
cName := c.goodName()

Debugf("initialize the command '%s'", cName)

c.initialized = true
Expand All @@ -272,13 +269,10 @@ func (c *Command) initialize() {

// init for cmd Arguments
c.Arguments.SetName(cName)
c.Arguments.SetValidateNum(gOpts.strictMode)
// c.Arguments.SetValidateNum(gOpts.strictMode)

// init for cmd Flags
c.Flags.SetConfig(newDefaultFlagOption())
c.Flags.InitFlagSet(cName)
// c.Flags.SetOption(cName)
// c.Flags.FSet().SetOutput(c.Flags.out)
// c.Flags.FSet().Usage = func() { // call on exists "-h" "--help"
// Logf(VerbDebug, "render help on exists '-h|--help' or has unknown flag")
// c.ShowHelp()
Expand Down Expand Up @@ -367,8 +361,7 @@ var errCallRunOnSub = errors.New("c.Run() cannot allow call at subcommand")
// cmd.MustRun([]string{"-a", ...})
func (c *Command) MustRun(args []string) {
if err := c.Run(args); err != nil {
color.Error.Println("ERROR:", err.Error())
panic(err)
color.Errorln("ERROR:", err.Error())
}
}

Expand All @@ -381,12 +374,8 @@ func (c *Command) MustRun(args []string) {
// // custom args
// cmd.Run([]string{"-a", ...})
func (c *Command) Run(args []string) (err error) {
if c.app != nil {
return errCallRunOnApp
}

if c.parent != nil {
return errCallRunOnSub
if c.app != nil || c.parent != nil {
return c.innerDispatch(args)
}

// mark is standalone
Expand Down Expand Up @@ -522,8 +511,6 @@ 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)

// NOTICE: disable output internal error message on parse flags
// c.FSet().SetOutput(ioutil.Discard)
Debugf("cmd: %s - will parse options by args: %v", c.Name, args)

// parse options, don't contains command name.
Expand Down
Loading

0 comments on commit ce659b1

Please sign in to comment.