mirror of
https://github.com/lancatlin/mdsh.git
synced 2026-11-08 03:30:12 +08:00
feat: add usage key in params and improve messages
This commit is contained in:
1 parent
96f526ac99
commit
8f764548b9
4 files changed
+35
-13
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
@@ -10,16 +11,21 @@ func parseParams(meta TemplateMeta) (ctx TemplateContext, err error) {
|
||||
result := make(map[string]*string)
|
||||
fs := flag.NewFlagSet("params", flag.ExitOnError)
|
||||
for key, spec := range meta.Params {
|
||||
result[key] = fs.String(key, spec.Default, key)
|
||||
result[key] = fs.String(key, spec.Default, spec.Usage)
|
||||
}
|
||||
fs.Parse(os.Args[2:])
|
||||
ctx = make(TemplateContext)
|
||||
message := ""
|
||||
for key, spec := range meta.Params {
|
||||
if spec.Required && *result[key] == "" {
|
||||
err = fmt.Errorf("parameter '%s' required but not provided", key)
|
||||
return
|
||||
message += fmt.Sprintf("parameter '%s' required but not provided\n", key)
|
||||
continue
|
||||
}
|
||||
ctx[key] = *result[key]
|
||||
}
|
||||
if message != "" {
|
||||
message += fmt.Sprintf("Use 'mdsh %s --help' to see usage", os.Args[1])
|
||||
return nil, errors.New(message)
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in new issue
Block a user