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
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
)
|
||||
|
||||
var tmpl = `
|
||||
@@ -27,8 +28,10 @@ func main() {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("'%s'", templateMeta)
|
||||
render(body, map[string]string{
|
||||
"from": "2025-07-15",
|
||||
})
|
||||
fmt.Println(templateMeta)
|
||||
ctx, err := parseParams(templateMeta)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
render(body, ctx)
|
||||
}
|
||||
@@ -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