Skip to content

Commit

Permalink
fix bindOpts to detect user's Args via WithArgs(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
hedzr committed Oct 31, 2024
1 parent a5fa52f commit bc3d1f3
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions cli/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
func New(cfg *cli.Config, opts ...cli.Opt) *workerS {
w := &workerS{Config: cfg}
w.setArgs(cfg.Args)
bindOpts(w, false, opts...)
bindOpts(context.TODO(), w, false, opts...)
return w
}

Expand Down Expand Up @@ -343,7 +343,7 @@ func (w *workerS) SuggestRetCode() int { return w.retCode } //
func (w *workerS) SetSuggestRetCode(ret int) { w.retCode = ret }
func (w *workerS) ParsedState() cli.ParsedState { return w.parsingCtx }

func bindOpts[Opt cli.Opt](w *workerS, installAsUnique bool, opts ...Opt) {
func bindOpts[Opt cli.Opt](ctx context.Context, w *workerS, installAsUnique bool, opts ...Opt) {
for _, opt := range opts {
opt(w.Config)
}
Expand All @@ -355,6 +355,15 @@ func bindOpts[Opt cli.Opt](w *workerS, installAsUnique bool, opts ...Opt) {
w.wrDebugScreen = w.DebugScreenWriter
}

// if cx, ok := w.root.Cmd.(*cli.CmdS); ok {
// cx.EnsureTree(ctx, w, w.root)
// }

// update args with w.Config.Args
if len(w.Config.Args) > 0 {
w.args = w.Config.Args
}

if installAsUnique {
if app := UniqueWorker(); app != w {
SetUniqueWorker(w)
Expand All @@ -363,7 +372,7 @@ func bindOpts[Opt cli.Opt](w *workerS, installAsUnique bool, opts ...Opt) {
}

func (w *workerS) Run(ctx context.Context, opts ...cli.Opt) (err error) {
bindOpts(w, true, opts...)
bindOpts(ctx, w, true, opts...)

// shutdown basics.Closers for the registered Peripheral, Closers.
// See also: basics.RegisterPeripheral, basics.RegisterClosable,
Expand All @@ -374,7 +383,8 @@ func (w *workerS) Run(ctx context.Context, opts ...cli.Opt) (err error) {
w.errs = errors.New(w.root.AppName)
defer w.errs.Defer(&err)

if w.attachError(w.preProcess(ctx)) {
if err = w.preProcess(ctx); err != nil {
w.attachErrors(err)
return
}

Expand Down

0 comments on commit bc3d1f3

Please sign in to comment.