From f91b5b558a354da6f1381fa8ca37ac78f5e859f3 Mon Sep 17 00:00:00 2001 From: John Guo Date: Wed, 24 Nov 2021 22:45:34 +0800 Subject: [PATCH] improve package gcmd --- os/gcmd/gcmd_parser.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/os/gcmd/gcmd_parser.go b/os/gcmd/gcmd_parser.go index 746381c9a..1ed5f28eb 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