mirror of
https://github.com/go-task/task.git
synced 2025-05-13 22:16:31 +02:00
58 lines
1.3 KiB
Go
58 lines
1.3 KiB
Go
package args
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/spf13/pflag"
|
|
"mvdan.cc/sh/v3/syntax"
|
|
|
|
"github.com/go-task/task/v3"
|
|
"github.com/go-task/task/v3/taskfile/ast"
|
|
)
|
|
|
|
// Get fetches the remaining arguments after CLI parsing and splits them into
|
|
// two groups: the arguments before the double dash (--) and the arguments after
|
|
// the double dash.
|
|
func Get() ([]string, string, error) {
|
|
args := pflag.Args()
|
|
doubleDashPos := pflag.CommandLine.ArgsLenAtDash()
|
|
|
|
if doubleDashPos == -1 {
|
|
return args, "", nil
|
|
}
|
|
|
|
var quotedCliArgs []string
|
|
for _, arg := range args[doubleDashPos:] {
|
|
quotedCliArg, err := syntax.Quote(arg, syntax.LangBash)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
quotedCliArgs = append(quotedCliArgs, quotedCliArg)
|
|
}
|
|
|
|
return args[:doubleDashPos], strings.Join(quotedCliArgs, " "), nil
|
|
}
|
|
|
|
// Parse parses command line argument: tasks and global variables
|
|
func Parse(args ...string) ([]*task.Call, *ast.Vars) {
|
|
calls := []*task.Call{}
|
|
globals := ast.NewVars()
|
|
|
|
for _, arg := range args {
|
|
if !strings.Contains(arg, "=") {
|
|
calls = append(calls, &task.Call{Task: arg})
|
|
continue
|
|
}
|
|
|
|
name, value := splitVar(arg)
|
|
globals.Set(name, ast.Var{Value: value})
|
|
}
|
|
|
|
return calls, globals
|
|
}
|
|
|
|
func splitVar(s string) (string, string) {
|
|
pair := strings.SplitN(s, "=", 2)
|
|
return pair[0], pair[1]
|
|
}
|