improve package gcmd

This commit is contained in:
John Guo
2021-11-24 22:45:34 +08:00
parent 0db3a9f0f6
commit f91b5b558a

View File

@ -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