diff --git a/os/gcmd/gcmd_parser.go b/os/gcmd/gcmd_parser.go index 746381c9ad1..1ed5f28eb1e 100644 --- a/os/gcmd/gcmd_parser.go +++ b/os/gcmd/gcmd_parser.go @@ -8,6 +8,7 @@ package gcmd import ( + "context" "os" "strings" @@ -30,6 +31,16 @@ type Parser struct { commandFuncMap map[string]func() // Command function map for function handler. } +// ParserFromCtx retrieves and returns Parser from context. +func ParserFromCtx(ctx context.Context) *Parser { + if v := ctx.Value(CtxKeyParser); v != nil { + if p, ok := v.(*Parser); ok { + return p + } + } + return nil +} + // Parse creates and returns a new Parser with os.Args and supported options. // // Note that the parameter `supportedOptions` is as [option name: need argument], which means