args.go 586 B

123456789101112131415161718192021222324252627282930
  1. package args
  2. import (
  3. "strings"
  4. "github.com/go-task/task/v3/taskfile/ast"
  5. )
  6. // Parse parses command line argument: tasks and global variables
  7. func Parse(args ...string) ([]*ast.Call, *ast.Vars) {
  8. calls := []*ast.Call{}
  9. globals := &ast.Vars{}
  10. for _, arg := range args {
  11. if !strings.Contains(arg, "=") {
  12. calls = append(calls, &ast.Call{Task: arg})
  13. continue
  14. }
  15. name, value := splitVar(arg)
  16. globals.Set(name, ast.Var{Value: value})
  17. }
  18. return calls, globals
  19. }
  20. func splitVar(s string) (string, string) {
  21. pair := strings.SplitN(s, "=", 2)
  22. return pair[0], pair[1]
  23. }