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