Add support for delegating CLI arguments with "--" and a special CLI_ARGS variable

Closes #327
This commit is contained in:
Andrey Nering
2021-03-20 11:56:19 -03:00
parent 8994c50d34
commit e6c4706b73
7 changed files with 68 additions and 16 deletions

View File

@@ -7,6 +7,7 @@ import (
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"github.com/spf13/pflag"
@@ -157,14 +158,18 @@ func main() {
}
var (
calls []taskfile.Call
globals *taskfile.Vars
calls []taskfile.Call
globals *taskfile.Vars
tasksAndVars, cliArgs = getArgs()
)
if v >= 3.0 {
calls, globals = args.ParseV3(pflag.Args()...)
calls, globals = args.ParseV3(tasksAndVars...)
} else {
calls, globals = args.ParseV2(pflag.Args()...)
calls, globals = args.ParseV2(tasksAndVars...)
}
globals.Set("CLI_ARGS", taskfile.Var{Static: strings.Join(cliArgs, " ")})
e.Taskfile.Vars.Merge(globals)
ctx := context.Background()
@@ -185,6 +190,22 @@ func main() {
}
}
func getArgs() (tasksAndVars, cliArgs []string) {
var (
args = pflag.Args()
doubleDashPos = pflag.CommandLine.ArgsLenAtDash()
)
if doubleDashPos != -1 {
tasksAndVars = args[:doubleDashPos]
cliArgs = args[doubleDashPos:]
} else {
tasksAndVars = args
}
return
}
func getSignalContext() context.Context {
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt, os.Kill, syscall.SIGTERM)