2017-09-07 13:57:06 -03:00
|
|
|
package args
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2018-11-04 21:23:35 -02:00
|
|
|
"github.com/go-task/task/v2/internal/taskfile"
|
2017-09-07 13:57:06 -03:00
|
|
|
)
|
|
|
|
|
2017-09-07 14:40:54 -03:00
|
|
|
// Parse parses command line argument: tasks and vars of each task
|
2019-05-11 11:06:47 -03:00
|
|
|
func Parse(args ...string) ([]taskfile.Call, taskfile.Vars) {
|
2018-02-17 14:22:18 -02:00
|
|
|
var calls []taskfile.Call
|
2019-05-11 11:06:47 -03:00
|
|
|
var globals taskfile.Vars
|
2017-09-07 13:57:06 -03:00
|
|
|
|
|
|
|
for _, arg := range args {
|
|
|
|
if !strings.Contains(arg, "=") {
|
2018-02-17 14:22:18 -02:00
|
|
|
calls = append(calls, taskfile.Call{Task: arg})
|
2017-09-07 13:57:06 -03:00
|
|
|
continue
|
|
|
|
}
|
2019-05-11 11:06:47 -03:00
|
|
|
|
2017-09-07 13:57:06 -03:00
|
|
|
if len(calls) < 1 {
|
2019-05-11 11:06:47 -03:00
|
|
|
if globals == nil {
|
|
|
|
globals = taskfile.Vars{}
|
|
|
|
}
|
2017-09-07 13:57:06 -03:00
|
|
|
|
2019-05-11 11:06:47 -03:00
|
|
|
name, value := splitVar(arg)
|
|
|
|
globals[name] = taskfile.Var{Static: value}
|
|
|
|
} else {
|
|
|
|
if calls[len(calls)-1].Vars == nil {
|
|
|
|
calls[len(calls)-1].Vars = make(taskfile.Vars)
|
|
|
|
}
|
2017-09-07 13:57:06 -03:00
|
|
|
|
2019-05-11 11:06:47 -03:00
|
|
|
name, value := splitVar((arg))
|
|
|
|
calls[len(calls)-1].Vars[name] = taskfile.Var{Static: value}
|
|
|
|
}
|
2017-09-07 13:57:06 -03:00
|
|
|
}
|
2019-05-11 11:06:47 -03:00
|
|
|
|
|
|
|
return calls, globals
|
|
|
|
}
|
|
|
|
|
|
|
|
func splitVar(s string) (string, string) {
|
|
|
|
pair := strings.SplitN(s, "=", 2)
|
|
|
|
return pair[0], pair[1]
|
2017-09-07 13:57:06 -03:00
|
|
|
}
|