mirror of
https://github.com/go-task/task.git
synced 2025-03-21 21:27:07 +02:00
- this makes it impossible to import these packages outside Task - as a side effect, it makes the root directory cleaner
37 lines
789 B
Go
37 lines
789 B
Go
package args
|
|
|
|
import (
|
|
"errors"
|
|
"strings"
|
|
|
|
"github.com/go-task/task"
|
|
)
|
|
|
|
var (
|
|
// ErrVariableWithoutTask is returned when variables are given before any task
|
|
ErrVariableWithoutTask = errors.New("task: variable given before any task")
|
|
)
|
|
|
|
// Parse parses command line argument: tasks and vars of each task
|
|
func Parse(args ...string) ([]task.Call, error) {
|
|
var calls []task.Call
|
|
|
|
for _, arg := range args {
|
|
if !strings.Contains(arg, "=") {
|
|
calls = append(calls, task.Call{Task: arg})
|
|
continue
|
|
}
|
|
if len(calls) < 1 {
|
|
return nil, ErrVariableWithoutTask
|
|
}
|
|
|
|
if calls[len(calls)-1].Vars == nil {
|
|
calls[len(calls)-1].Vars = make(task.Vars)
|
|
}
|
|
|
|
pair := strings.SplitN(arg, "=", 2)
|
|
calls[len(calls)-1].Vars[pair[0]] = task.Var{Static: pair[1]}
|
|
}
|
|
return calls, nil
|
|
}
|