From b7b71795a417d938f5796cc0316e815e47427699 Mon Sep 17 00:00:00 2001 From: John Guo Date: Tue, 7 Dec 2021 22:19:28 +0800 Subject: [PATCH] improve package gcmd --- os/gcmd/gcmd_command_object.go | 1 - os/gcmd/gcmd_command_run.go | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/os/gcmd/gcmd_command_object.go b/os/gcmd/gcmd_command_object.go index 3158bb94c..af4282b21 100644 --- a/os/gcmd/gcmd_command_object.go +++ b/os/gcmd/gcmd_command_object.go @@ -208,7 +208,6 @@ func newCommandFromMethod(object interface{}, method reflect.Value) (command *Co // Create function that has value return. command.FuncWithValue = func(ctx context.Context, parser *Parser) (out interface{}, err error) { ctx = context.WithValue(ctx, CtxKeyParser, parser) - ctx = context.WithValue(ctx, CtxKeyCommand, command) defer func() { if exception := recover(); exception != nil { diff --git a/os/gcmd/gcmd_command_run.go b/os/gcmd/gcmd_command_run.go index 2622079b0..8cc3bbb2d 100644 --- a/os/gcmd/gcmd_command_run.go +++ b/os/gcmd/gcmd_command_run.go @@ -56,6 +56,8 @@ func (c *Command) RunWithValue(ctx context.Context) (value interface{}, err erro } func (c *Command) doRun(ctx context.Context, parser *Parser) (value interface{}, err error) { + ctx = context.WithValue(ctx, CtxKeyCommand, c) + // Check built-in help command. if parser.ContainsOpt(helpOptionName) || parser.ContainsOpt(helpOptionNameShort) { if c.HelpFunc != nil {