mirror of
https://github.com/lancatlin/mdsh.git
synced 2026-11-05 19:30:12 +08:00
feat: add parseParams
This commit is contained in:
1 parent
437efc6f2e
commit
12dcbb460c
2 files changed
+30
-4
No files matched your search
@@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func parseParams(meta TemplateMeta) (ctx TemplateContext, err error) {
|
||||
result := make(map[string]*string)
|
||||
for key, spec := range meta.Params {
|
||||
result[key] = flag.String(key, spec.Default, key)
|
||||
}
|
||||
flag.Parse()
|
||||
ctx = make(TemplateContext)
|
||||
for key, spec := range meta.Params {
|
||||
if spec.Required && *result[key] == "" {
|
||||
err = fmt.Errorf("parameter '%s' required but not provided", key)
|
||||
return
|
||||
}
|
||||
ctx[key] = *result[key]
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in new issue
Block a user